Recent

Author Topic: Pie chart?[SOLVED]  (Read 4023 times)

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 324
  • Love my Wife, My Kids and Lazarus/Freepascal.
Pie chart?[SOLVED]
« on: December 22, 2021, 01:16:26 pm »
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? :(

Thanks in advance
Rob
« Last Edit: December 22, 2021, 05:30:38 pm by Robert W.B. »
Rob

wp

  • Hero Member
  • *****
  • Posts: 9921
Re: Pie chart?
« Reply #1 on: December 22, 2021, 01:40:43 pm »
I try to create a simple piechart for odd and even numbers
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  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Chart1PieSeries1.AddXY(0, 10, 'A');
  4.   Chart1PieSeries1.AddXY(0, 23, 'B');
  5.   Chart1PieSeries1.AddXY(0,  2, 'C');
  6. 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  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Chart1PieSeries1.AddXY(0, 10, 'A');
  4.   Chart1PieSeries1.AddXY(0, 23, 'B');
  5.   Chart1PieSeries1.AddXY(0.25,  2, 'C');
  6.   Chart1PieSeries1.Exploded := true;
  7. end;
« Last Edit: December 22, 2021, 03:28:21 pm by wp »

winni

  • Hero Member
  • *****
  • Posts: 3125
Re: Pie chart?
« Reply #2 on: December 22, 2021, 01:41:11 pm »

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 324
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: Pie chart?
« Reply #3 on: December 22, 2021, 03:35:58 pm »
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?

Thanks in advance
Rob
Rob

wp

  • Hero Member
  • *****
  • Posts: 9921
Re: Pie chart?
« Reply #4 on: December 22, 2021, 04:31:05 pm »
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  [Select][+][-]
  1. uses
  2.   TAChartUtils, TARadialSeries, TALegend;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   Chart1.BottomAxis.Visible := false;
  7.   Chart1.LeftAxis.Visible := false;
  8.   Chart1.Frame.Visible := false;
  9.  
  10.   Chart1PieSeries1.AddXY(0.00, 10, 'A', RgbToColor(200, 180, 164));
  11.   Chart1PieSeries1.AddXY(0.00, 23, 'B', RgbToColor(92, 224, 172));
  12.   Chart1PieSeries1.AddXY(0.25,  2, 'C', clYellow);
  13.   Chart1PieSeries1.Exploded := true;
  14.   Chart1PieSeries1.Marks.Style := smsLabelPercent;     // requires TAChartUtils in "uses"
  15.   Chart1PieSeries1.MarkPositions := pmpInside;         // requires TARadialSeries in "uses"
  16.   Chart1PieSeries1.Depth := 20;
  17.  
  18.   Chart1.Legend.Visible := true;
  19.   Chart1PieSeries1.Legend.Multiplicity := lmPoint;    // requires TALegend to "uses"
  20.  
  21.   { if you have Lazarus 2.2RC2: activate the following lines to create an image like in the screenshot... }
  22.   //Chart1PieSeries1.InnerRadiusPercent := 25;
  23.   //Chart1PieSeries1.DepthBrightnessDelta := -50;
  24.   //Chart1PieSeries1.Orientation := poHorizontal;
  25. end; ;

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 324
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: Pie chart?
« Reply #5 on: December 22, 2021, 05:30:13 pm »
Fantastic wp :D You are a real Hero member  O:-)
Take care
Best regards
Rob
Rob

 

TinyPortal © 2005-2018