Saving charts to stream with extra properties

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Toreba
Newbie
Newbie
Posts: 29
Joined: Wed Sep 02, 2015 12:00 am

Saving charts to stream with extra properties

Post by Toreba » Tue Mar 01, 2016 2:44 pm

Hi,

I find it very useful to stream complex charts into a database blob field for use as templates. But I need to save extra properties for each series such as the gauging location and variate the data represents, the file from which the data comes, and the index of the series in that file. I can subclass the various classes of TChartSeries to add in these properties.

Using TeeChart Pro 2014.12.140923, the SaveChartToStream method recognises some, but not all, of my extra properties, and these appear in the saved BLOB field. But the SaveChartToStream method doesn't recognize my subclass name, so it's difficult to retrieve these extra properties. How can I make this work? How can I store and retrieve extra properties when streaming charts? Is a TagObject included in the stream? That would be very handy.

Thanks for any advice.

Toreba

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

Re: Saving charts to stream with extra properties

Post by Yeray » Tue Mar 01, 2016 4:31 pm

Hello,

Have you tried overriding DefineProperties and adding there the properties to serialize?
This works fine for me here:

Code: Select all

uses TeeStore;

type TMyBarSeries = class(TBarSeries)
  private
    procedure WriteBarName(Writer: TWriter);
    procedure ReadBarName(Reader: TReader);
    procedure WriteBarID(Writer: TWriter);
    procedure ReadBarID(Reader: TReader);

  protected
    IBarNameStored: boolean;
    IBarName: string;
    IBarID: Integer;

    procedure DefineProperties(Filer:TFiler); override;

  public
    property BarName: string read IBarName write IBarName stored IBarNameStored;
    property BarID: Integer read IBarID write IBarID default -1;
end;

procedure TMyBarSeries.DefineProperties(Filer: TFiler);
begin
  inherited;
  Filer.DefineProperty('BarName',ReadBarName,WriteBarName,True);
  Filer.DefineProperty('BarID',ReadBarID,WriteBarID,True);
end;

procedure TMyBarSeries.WriteBarName(Writer: TWriter);
begin
  Writer.WriteIdent(BarName);
end;

procedure TMyBarSeries.ReadBarName(Reader: TReader);
begin
  BarName:=Reader.ReadIdent;
end;

procedure TMyBarSeries.WriteBarID(Writer: TWriter);
begin
  Writer.WriteInteger(BarID);
end;

procedure TMyBarSeries.ReadBarID(Reader: TReader);
begin
  BarID:=Reader.ReadInteger;
end;

procedure TForm1.FormCreate(Sender: TObject);
var memStream1: TStream;
begin
  RegisterClass(TMyBarSeries);

  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  with Chart1.AddSeries(TMyBarSeries) as TMyBarSeries do
  begin
    BarID:=1;
    BarName:='MyBar #' + IntToStr(BarID);
    FillSampleValues;
    ColorEachPoint:=true;
  end;

  memStream1:=TMemoryStream.Create;
  SaveChartToStream(Chart1, memStream1);

  memStream1.Position:=0;
  LoadChartFromStream(Chart2, memStream1);

  Chart2.Top:=Chart1.Top+Chart1.Height+10;

  Memo1.Clear;
  Memo1.Lines.Add(IntToStr((Chart1[0] as TMyBarSeries).BarID));
  Memo1.Lines.Add((Chart1[0] as TMyBarSeries).BarName);

  Memo1.Lines.Add(IntToStr((Chart2[0] as TMyBarSeries).BarID));
  Memo1.Lines.Add((Chart2[0] as TMyBarSeries).BarName);
end;
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

Toreba
Newbie
Newbie
Posts: 29
Joined: Wed Sep 02, 2015 12:00 am

Re: Saving charts to stream with extra properties

Post by Toreba » Wed Mar 02, 2016 5:13 pm

Yeray,

Wow, I didn't know I could do any of that, and it works!

Though I notice that if my extra properties are published rather than public, then they appear in the streamed version anyway, it seems, but that default mechanism can't cope with enumerated types. So if I also add them using the method you've shown me they appear twice in the streamed version. I've downgraded from published to public and now have proper control of data type using the methods passed to TFiler.DefineProperty

If I'm using many different series types, then all this coding gets very repetitive. I presume the only way to avoid this is to get the source code for TChartSeries, and add my properties in there.

Thanks very much for your help.

Toreba

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

Re: Saving charts to stream with extra properties

Post by Yeray » Thu Mar 03, 2016 9:13 am

Hello Toreba,

I'm not sure your only option is to modify the TeeChart sources.
I'd suggest you to read the documentation here.
Toreba wrote:but that default mechanism can't cope with enumerated types
Since you have the TeeChart sources, you'll see we use published properties to store custom enums, ie the MultiBar property in TCustomBarSeries.
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