Recent

Author Topic: Updating Pie Data  (Read 3107 times)

ccanute

  • Newbie
  • Posts: 1
Updating Pie Data
« on: May 27, 2018, 04:05:39 am »
I searched the web for a few hours then read every forum entry but did not see a solution...
I would like to update pie data programmically.  What I am doing does not show up.  Using Lazarus 1.8.4 and I am a newbie to Lazarus but not to programming.


  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Chart1: TChart;
    ChartToolset1: TChartToolset;
    ChartToolset1ZoomMouseWheelTool1: TZoomMouseWheelTool;
    Prov1: TPieSeries;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure Chart1Resize(Sender: TObject);
    procedure FormCreate(Sender: TObject);


  private

  public

  end;

var
  Form1: TForm1;

  Values: array[1..5] of double = (22, 20, 20, 5, 12);
  Colours: array[1..5] of TColor = (clRed, clYellow, clFuchsia, clGreen, clBlue);
  Labels: array[1..5] of string = ('', '', '', '', '');

implementation

{$R *.lfm}
uses
  Math, TAChartUtils, TATextElements, TATypes;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var

  Count: integer;
begin
  Form1.Caption := 'Election Game';
  Prov1 := TPieSeries.Create(Chart1);
  Chart1.Title.Text.Strings[0] := 'Province 1';
  Chart1.Title.Font.Size := 10;;
  Chart1.Title.Visible := true;
  Prov1.Marks.Style := TSeriesMarksStyle(smsNone);
  Prov1.Marks.Distance:= 5;
  for Count := 1 to 5 do
    Prov1.AddPie(Values[count], Labels[count], Colours[Count]);
  Chart1.AddSeries(Prov1);
  Chart1.AxisVisible := false;
end;

procedure TForm1.Chart1Resize(Sender: TObject);
begin

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Count: integer;
begin
  Values[5] := Values[5]+2;
  Values[1] := Values[1]-2;
  Prov1.SetXValue(4,Values[5]);
  Prov1.SetXValue(0,Values[5]);
end;   

What am I missing here?

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Updating Pie Data
« Reply #1 on: May 27, 2018, 11:00:14 am »
First of all: if you post code within your text please enclose it with [code=Pascal] and [/code] tags - this will format the text as pascal code and make it more readable. And, since you seem to have a test project, pack the .pas, .lfm, .lpi and .lpr files into a common .zip and upload it here under "Attachements and other options" - a compilable project helps me in understanding and verifying your issue.

Back to your question. The pie series interprets the x,y data provided in a special way:
  • The x value is the distance of the pie from the center (in units of the pie radius) if the pie series' Exploded is set to true. Otherwise x is ignored
  • The y value is the "size" of the pie (to be more precise: the pie angle after normalizing the sum of all pie y values to the full circle at 360°)
If you add data points by calling the pie series' AddPie the provided values go into the y channel. But your button click routine puts the modified values into the x channel. And x is ignored unless Exploded is true. So, if you use "Prov1.SetYValue(...)" the chart should work correctly.

If you want to use the Exploded feature don't add data by AddPie because it increments x with each value added. Instead, use AddXY where you can specify the x value explicitely, e.g. if you want to "explode" the first pie by one quarter of the pie radius do this:
Code: Pascal  [Select][+][-]
  1. var
  2.   x: Double;
  3. ...
  4.   for Count := 1 to 5 do begin
  5.     if Count=1 then x := 0.25 else x := 0;   // units are fractions of the pie radius
  6.     Prov1.AddXY(x, values[Count], Labels[Count], Colours[Count]);  
  7.   end;
  8.   Prov1.Exploded := true;

Another remark: Since the identifier smsNone has type TSeriesMarksStyle there is no need to cast it explicitely to this type:
Code: Pascal  [Select][+][-]
  1.   Prov1.Marks.Style := smsNone; // instead of: TSeriesMarksStyle(smsNone);


 

TinyPortal © 2005-2018