此 HotPDF 元件示例將 Delphi TeeChart 圖表匯出到 PDF 文件。該示例展示了互動式 VCL 圖表和可分發的 PDF 報告之間的實際橋樑:使用者可以在表單中配置圖表樣式,然後應用程式將選定的圖表狀態列印到 PDF 頁面。
此程式碼適用於儀表盤、工程報告、財務摘要以及任何已經使用 TeeChart 進行螢幕視覺化的 Delphi 應用程式。與手動重建圖表並使用 PDF 繪圖呼叫不同,此示例允許 TeeChart 將自身渲染到 HotPDF 頁面畫布上。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
unit ChartUnit; interface uses {$IF CompilerVersion >= 16} Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, VclTee.TeeGDIPlus, VclTee.TeEngine, VclTee.Series, VclTee.TeeProcs, VclTee.Chart, {$ELSE}Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, TeEngine, Series, TeeProcs, Chart, {$ENDIF} HPDFDoc; type TForm1 = class(TForm) RadioGroup1: TRadioGroup; DemoChart: TChart; BarSeries1: TBarSeries; BarSeries2: TBarSeries; BarSeries3: TBarSeries; Panel1: TPanel; Label1: TLabel; RadioGroup2: TRadioGroup; ComboBox1: TComboBox; Button1: TButton; procedure RadioGroup1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure RadioGroup2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.RadioGroup1Click(Sender: TObject); begin Case RadioGroup1.ItemIndex of 0: BarSeries1.MultiBar:=mbNone; 1: BarSeries1.MultiBar:=mbSide; 2: BarSeries1.MultiBar:=mbStacked; 3: BarSeries1.MultiBar:=mbStacked100; end; end; procedure TForm1.FormCreate(Sender: TObject); var H: Integer; begin DemoChart.View3D:=False; Randomize; {$IFDEF VER220} with BarSeries1 do for H:=1 to 12 do Add( Random(100),FormatSettings.ShortMonthNames[H],clTeeColor); with BarSeries2 do for H:=1 to 12 do Add( Random(100),FormatSettings.ShortMonthNames[H],clTeeColor); with BarSeries3 do for H:=1 to 12 do Add( Random(100),FormatSettings.ShortMonthNames[H],clTeeColor); {$ELSE} with BarSeries1 do for H:=1 to 12 do Add( Random(100),ShortMonthNames[H],clTeeColor); with BarSeries2 do for H:=1 to 12 do Add( Random(100),ShortMonthNames[H],clTeeColor); with BarSeries3 do for H:=1 to 12 do Add( Random(100),ShortMonthNames[H],clTeeColor); {$ENDIF} ComboBox1.Items.Clear; for H:=0 to DemoChart.SeriesCount-1 do ComboBox1.Items.Add(DemoChart.Series[H].Name); ComboBox1.ItemIndex:=0; end; procedure TForm1.RadioGroup2Click(Sender: TObject); begin With DemoChart.Series[ComboBox1.ItemIndex] as TBarSeries do BarStyle:=TBarStyle(RadioGroup2.Itemindex); end; procedure TForm1.Button1Click(Sender: TObject); var HotPDF: THotPDF; ChartRect: TRect; begin HotPDF := THotPDF.Create(nil); try case RadioGroup1.ItemIndex of 0: HotPDF.FileName := 'PDFChart-Behind.pdf'; 1: HotPDF.FileName := 'PDFChart-SideToSide.pdf'; 2: HotPDF.FileName := 'PDFChart-Stacked.pdf'; 3: HotPDF.FileName := 'PDFChart-Stacked100.pdf'; end; HotPDF.BeginDoc; ChartRect.Left := 0; ChartRect.Right := 800; ChartRect.Top := 0; ChartRect.Bottom := 600; DemoChart.PrintPartialCanvas( HotPDF.CurrentPage.Canvas, ChartRect ); HotPDF.EndDoc; finally HotPDF.Free; end; end; end. |
匯出流程
- 表單建立示例圖表資料,並允許使用者選擇柱狀圖佈局。
- 匯出按鈕建立 THotPDF,並根據圖表模式選擇輸出檔名。
- ChartRect 定義了 PDF 頁面上的目標繪圖區域。
- DemoChart.PrintPartialCanvas 將圖表渲染到 PDF 畫布上。
- EndDoc 完成 PDF 檔案的建立。
佈局建議
保持圖表的矩形與螢幕圖表區域的比例一致,並測試帶有長本地化文本的軸標籤。如果 PDF 是報告的一部分,請預留標題、時間戳、資料來源和註釋的空間,以便在檢視原始應用程式之外時,圖表仍然具有意義。