Saving ChartStream to File

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
moelski
Newbie
Newbie
Posts: 92
Joined: Tue Jun 19, 2007 12:00 am
Contact:

Saving ChartStream to File

Post by moelski » Thu Nov 19, 2009 9:08 pm

Hi !

I use SaveChartToStream to store a Chart to a stream :) Well done sentence :D

Ok now I want to save this Stream to a file. This could be done by

Code: Select all

<STREAM>.SaveToFile(FileName);
Well this is for sure not a valid methode for storing a chart into a file - but it still works!
I loaded the stored stream into TeeChart Office and it works.
There is also no problem to convert such a stream file with "ConvertTeeFileToText" to a Tee Text file.

So what do you say about this? Is this practicable? Normally I use this code to store the chart within a Thread:

Code: Select all

  ChartStream.Position := 0;
  LoadChartFromStream(TCustomChart(ChartCopy), ChartStream);
  ChartStream.Free;
    SaveChartToFile(TCustomChart(ChartCopy),
                    FileName,
                    True,         // Include Data
                    False);       // Text Format ?
  ChartCopy.Free;
But this code needs a lot of space in the memory. First of all the real chart, the Stream Copy and finally the ChartCopy which is only for storing into a file.

If I use the workaround from above I save 1/3 of memory consumption.

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

Re: Saving ChartStream to File

Post by Yeray » Mon Nov 23, 2009 11:14 am

Hi Dominik,

Here is how I would do this function:

Code: Select all

procedure TForm1.SaveStreamToTeeFile(s: TMemoryStream; FileName: string);
var tmpFileStream: TFileStream;
begin
  tmpFileStream:=TFileStream.Create(TeeCheckExtension(FileName),fmCreate);

  try
    s.Position:=0;
    tmpFileStream.CopyFrom(s,s.Size);
  finally
    tmpFileStream.Free;
  end;
end;
And here is a complete example of how you could use it:

Code: Select all

uses series, teestore;

var MyStream: TMemoryStream;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.AddSeries(TBarSeries.Create(self));
  Chart1.AddSeries(TBarSeries.Create(self));

  Chart1[0].FillSampleValues(6);
  Chart1[1].FillSampleValues(6);

  Chart1.Draw;
  MyStream:=TMemoryStream.Create;
  SaveChartToStream(Chart1, MyStream, true, true);
end;

procedure TForm1.SaveStreamToTeeFile(s: TMemoryStream; FileName: string);
var tmpFileStream: TFileStream;
begin
  tmpFileStream:=TFileStream.Create(TeeCheckExtension(FileName),fmCreate);

  try
    s.Position:=0;
    tmpFileStream.CopyFrom(s,s.Size);
  finally
    tmpFileStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SaveStreamToTeeFile(MyStream, 'C:\tmp\test.tee');
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

moelski
Newbie
Newbie
Posts: 92
Joined: Tue Jun 19, 2007 12:00 am
Contact:

Re: Saving ChartStream to File

Post by moelski » Mon Nov 23, 2009 11:58 am

Hi Yeray,

thx alot.

Post Reply