Forum > TAChart

Pie chart?[SOLVED]

(1/2) > >>

Robert W.B.:
I try to create a simple piechart for odd and even numbers but, I can't success!
I looked at the exampel filein Lazarus but it can't solved my problem!  %)

Is there a simple way to create a basic piechart? :(

Rob

wp:

--- Quote from: Robert W.B. on December 22, 2021, 01:16:26 pm ---I try to create a simple piechart for odd and even numbers

--- End quote ---
What do you mean with "piechart for odd and even numbers"?

Suppose there is an opinion poll for one of several options to choose from:
- 10 voters are for option "A"
- 23 voters are for option "B"
- 2 voters are for option "C".

Add a TPieSeries to a TChart and add these data to the Pieseries, for example in the OnCreate event of the form:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);begin  Chart1PieSeries1.AddXY(0, 10, 'A');  Chart1PieSeries1.AddXY(0, 23, 'B');  Chart1PieSeries1.AddXY(0,  2, 'C');end; Let's discuss the arguments from right to left: the last argument is the name of the option selected. The second argument is the number of voters for this option. The first argument is the distance of the pie from the center. When it is zero all pies touch each other in the center of the circle. When you increase the first parameter the  pie is moved away from the center. The first parameter is measured in units of the pie radius, so be moderate in using non-zero values. The following example moves pie "C" away from the center by a quarter of the pie radius. Note that the "Exploded" property must be set to true for this to become effective.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);begin  Chart1PieSeries1.AddXY(0, 10, 'A');  Chart1PieSeries1.AddXY(0, 23, 'B');  Chart1PieSeries1.AddXY(0.25,  2, 'C');  Chart1PieSeries1.Exploded := true;end;

winni:
Hi!

Canvas.Pie draws a pie segment:

https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tcanvas.pie.html

Winni

Robert W.B.:
Great :) That's what I looking for. Many thanks wp.
If You want to set percent and a specific colors on the chart. How can I do that?

Rob

wp:
In order to modify pie colors add the requested color as last parameter after the pie title.
To see the value labels with percentages set the PieSeries' Marks.Style to smsLabelPercent.

Some other options are used in the following code snippet. See also the documentation https://wiki.lazarus.freepascal.org/TAChart_documentation#Pie_series, but note that this is kept up-to-date with Laz-main; if you have an older version, not every feature mentioned there will work.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  TAChartUtils, TARadialSeries, TALegend; procedure TForm1.FormCreate(Sender: TObject);begin  Chart1.BottomAxis.Visible := false;  Chart1.LeftAxis.Visible := false;  Chart1.Frame.Visible := false;    Chart1PieSeries1.AddXY(0.00, 10, 'A', RgbToColor(200, 180, 164));  Chart1PieSeries1.AddXY(0.00, 23, 'B', RgbToColor(92, 224, 172));  Chart1PieSeries1.AddXY(0.25,  2, 'C', clYellow);  Chart1PieSeries1.Exploded := true;  Chart1PieSeries1.Marks.Style := smsLabelPercent;     // requires TAChartUtils in "uses"  Chart1PieSeries1.MarkPositions := pmpInside;         // requires TARadialSeries in "uses"  Chart1PieSeries1.Depth := 20;   Chart1.Legend.Visible := true;  Chart1PieSeries1.Legend.Multiplicity := lmPoint;    // requires TALegend to "uses"   { if you have Lazarus 2.2RC2: activate the following lines to create an image like in the screenshot... }  //Chart1PieSeries1.InnerRadiusPercent := 25;  //Chart1PieSeries1.DepthBrightnessDelta := -50;  //Chart1PieSeries1.Orientation := poHorizontal;end; ;