Metafile output for Multiple charts...

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Metafile output for Multiple charts...

Post by tbonejo » Mon Nov 20, 2006 1:45 pm

I am having trouble getting this bit of code to work.

Basically I have an array of Tcharts and I want to draw them to a metafile for saving and printing. However for some reason, its not working. I think I am not assigning the image to the metafile correctly. Can someone tell by my code what may be wrong?

Code: Select all

Var
i,topc,botc,tmpH,tmpW: Integer;
  
  i := 0;
  topc := 2;
  botc := 3;
  tmpW:=myChart[i].Width;
  tmpH:=myChart[i].Height;

  With TMetafile.Create do
    try
      for i := 0 to charts - 1 do
  begin
    if i = 0 then
    myChart[i].Draw(Canvas,Rect(0,0,tmpW,tmpH));
    if i = 1 then
    myChart[i].Draw(Canvas,Rect(0,tmpH,tmpW,topc*tmpH));
    if i >= 2 then
       begin
         myChart[i].Draw(Canvas,Rect(0,topc*tmpH,tmpW,botc*tmpH));
         Inc(topc);
         Inc(botc);
       end;
  end;
    SaveToFile(myfilename);
    finally
     free;
    end;

Thanks,

Tom
Tom

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 Nov 20, 2006 2:11 pm

Hi Tom,

You'll find an example of that at this question in our FAQ section.
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

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Mon Nov 20, 2006 5:42 pm

Im not sure if this would be my solution because I would be creating many metafiles rather than 1 file with many charts on it. My code as follows does not work although it works similiarly for bitmaps:

Code: Select all

With TMetafile.Create do
  try
  i := 0;
  topc := 2;
  botc := 3;
  tmpW:=myChart[i].Width;
  tmpH:=myChart[i].Height;
  for i := 0 to charts - 1 do
  begin
    if i = 0 then
    myChart[i].Draw(Canvas,Rect(0,0,tmpW,tmpH));
    if i = 1 then
    myChart[i].Draw(Canvas,Rect(0,tmpH,tmpW,topc*tmpH));
    if i >= 2 then
       begin
         myChart[i].Draw(Canvas,Rect(0,topc*tmpH,tmpW,botc*tmpH));
         Inc(topc);
         Inc(botc);
       end;
  end;
    resultspath := ConfigGrid.ColumnByName['ResultPath'].Rows[1];
    //SaveToFile(ResultsPath + '\' + run + ' ' + filename + '.wmf');
    SaveToFile('C:\test.wmf');
    finally
     free;
    end;
Tom

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

Post by Narcís » Tue Nov 21, 2006 9:13 am

Hi Tom,

Have you tried the StretchDraw method approach as in the FAQ example?
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

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Tue Nov 21, 2006 12:46 pm

Narcis,

Trying this hasnt worked either, I believe I am missing something small or something...

Code: Select all

procedure TForm1.AdvToolBarButton14Click(Sender: TObject);
Var
topc,botc: Integer;
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port   : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
tmpMeta: TMetaFile;
m: TMetafile;
c: TMetafileCanvas;
tmpW,tmpH: Integer;
resultspath: String;

begin
Printer.PrinterIndex := Printer.PrinterIndex;
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if hDMode <> 0 then begin
    pDMode := GlobalLock(hDMode);
    if pDMode <> nil then begin


      pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
      pDMode^.dmPaperSize := DMPAPER_11X17;
      GlobalUnlock(hDMode);
    end;
  end;


 
  m := TMetafile.Create;
  c := TMetafileCanvas.Create(m, 0);

  try
  i := 0;
  topc := 2;
  botc := 3;
  tmpW:=myChart[i].Width;
  tmpH:=myChart[i].Height;
  for i := 0 to charts - 1 do
  begin
    if i = 0 then
    myChart[i].Draw(c,Rect(0,0,tmpW,tmpH));
    if i = 1 then
    myChart[i].Draw(c,Rect(0,tmpH,tmpW,topc*tmpH));
    if i >= 2 then
       begin
    myChart[i].Draw(c,Rect(0,topc*tmpH,tmpW,botc*tmpH));
         Inc(topc);
         Inc(botc);
       end;
  end;

      Printer.BeginDoc;

        Printer.Canvas.StretchDraw(Rect(0,0,tmpW,botc*tmpH),m);

        Printer.EndDoc;



    resultspath := ConfigGrid.ColumnByName['ResultPath'].Rows[1];
    m.SaveToFile(ResultsPath + '\' + run + ' ' + filename + '.wmf');
    finally
     m.free;
    end;

end;
I cant even save the metafile. Its always blank as if Im not even drawing to the canvas.


Thanks,

Tom
Tom

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

Post by Narcís » Tue Nov 21, 2006 12:52 pm

Hi Tom,

Try initializing the metafile as in the StretchDraw example.
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

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Tue Nov 21, 2006 1:06 pm

Narcis,

Im not sure where in the stretch code it initializes?

Could you show me what I would need?


Thanks,

Tom
Tom

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

Post by Narcís » Tue Nov 21, 2006 1:30 pm

Hi Tom,

I meant initializing the metafile with the chart:

Code: Select all

  Meta := Chart1.TeeCreateMetafile(True,Rect(0,0,Chart1.Width,Chart1.Height));
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

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Tue Nov 21, 2006 1:32 pm

Narcis,

I did get it partially working, you have to destroy the canvas first then that transfers the image to the file for printing. Print works fine but the file itself only shows 3 out of 14 charts....go figure...the print quality from the metafile is not very good at all. I think I am gonna retry doing it all in the teepreviewpanel and try to adjust everything there. The only issue I had before was an annotation not printing correctly, which would require the metafile I guess, isnt there a spot to put it that makes it there without doing the metafile thing?


Thanks,

Tom
Tom

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Tue Nov 21, 2006 1:40 pm

narcis wrote:Hi Tom,

I meant initializing the metafile with the chart:

Code: Select all

  Meta := Chart1.TeeCreateMetafile(True,Rect(0,0,Chart1.Width,Chart1.Height));

So now my code would look like....

Code: Select all

for i := 0 to charts - 1 do
  begin
    if i = 0 then
    meta := mychart[i].TeeCreateMetafile(true,Rect(0,0,tmpW,tmpH));
    if i = 1 then
    meta:=mychart[i].TeeCreateMetafile(true,Rect(0,tmpH,tmpW,topc*tmpH));
    if i >= 2 then
       begin
    
    meta := mychart[i].TeeCreateMetafile(true,Rect(0,topc*tmpH,tmpW,botc*tmpH));
         Inc(topc);
         Inc(botc);
       end;
  end;

Printer.BeginDoc;
      Printer.Canvas.StretchDraw(Rect(0,0,6000,2000),meta);
      Printer.EndDoc;
That doesnt draw anything at all? Its all blank. Does the TeeCreateMetaFile do that?

Guess Im not sure how its suppose to work.


Thanks,

Tom
Tom

Marjan
Site Admin
Site Admin
Posts: 745
Joined: Fri Nov 07, 2003 5:00 am
Location: Slovenia
Contact:

Post by Marjan » Wed Nov 22, 2006 8:10 am

Hi, Tom.

TeeCreateMetafile creates a TMetaFileCanvas and then paints tChart on it. If you want to put several charts on a single metafile canvas, you'll have to slightly modify the code from TeeCreateMetafile. In the example below 5 charts are painted on the same metafile canvas.

Code: Select all

var m: TMetaFile;
  mCanvas : TMetafileCanvas;
  Rect: TRect;
  i,n: Integer;
  w,h: Integer;
begin
  n := 5; // draw 5 x charts in single column
  m := TMetaFile.Create;
  try
    w := Chart1.Width;
    h := n*Chart1.Height + n-1;
    m.Width := w;
    m.Height := h;
    m.Enhanced := True;
    mCanvas := TMetaFileCanvas.Create(m,0);
    try
      Rect := Chart1.ClientRect;
      for i := 0 to n - 1 do
      begin
        Chart1.DrawToMetaCanvas(mCanvas,Rect);
        Inc(Rect.Top,Chart1.Height);
        Inc(Rect.Bottom,Chart1.Height+1);
      end;
    finally
      mCanvas.Free;
    end;

    m.SaveToFile('c:\temp\test.emf');
  finally
    m.Free;
  end;
end;
Marjan Slatinek,
http://www.steema.com

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Wed Nov 22, 2006 1:17 pm

Marjan wrote:Hi, Tom.

TeeCreateMetafile creates a TMetaFileCanvas and then paints tChart on it. If you want to put several charts on a single metafile canvas, you'll have to slightly modify the code from TeeCreateMetafile. In the example below 5 charts are painted on the same metafile canvas.

Code: Select all

var m: TMetaFile;
  mCanvas : TMetafileCanvas;
  Rect: TRect;
  i,n: Integer;
  w,h: Integer;
begin
  n := 5; // draw 5 x charts in single column
  m := TMetaFile.Create;
  try
    w := Chart1.Width;
    h := n*Chart1.Height + n-1;
    m.Width := w;
    m.Height := h;
    m.Enhanced := True;
    mCanvas := TMetaFileCanvas.Create(m,0);
    try
      Rect := Chart1.ClientRect;
      for i := 0 to n - 1 do
      begin
        Chart1.DrawToMetaCanvas(mCanvas,Rect);
        Inc(Rect.Top,Chart1.Height);
        Inc(Rect.Bottom,Chart1.Height+1);
      end;
    finally
      mCanvas.Free;
    end;

    m.SaveToFile('c:\temp\test.emf');
  finally
    m.Free;
  end;
end;

Ok this works pretty good but I have a few questions on using it.

1. My x axis scale increments change from whats on the screen to the final printout. Can that be fixed?

2. Saving the image doesnt seem to show all the charts...as if the size of the image is different than the printed one. Do I have to set the height/width like I would if using bitmaps?

The stretch quality of the print isnt that great. It looks elongated, can I make it more defined like the TeePreviewPanel makes a printout look?


Thanks,

Tom
Tom

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Fri Dec 01, 2006 12:54 pm

Hi Tom,

would you be so kind to send me an example directly to pep@steema.com with which I can reproduce as is the problem here and try to find which could be the problem ?

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Fri Dec 01, 2006 1:18 pm

Pep wrote:Hi Tom,

would you be so kind to send me an example directly to pep@steema.com with which I can reproduce as is the problem here and try to find which could be the problem ?
Pep,

I got it all working the way I want except the print out for the Left Axes Title is all scrunched together overlapping each other. On the screen it all looks good, however in my print out the letters for the title are all overlapping one another. I see for the labels you can set the width and other things but I dont see any option like this for the title. If I could stretch my title a little I would be good to go.


Thanks,

Tom
Tom

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Tue Dec 05, 2006 12:23 pm

Hi Tom,

are you changing the Title font size ?
If so, have you used Font.Height instead of Font.Size for font size ?

Post Reply