跳轉到

圖表報告範例(Pro)

需求套件nextpdf/pro 難度:進階

此範例使用 nextpdf/pro 的原生圖表模組,圖表直接渲染為 PDF 向量圖形,無需 Chrome 或任何瀏覽器依賴。

完整程式碼

<?php

declare(strict_types=1);

use NextPDF\Core\Document;
use NextPDF\Core\ValueObjects\PageSize;
use NextPDF\Core\ValueObjects\Margin;
use NextPDF\Core\ValueObjects\Position;
use NextPDF\Core\ValueObjects\Dimension;
use NextPDF\Core\ValueObjects\Color;
use NextPDF\Pro\Charts\ChartRenderer;
use NextPDF\Pro\Charts\BarChart;
use NextPDF\Pro\Charts\LineChart;
use NextPDF\Pro\Charts\PieChart;
use NextPDF\Pro\Charts\ValueObjects\ChartDataset;
use NextPDF\Pro\Charts\ValueObjects\ChartOptions;
use NextPDF\Pro\Charts\ValueObjects\Axis;

// ─── 文件初始化 ───────────────────────────────────────────────
$document = Document::createStandalone(
    pageSize: PageSize::A4,
    margin:   Margin::symmetric(vertical: 20.0, horizontal: 20.0),
);

$page    = $document->pages()->add();
$text    = $document->text();
$drawing = $document->drawing();
$charts  = ChartRenderer::forDocument($document);

// ─── 報告標題 ─────────────────────────────────────────────────
$drawing->rectangle(
    x: 20.0, y: 20.0, width: 170.0, height: 18.0,
    fill: Color::fromHex('#1E3A8A'), cornerRadius: 2.0,
);
$text->write(
    text:      '2024 年度業績分析報告',
    position:  Position::at(x: 105.0, y: 26.0),
    fontSize:  14.0,
    fontName:  'NotoSansCJKtc-Bold',
    color:     Color::fromHex('#FFFFFF'),
    alignment: 'center',
    maxWidth:  170.0,
);

// ─── 長條圖:月度營收 ─────────────────────────────────────────
$text->write(
    text:     '月度營收趨勢(NT$)',
    position: Position::at(x: 20.0, y: 48.0),
    fontSize: 10.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);

$barChart = BarChart::create(
    labels:   ['一月', '二月', '三月', '四月', '五月', '六月',
               '七月', '八月', '九月', '十月', '十一月', '十二月'],
    datasets: [
        ChartDataset::create(
            label:           '2024',
            data:            [3.2, 2.8, 4.1, 3.9, 4.8, 5.2, 4.7, 5.5, 4.9, 3.8, 4.2, 4.6],
            backgroundColor: Color::fromHex('#1E3A8A'),
        ),
        ChartDataset::create(
            label:           '2023',
            data:            [2.5, 2.1, 3.2, 3.0, 3.8, 4.0, 3.6, 4.2, 3.7, 3.0, 3.3, 3.5],
            backgroundColor: Color::fromHex('#BFDBFE'),
        ),
    ],
    options:  ChartOptions::create()
        ->withXAxis(Axis::create()->withTitle('月份'))
        ->withYAxis(Axis::create()->withTitle('百萬 NT$')->withMin(0.0))
        ->withLegend(show: true, position: 'top')
        ->withGridLines(color: Color::fromHex('#F3F4F6'), width: 0.3),
);

$charts->render(
    chart:     $barChart,
    position:  Position::at(x: 20.0, y: 54.0),
    dimension: Dimension::create(width: 170.0, height: 65.0),
);

// ─── 折線圖:客戶成長 ─────────────────────────────────────────
$text->write(
    text:     '累計客戶數成長',
    position: Position::at(x: 20.0, y: 128.0),
    fontSize: 10.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);

$lineChart = LineChart::create(
    labels:   ['Q1', 'Q2', 'Q3', 'Q4'],
    datasets: [
        ChartDataset::create(
            label:           '企業客戶',
            data:            [45, 62, 78, 98],
            borderColor:     Color::fromHex('#1E3A8A'),
            backgroundColor: Color::fromHex('#1E3A8A')->withAlpha(0.1),
            fill:            true,
            tension:         0.4,
        ),
        ChartDataset::create(
            label:           '個人客戶',
            data:            [120, 185, 230, 287],
            borderColor:     Color::fromHex('#D97706'),
            backgroundColor: Color::fromHex('#D97706')->withAlpha(0.1),
            fill:            true,
            tension:         0.4,
        ),
    ],
    options:  ChartOptions::create()
        ->withYAxis(Axis::create()->withTitle('客戶數')->withMin(0.0))
        ->withLegend(show: true, position: 'top')
        ->withDataLabels(show: true, fontSize: 7.0),
);

$charts->render(
    chart:     $lineChart,
    position:  Position::at(x: 20.0, y: 134.0),
    dimension: Dimension::create(width: 105.0, height: 60.0),
);

// ─── 圓餅圖:營收來源 ─────────────────────────────────────────
$text->write(
    text:     '營收來源佔比',
    position: Position::at(x: 130.0, y: 128.0),
    fontSize: 10.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);

$pieChart = PieChart::create(
    labels:   ['Enterprise', 'Pro', 'Core', '服務費'],
    data:     [45.2, 31.8, 15.6, 7.4],
    colors:   [
        Color::fromHex('#1E1B4B'),
        Color::fromHex('#D97706'),
        Color::fromHex('#1E3A8A'),
        Color::fromHex('#6B7280'),
    ],
    options: ChartOptions::create()
        ->withLegend(show: true, position: 'bottom')
        ->withDataLabels(show: true, format: 'percent'),
);

$charts->render(
    chart:     $pieChart,
    position:  Position::at(x: 128.0, y: 134.0),
    dimension: Dimension::square(size: 62.0),
);

// ─── 分頁 + 說明文字 ─────────────────────────────────────────
$page2 = $document->pages()->add();

$text->write(
    text:     '數據摘要與分析說明',
    position: Position::at(x: 20.0, y: 35.0),
    fontSize: 12.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);

// ... 第二頁的分析文字 ...

// ─── 輸出 ────────────────────────────────────────────────────
$document->save('/output/chart-report-2024.pdf');

支援的圖表類型

圖表類型 類別 適用場景
長條圖 BarChart 分類比較
水平長條圖 HorizontalBarChart 排名列表
折線圖 LineChart 時間序列趨勢
面積圖 AreaChart 累積趨勢
圓餅圖 PieChart 佔比組成
環形圖 DoughnutChart 佔比(中央留白)
散佈圖 ScatterChart 相關性分析
雷達圖 RadarChart 多維度比較
瀑布圖 WaterfallChart 財務分析

延伸閱讀