Page 1 of 1

copy chart to excel

Posted: Mon Aug 04, 2008 3:27 pm
by 9342775
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

Posted: Tue Aug 05, 2008 8:15 am
by yeray
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;

Posted: Wed Aug 06, 2008 3:02 pm
by 9342775
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.

Posted: Mon Aug 25, 2008 10:06 am
by narcis
Hi mc,

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

Thanks in advance.