Skip to content

Pie: respect LineStyle, fix automatic axis limits, and demonstrate percent labels#4278

Merged
swharden merged 8 commits intoScottPlot:mainfrom
LeaFrock:pie_percentage
Oct 9, 2024
Merged

Pie: respect LineStyle, fix automatic axis limits, and demonstrate percent labels#4278
swharden merged 8 commits intoScottPlot:mainfrom
LeaFrock:pie_percentage

Conversation

@LeaFrock
Copy link
Contributor

@LeaFrock LeaFrock commented Sep 20, 2024

Bring the feature of Pie plot which ScottPlot 4 owns to 5.

image

@aespitia
Copy link
Contributor

aespitia commented Sep 23, 2024

sorry, i was talking about this part:

slicePercentFormat.StartsWith('P') // Just follow the code quality rule CA1865
#else
slicePercentFormat.StartsWith("P", StringComparison.Ordinal)

in common usage, lowercase "p" actually works too.

double number = .2468013;

Console.WriteLine(number.ToString("p", CultureInfo.InvariantCulture));
// Displays 24.68 %

Console.WriteLine(number.ToString("p", CultureInfo.CreateSpecificCulture("hr-HR")));
// Displays 24,68%

Console.WriteLine(number.ToString("p1", CultureInfo.InvariantCulture));
// Displays 24.7 %

@LeaFrock
Copy link
Contributor Author

Well, the document doesn't mention the lower case. But after I check the source codes of double.ToString , I admit that you're right. I'll improve it then.

@swharden
Copy link
Member

swharden commented Oct 9, 2024

Hi @LeaFrock and @aespitia,

I believe this functionality is possible with the existing library and a code change is not required. I'll adapt this PR to add a cookbook recipe that demonstrates this 👍

// create a pie chart
double[] values = [6, 8, 10];
var pie = myPlot.Add.Pie(values);
pie.ExplodeFraction = .1;
pie.ShowSliceLabels = true;
pie.SliceLabelDistance = 0.5;

// determine percentages for each slice
double total = pie.Slices.Select(x => x.Value).Sum();
double[] percentages = pie.Slices.Select(x => x.Value / total * 100).ToArray();

// set each slice label to its percentage
for (int i = 0; i < pie.Slices.Count; i++)
{
    pie.Slices[i].Label = $"{percentages[i]:0.0}%";
    pie.Slices[i].LabelFontSize = 20;
    pie.Slices[i].LabelBold = true;
    pie.Slices[i].LabelFontColor = Colors.Black.WithAlpha(.5);
}

// hide unnecessary plot components
myPlot.Axes.Frameless();
myPlot.HideGrid();
myPlot.HideLegend();

image

@swharden swharden changed the title Pie: Add percent labels Pie: respect LineStyle, fix automatic axis limits, and demonstrate percent labels Oct 9, 2024
@swharden swharden enabled auto-merge (squash) October 9, 2024 19:53
@swharden swharden merged commit b6821d9 into ScottPlot:main Oct 9, 2024
@LeaFrock
Copy link
Contributor Author

@swharden The problem is that sometimes we want two labels: one is inside(as a percent) and the other is outside(as a tag or other information). It's the original feature request of this PR.

The current SliceLabel does not satisfy the requirement.

swharden added a commit to johndoh/ScottPlot that referenced this pull request Oct 10, 2024
swharden added a commit that referenced this pull request Oct 10, 2024
…) (#4305)

* Coxcomb: fix position of intiial slice to match rotation setting (#4304)

* Coxcomb: improve first slice alignment

re-implementation of #4305 after #4278 and #4280 merge conflicts

* Update CHANGELOG.md

---------

Co-authored-by: Scott W Harden <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants