Page 1 of 1

Metafile output for Multiple charts...

Posted: Mon Nov 20, 2006 1:45 pm
by 9242408
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

Posted: Mon Nov 20, 2006 2:11 pm
by narcis
Hi Tom,

You'll find an example of that at this question in our FAQ section.

Posted: Mon Nov 20, 2006 5:42 pm
by 9242408
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;

Posted: Tue Nov 21, 2006 9:13 am
by narcis
Hi Tom,

Have you tried the StretchDraw method approach as in the FAQ example?

Posted: Tue Nov 21, 2006 12:46 pm
by 9242408
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

Posted: Tue Nov 21, 2006 12:52 pm
by narcis
Hi Tom,

Try initializing the metafile as in the StretchDraw example.

Posted: Tue Nov 21, 2006 1:06 pm
by 9242408
Narcis,

Im not sure where in the stretch code it initializes?

Could you show me what I would need?


Thanks,

Tom

Posted: Tue Nov 21, 2006 1:30 pm
by narcis
Hi Tom,

I meant initializing the metafile with the chart:

Code: Select all

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

Posted: Tue Nov 21, 2006 1:32 pm
by 9242408
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

Posted: Tue Nov 21, 2006 1:40 pm
by 9242408
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

Posted: Wed Nov 22, 2006 8:10 am
by Marjan
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;

Posted: Wed Nov 22, 2006 1:17 pm
by 9242408
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

Posted: Fri Dec 01, 2006 12:54 pm
by Pep
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 ?

Posted: Fri Dec 01, 2006 1:18 pm
by 9242408
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

Posted: Tue Dec 05, 2006 12:23 pm
by Pep
Hi Tom,

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