Creating Pie Series In Code

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Nigel Tavendale
Newbie
Newbie
Posts: 2
Joined: Wed Oct 21, 2009 12:00 am

Creating Pie Series In Code

Post by Nigel Tavendale » Tue Dec 14, 2010 5:04 am

Trying to create a Pie Series connected to a TClientDataSet in code.

Code: Select all

procedure TMyForm.CreateChart;
var
  pieSeries: TPieSeries;
  i: Integer;
begin
  MyChart.View3D := FALSE;  //MyChart is a TeeChart On The From

  Screen.Cursor := crHourglass;
  try
    for i:= (MyChart.SeriesCount -1) downto 0 do
      MyChart.Series[i].Free;
    MyChart.SeriesList.Clear;

    pieSeries := TPieSeries.Create(MyChart);
    pieSeries.ParentChart := MyChart;
    pieSeries.DataSource := cdsChartData;  //cdsChartData is The TClientDataSet with the data I want to display
    pieSeries.PieValues.ValueSource := 'YearTotal';  //Field
    pieSeries.XLabelsSource := 'SliceLabel';  //Labels for the different values

    MyChart.SeriesList.Add(pieSeries);

    MyChart.Title.Text.Clear;
    MyChart.Title.Text.Add(''My Pie Series');

    GetData(); //Loads the Dataset by opening it
    pieSeries.CheckDataSource;
  finally
    Screen.Cursor := crDefault;
  end;
end;
There are two problems I am running into.

1) The Pie chart is not flat. Instead it is on an angle like it is trying to be a 3D chart. How do I stop it from doing this?

2) When I close the form (after the chart is loaded) I get an Exception (Invalid Pointer Operation). I think that has something to do with the Series and it's parent.

I combed the examples but they all seem to be examples with the series created at design time. I need to do it at run time.

Nigel Tavendale
Newbie
Newbie
Posts: 2
Joined: Wed Oct 21, 2009 12:00 am

Re: Creating Pie Series In Code

Post by Nigel Tavendale » Wed Dec 15, 2010 4:55 am

Removing the following

Code: Select all

    MyChart.SeriesList.Add(pieSeries);
gets rid of the Exception. Only now the pie chart is invisible. The lables are sill there, which makes me think the pie is too, just with a radius so small I can't see it on the screen.

Does anyone have any idea what I am doing wrong?

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Creating Pie Series In Code

Post by Yeray » Wed Dec 15, 2010 2:13 pm

Hi Nigel,

Simplifying your example (because I don't have your datasource) I have the following:

Code: Select all

uses series;

procedure TForm1.FormCreate(Sender: TObject);
var pieSeries: TPieSeries;
begin
  Chart1.View3D := FALSE;

  pieSeries := TPieSeries.Create(Chart1);
  pieSeries.ParentChart := Chart1;
  pieSeries.FillSampleValues();
end;
The code above in a new simple project with only a chart on the form seems to work fine here. So maybe you are doing something wrong at designtime or at GetData(). Or maybe there is something wrong with the data itself but I doubt it.
You can try adding resetting the settings of your chart to the defaults at the beginning of your CreateChart procedure. This would eliminate the suspect of some wrong setting at design time.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply