copy chart to excel

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
mc
Newbie
Newbie
Posts: 26
Joined: Fri Jul 15, 2005 4:00 am

copy chart to excel

Post by mc » Mon Aug 04, 2008 3:27 pm

I need to create an enlarged copy of a chart and copy it into an excel sheet.
If I do this:
TeeSaveToJPEGFile( Chart1,
'c:\Temp.jpg',
False, // not gray scale
jpBestQuality,
95, // compression
Round(513*1.25),
Round(497*1.25) );

The result is that the chart that ends up in the excel sheet does not have very good resolution.

However, if in the app where the chart is displayed to the user, I stretch the chart manually and then do "TeeSaveToJPEG" and then "insert pic" in excel, I get a much better image.

How can I do this in code please?

Thanks

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

Post by Yeray » Tue Aug 05, 2008 8:15 am

Hi mc,

Could you take a look at this example and see if it's useful at your end?

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
  Function GetChartJPEG(AChart:TCustomChart):TJPEGImage;
  var tmpBitmap:TBitmap;
      tmpChart: TChart;
      i:integer;
  begin
    result:=TJPEGImage.Create;   { <-- create a TJPEGImage }
    tmpBitmap:=TBitmap.Create;   { <-- create a temporary TBitmap }
    tmpChart := TChart.Create(AChart.Parent);

    try
      tmpChart.Assign(AChart);

      for i := 0 to AChart.SeriesCount - 1 do
        CloneChartSeries(AChart.Series[i]).ParentChart := tmpChart;

      tmpChart.Width := trunc(AChart.Width*1.25);
      tmpChart.Height := trunc(AChart.Height*1.25);

      tmpBitmap.Width :=tmpChart.Width;   { <-- set the bitmap dimensions }
      tmpBitmap.Height:=tmpChart.Height;
      { draw the Chart on the temporary Bitmap... }
      tmpChart.Draw(tmpBitmap.Canvas,Rect(0,0,tmpBitmap.Width,tmpBitmap.Height));
      { set the desired JPEG options... }
      With result do
      begin
        GrayScale            :=False;
        ProgressiveEncoding  :=True;
        CompressionQuality   :=95;  // % 0 - 100
        PixelFormat          :=jf24bit;  // or jf8bit
        ProgressiveDisplay   :=True;
        Performance          :=jpBestQuality;  // or jpBestSpeed
        Scale                :=jsFullSize;  // or jsHalf, jsQuarter, jsEighth
        Smoothing            :=True;
        { Copy the temporary Bitmap onto the JPEG image... }
        Assign(tmpBitmap);
      end;
    finally
      tmpBitmap.Free;  { <-- free the temporary Bitmap }
      tmpChart.Free;
    end;
  end;

begin
  With GetChartJPEG(Chart1) do
  try
    SaveToFile('C:\tmp\myJPEGChart.jpg');    { <-- save the JPEG to disk }
  finally
    Free;  { <-- free the temporary JPEG object }
  end;
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

mc
Newbie
Newbie
Posts: 26
Joined: Fri Jul 15, 2005 4:00 am

Post by mc » Wed Aug 06, 2008 3:02 pm

No, this works no better than what I was doing before - in addition to which this way I have to re-draw all the annotations.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Aug 25, 2008 10:06 am

Hi mc,

Have you tried increasing the jpg file size befire exporting it so that you have better resolution?

Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply