首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 圖表生成庫 Libchart 完整使用指南:柱狀、折線、餅圖與中文亂碼解決方案

     編輯:PHP 相關知識

前言

Libchart 是純 PHP 實現的圖表生成庫,無需外部擴展即可輸出 PNG 格式的柱狀圖、折線圖、餅圖與橫向柱狀圖。本文將示範如何封裝通用繪圖類、解決中文亂碼(標題、軸標籤)、字體替換與實戰調用全流程。

一、快速上手:封裝通用繪圖類

<?php
require_once 'libchart/classes/libchart.php';

class ChartRenderer {
    private $chart;
    private int $style; // 1:垂直柱 2:折線 3:餅圖 4:橫向柱

    public function __construct(int $style = 1, int $width = 500, int $height = 250) {
        $this->style = $style;
        switch ($style) {
            case 1: $this->chart = new VerticalBarChart($width, $height); break;
            case 2: $this->chart = new LineChart($width, $height); break;
            case 3: $this->chart = new PieChart($width, $height); break;
            default: $this->chart = new HorizontalBarChart($width, $height);
        }
    }

    public function render(object $dataObj): void {
        $dataSet = null;
        $this->chart->setTitle($dataObj->title);

        switch ($this->style) {
            case 1: // 垂直柱狀圖
            case 4: // 橫向柱狀圖
                $dataSet = new XYDataSet();
                foreach ($dataObj->dataArray as $key => $val) {
                    $dataSet->addPoint(new Point($key, $val));
                }
                break;
            case 2: // 折線圖(支援多數據系列)
                $dataSet = new XYSeriesDataSet();
                $i = 0;
                foreach ($dataObj->dataArray as $seriesName => $seriesData) {
                    $serie = new XYDataSet();
                    foreach ($seriesData as $k => $v) {
                        $serie->addPoint(new Point($k, $v));
                    }
                    $dataSet->addSerie($seriesName, $serie);
                    $i++;
                }
                break;
            case 3: // 餅圖
                $dataSet = new XYDataSet();
                foreach ($dataObj->dataArray as $key => $val) {
                    $dataSet->addPoint(new Point("{$key} ({$val})", $val));
                }
                break;
        }

        $this->chart->setDataSet($dataSet);
        $this->chart->render();
    }
}

// 使用範例
$data = new stdClass();
$data->title = "年度銷售統計";
$data->dataArray = [
    "2020" => 120, "2021" => 150, "2022" => 180, "2023" => 210
];

$renderer = new ChartRenderer(1); // 垂直柱狀圖
$renderer->render($data);

二、中文亂碼完整解決方案

Libchart 內建字體不支援中文,需三步驟修正:

1. 數據與標題編碼轉換(即時方案)

$str = '二月';
$str = mb_convert_encoding($str, "HTML-ENTITIES", "UTF-8");
$dataSet->addPoint(new Point($str, 120));

$chart->setTitle(mb_convert_encoding("統計圖", "HTML-ENTITIES", "UTF-8"));

2. 修改 Chart.php 強制標題編碼(永久方案)

檔案:libchart/classes/view/chart/Chart.php

public function setTitle($title) {
    $title = mb_convert_encoding($title, "HTML-ENTITIES", "UTF-8");
    $this->plot->setTitle($title);
}

3. 替換中文字體檔(徹底解決軸標籤亂碼)

檔案:libchart/classes/view/text.php

public function __construct() {
    $baseDir = dirname(__FILE__) . "/../../../";
    // 下載中文字體(如思源黑體、微軟正黑體)放入 fonts 目錄
    $this->fontCondensed = $baseDir . "fonts/SourceHanSansTC-Regular.otf";
    $this->fontCondensedBold = $baseDir . "fonts/SourceHanSansTC-Bold.otf";
}

注意:字體檔需為 TTF/OTF 格式,確保 Web 伺服器可讀取權限。

三、實戰範例:多系列折線圖

$dataObj = new stdClass();
$dataObj->title = "雙產品銷售趨勢對比";
$dataObj->dataArray = [
    "產品A" => ["2021" => 100, "2022" => 150, "2023" => 200],
    "產品B" => ["2021" => 80,  "2022" => 120, "2023" => 180],
];

$renderer = new ChartRenderer(2, 600, 300); // 折線圖
$renderer->render($dataObj);

四、常見問題與優化建議

  • 輸出為檔案而非直接輸出:$chart->render('path/to/chart.png'); 避免 HTTP 標頭衝突。
  • 大數據量效能:Libchart 純 PHP 繪圖,數據點超過 500 可能明顯延遲,建議改用 pChart 或前端圖表庫。
  • 自定義顏色:繼承 Plot 類覆寫 getColor() 方法實現品牌色系。

結語

Libchart 輕量零依賴,適合簡單報表與後台圖表嵌入。透過字體替換與編碼轉換,可完美支援中文環境。若專案需互動、動畫或大數據視覺化,建議改用 ECharts、Chart.js 等前端方案,後端僅提供 JSON API。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved