Printing 2 charts on one page

TeeChart FireMonkey (Windows,OSX,iOS & Android) for Embarcadero RAD Studio, Delphi and C++ Builder (XE5+)
Post Reply
realsol
Newbie
Newbie
Posts: 70
Joined: Mon Feb 27, 2017 12:00 am

Printing 2 charts on one page

Post by realsol » Thu Mar 16, 2017 2:24 pm

Regarding the layout, I see two options:
- You can create two charts. One on the top with two TBarSeries, and one on the bottom with a TPieSeries.
- You can create a chart with a TSubChartTool. The main chart could contain the two TBarSeries and the Chart in the SubChartTool could contain the TPieSeries.
I have the Std Version and have used two charts on the screen for the user. 1 bar chart with 2 series and a pie chart with 1 series. Is there a way to have both charts print on one page?

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

Re: Printing 2 charts on one page

Post by Yeray » Fri Mar 17, 2017 9:45 am

Hello,

Have you tried to follow the "Print previewing several Charts on one page" instructions in the "Tutorial 14 - Printing Charts" in the online documentation?
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

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

Re: Printing 2 charts on one page

Post by Yeray » Fri Mar 17, 2017 10:26 am

Yeray wrote:Hello,

Have you tried to follow the "Print previewing several Charts on one page" instructions in the "Tutorial 14 - Printing Charts" in the online documentation?
Not as easy as that, but extracting part of the code from the Pro unit "PreviewPanel":

Code: Select all

uses FMX.Printer, FMXTee.Canvas;

procedure TForm1.Button1Click(Sender: TObject);
var PaperRect: TRect;
    PaperColor: TColor;

  Procedure CalcPaperRectangles;
  var
    PrinterWidth  : TCoordinate;
    PrinterHeight : TCoordinate;

    procedure SetScreenSize;
    var P : TPoint;
    begin
      P:=TeeGetScreenSize;
      PrinterWidth:=P.X;
      PrinterHeight:=P.Y;
    end;

  Const Margin=5;
  Var tmpClientWidth,
      tmpClientHeight,
      tmpWidth,
      tmpHeight : TCoordinate;
  begin
    Printer.Orientation:=TPrinterOrientation.poPortrait;

    {$IFDEF ANDROID}
    SetScreenSize;
    {$ELSE}
    try
      PrinterWidth :=Printer.PageWidth;
      PrinterHeight:=Printer.PageHeight;
    except
      on EPrinter do
         SetScreenSize;
    end;
    {$ENDIF}

    tmpClientWidth:=Width;
    tmpClientHeight:=Height;

    if (tmpClientWidth*PrinterHeight)>(tmpClientHeight*PrinterWidth) then
    Begin
      tmpHeight:=tmpClientHeight-Round(tmpClientHeight/Margin);
      PaperRect.Top:=Round(tmpClientHeight/(2*Margin));
      PaperRect.Bottom:=PaperRect.Top+tmpHeight;

      if PrinterHeight>0 then
        tmpWidth:=(tmpHeight*PrinterWidth / PrinterHeight)
      else
        tmpWidth:=tmpClientWidth;

      PaperRect.Left:=(tmpClientWidth-tmpWidth)*0.5;
      PaperRect.Right:=PaperRect.Left+tmpWidth;
    end
    else
    Begin
      tmpWidth:=tmpClientWidth-Round(tmpClientWidth/Margin);
      PaperRect.Left:=Round(tmpClientWidth/(2*Margin));
      PaperRect.Right:=PaperRect.Left+tmpWidth;

      if PrinterWidth>0 then
        tmpHeight:=(tmpWidth*PrinterHeight / PrinterWidth)
      else
        tmpHeight:=tmpClientHeight;

      PaperRect.Top:=(tmpClientHeight-tmpHeight) *0.5;
      PaperRect.Bottom:=PaperRect.Top+tmpHeight;
    end;
  end;

  Function CalcImagePrintMargins(const APanel:TCustomTeePanel):TRect;
  var PaperWidth  : Integer;
      PaperHeight : Integer;
  begin
    RectSize(PaperRect,PaperWidth,PaperHeight);

    if Assigned(APanel) then
    begin
      with APanel do
         if PrintProportional then
            PrintMargins:=CalcProportionalMargins;

      result:=APanel.PrintMargins
    end
    else result:=TeeRect(15,15,15,15);

    With result do
    begin
      Left  :=PaperRect.Left  +(Left  *PaperWidth *0.01);
      Right :=PaperRect.Right -(Right *PaperWidth *0.01);
      Top   :=PaperRect.Top   +(Top   *PaperHeight *0.01);
      Bottom:=PaperRect.Bottom-(Bottom*PaperHeight *0.01);
    end;
  end;

  Function GetPrintingBitmap(const APanel:TCustomTeePanel):TBitmap;
  var tmpR      : TRect;
      WinWidth  : Integer;
      WinHeight : Integer;
      tmpW      : Integer;
      tmpH      : Integer;
      tmpWidth  : TCoordinate;
      tmpHeight : TCoordinate;
  begin
    APanel.Printing:=True;
    try
      With APanel.GetRectangle do
      begin
        tmpWidth:=Right-Left;
        tmpHeight:=Bottom-Top;
      end;

      tmpR:=CalcImagePrintMargins(APanel);
      APanel.CalcMetaBounds(tmpR,TeeRect(tmpWidth,tmpHeight),WinWidth,WinHeight,tmpW,tmpH);
      result:=APanel.TeeCreateBitmap(PaperColor,TeeRect(WinWidth,WinHeight),TeePixelFormat);
    finally
      APanel.Printing:=False;
    end;
  end;

  Procedure SendAsBitmap(const APanel:TCustomTeePanel; const ACanvas:TCanvas; Const R:TRect);
  var tmpBitmap : TBitmap;
  begin
    tmpBitmap:=GetPrintingBitmap(APanel);
    try
      ACanvas.DrawBitmap(tmpBitmap, TeeRect(tmpBitmap.Width,tmpBitmap.Height),
                         R,1);
    finally
      tmpBitmap.Free;
    end;
  end;

begin
  PaperColor:=TAlphaColors.White;
  CalcPaperRectangles;

  Printer.BeginDoc;
  try
    SendAsBitmap(Chart1,Printer.Canvas,Chart1.ChartPrintRect);
    SendAsBitmap(Chart2,Printer.Canvas,Chart2.ChartPrintRect);

    Printer.EndDoc;

    except
    on Exception do
    begin
      Printer.Abort;

      if Printer.Printing then
         Printer.EndDoc;

      Raise;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.PrintProportional:=False;
  Chart2.PrintProportional:=False;

  Chart1.PrintMargins:=Rect(2,2,2,50);
  Chart2.PrintMargins:=Rect(2,50,2,2);
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

realsol
Newbie
Newbie
Posts: 70
Joined: Mon Feb 27, 2017 12:00 am

Re: Printing 2 charts on one page

Post by realsol » Fri Mar 17, 2017 1:17 pm

Thanks for this code. But my double chart will show only one chart when either chart is clicked or drilled-down.
  • If a user clicks on the bar chart, I am going to set the Bar chart and the splitter visible := false and use only the pie chart.
  • In some cases if a user clicks on the Pie chart I will do the same thing, leaving only the pie chart
procedure TForm1.FormCreate(Sender: TObject);
begin
Chart1.PrintProportional:=False;
Chart2.PrintProportional:=False;

Chart1.PrintMargins:=Rect(2,2,2,50);
Chart2.PrintMargins:=Rect(2,50,2,2);
end;
What would I need to alter when printing just the pie chart?

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

Re: Printing 2 charts on one page

Post by Yeray » Fri Mar 17, 2017 4:08 pm

Hello
realsol wrote:What would I need to alter when printing just the pie chart?
To print only one chart you can just:

Code: Select all

Chart2.Print;
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

realsol
Newbie
Newbie
Posts: 70
Joined: Mon Feb 27, 2017 12:00 am

Re: Printing 2 charts on one page

Post by realsol » Fri Mar 17, 2017 4:11 pm

I knew it was a simple answer. I'll move the code from the OnCreate to my printer button and use it only if I am printing 2 reports and use Chart.Print when printing one.

Thanks.

Post Reply