Page 1 of 1

Printing 2 charts on one page

Posted: Thu Mar 16, 2017 2:24 pm
by 16880166
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?

Re: Printing 2 charts on one page

Posted: Fri Mar 17, 2017 9:45 am
by yeray
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?

Re: Printing 2 charts on one page

Posted: Fri Mar 17, 2017 10:26 am
by yeray
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;

Re: Printing 2 charts on one page

Posted: Fri Mar 17, 2017 1:17 pm
by 16880166
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?

Re: Printing 2 charts on one page

Posted: Fri Mar 17, 2017 4:08 pm
by yeray
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;

Re: Printing 2 charts on one page

Posted: Fri Mar 17, 2017 4:11 pm
by 16880166
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.