Page 1 of 1

Creating Pie Series In Code

Posted: Tue Dec 14, 2010 5:04 am
by 10554476
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.

Re: Creating Pie Series In Code

Posted: Wed Dec 15, 2010 4:55 am
by 10554476
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?

Re: Creating Pie Series In Code

Posted: Wed Dec 15, 2010 2:13 pm
by yeray
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.