Page 1 of 1

Saving charts to stream with extra properties

Posted: Tue Mar 01, 2016 2:44 pm
by 16575285
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

Re: Saving charts to stream with extra properties

Posted: Tue Mar 01, 2016 4:31 pm
by yeray
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;

Re: Saving charts to stream with extra properties

Posted: Wed Mar 02, 2016 5:13 pm
by 16575285
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

Re: Saving charts to stream with extra properties

Posted: Thu Mar 03, 2016 9:13 am
by yeray
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.