Chart component related


Q: How can I change the type of a Series programmatically?


There is a global method located at chart.pas unit which allows you to do this.

Some Series types cannot be changed to some others (for example you cannot change from Line to Surface because the Surface needs 3 values per point XYZ while the Line has only Y or XY ).

Another important thing to remember is to always do a safe typecasting before accessing Series properties, because it might happen the Series type has changed, so the properties you access do not apply to the new series type, so you will get an access violation exception or unexpected problems:

if MySeries is TPieSeries then (MySeries as TPieSeries).RotationAngle:=123;

DELPHI CODE:

Var MySeries:TChartSeries;
MySeries:=Series1;
ChangeSeriesType( MySeries, TBarSeries );

CBUILDER CODE:

ChangeSeriesType( Series1, __classid(TBarSeries) );

Q: How to copy a Chart onto another Chart ?


A Chart can be copied onto another chart (for example in another Form) by doing this:

DELPHI CODE:

procedure TForm1.BitBtn1Click(Sender: TObject);
var t:Integer;
begin
  With Form2 do
  begin
    Chart2.FreeAllSeries;
    Chart2.Assign(Self.Chart1);
    { duplicate the Series and values }
    for t:=0 to Self.Chart1.SeriesCount-1 do
        CloneChartSeries(Self.Chart1[t]).ParentChart:=Chart2;
    Show;
  end;
end;

CBUILDER CODE:

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
    Form2->Chart2->FreeAllSeries();
    Form2->Chart2->Assign(this->Chart1);
     duplicate the Series and values
    for (int t=0;t<=this->Chart1->SeriesCount()-1;t++)
      CloneChartSeries(this->Chart1->Series[t])->ParentChart=Form2->Chart2;

    Form2->Show();

}

Q: How to clear the Chart BackImage background picture ?


You can clear BackImage with this code:

DELPHI CODE:

Chart1.BackImage.Assign(nil);

CBUILDER CODE:

Chart1->BackImage->Assign(NULL);

(Contributed by Manolis Afentakis)


Q: How to save Chart definitions to file and load Chart definitions from file?

Use the TeeStore.pas unit and the methods LoadChartFromFile... SaveChartToFile:

DELPHI CODE:

Uses TeeStore;

procedure TForm1.BtnSaveClick(Sender: TObject);
begin
  SaveChartToFile(Chart1,'c:\temp\test.tee');
end;

procedure TForm1.BtnLoadClick(Sender: TObject);
var tmpChart:TCustomChart;
begin
  Chart1.Free; //assuming Chart1 is already on the Form
  tmpChart:=TChart.Create(Self);
  LoadChartfromFile(tmpChart,'c:\temp\test.tee);
  Chart1 := tmpChart as TChart;
  Chart1.Parent:=Self;
end;