Page 1 of 1

Saving ChartStream to File

Posted: Thu Nov 19, 2009 9:08 pm
by 10545590
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.

Re: Saving ChartStream to File

Posted: Mon Nov 23, 2009 11:14 am
by yeray
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;

Re: Saving ChartStream to File

Posted: Mon Nov 23, 2009 11:58 am
by 10545590
Hi Yeray,

thx alot.