You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHPLOT调用自定义数组无法生成图表问题求助

PHPLOT柱状图无法显示问题排查与修复

问题场景

使用PHPLOT时,测试数据能正常生成柱状图,但调用ProcessaPdf::geraArrayPalavrasChave()获取$words数组替换测试数据后,图表完全无法显示。

原图表代码

<?php 
include_once ("class/ProcessaPdf.class.php");
require_once 'lib/phplot-5.5.0/phplot.php';

//$words = ProcessaPdf::geraArrayPalavrasChave();

$plot = new PHPlot(640 , 480);

$data = array(
    array('1940' , 6.2 ), 
    array('1950' , 6.2 ),
    array('1960' , 6.3 ),
    array('1970' , 5.8 ),
    );

$plot->SetTitle('Grafico da analise de palavras-chave encontradas');
$plot->SetPlotType("bars");

$plot->SetXLabel("Palavras");
$plot->SetYLabel("Frequencia");

$plot->SetXLabelFontSize(2);
$plot->SetAxisFontSize(2);

$plot->SetDataValues($data);
$plot->SetYDataLabelPos('plotin');

$plot->DrawGraph();
?>

生成$words的方法代码

static function geraArrayPalavrasChave(){
    $p_chaves = file("./lib/palavras_chave.txt", FILE_TEXT | FILE_IGNORE_NEW_LINES);

    // Initialize and load PDF Parser library 
    $parser = new \Smalot\PdfParser\Parser(); 
    
    // Source PDF file to extract text 
    $file = 'lib/projeto.pdf'; 
    
    // Parse pdf file using Parser library 
    $pdf = $parser->parseFile($file); 
    
    // Extract text from PDF 
    $text = $pdf->getText();

    for($i=0; $i<count($p_chaves); $i++){
        if(substr_count(' '.$text.' ', ' '.$p_chaves[$i].' ') != ''){
            $dados[$i][] = $p_chaves[$i];
            $dados[$i][] = substr_count(mb_strtoupper(' '.$text.' '), mb_strtoupper(' '.$p_chaves[$i].' '));
        }
    }

   for($i=0; $i<count($dados); $i++){
        $key[] = $i;
    }

    $arrayPalavras = array_combine($key, $dados);

    return $arrayPalavras;
}

$words数组结构

Array (     
    [0] => Array (
        [0] => inovações             
        [1] => 3         
    )       
    [1] => Array (
        [0] => tecnologia da informação             
        [1] => 2         
    )       
    [2] => Array (
        [0] => intelectuais             
        [1] => 4         
    )       
    [3] => Array (
        [0] => patente             
        [1] => 5         
    )   
)

问题根源

  1. 数组索引不连续:原方法中通过for循环生成索引再array_combine,若某个关键词匹配失败,$dados会出现索引断层,PHPLOT无法解析非连续索引的数据集。
  2. 特殊字符与长标签兼容问题:关键词包含重音字符和空格,PHPLOT默认渲染逻辑可能因编码或宽度限制导致图表崩溃。

修复方案

1. 修正数组生成逻辑

修改geraArrayPalavrasChave(),改用数组追加方式确保索引连续:

static function geraArrayPalavrasChave(){
    $p_chaves = file("./lib/palavras_chave.txt", FILE_TEXT | FILE_IGNORE_NEW_LINES);
    $parser = new \Smalot\PdfParser\Parser(); 
    $file = 'lib/projeto.pdf'; 
    $pdf = $parser->parseFile($file); 
    $text = $pdf->getText();

    $dados = [];
    foreach($p_chaves as $chave){
        $count = substr_count(mb_strtoupper(' '.$text.' '), mb_strtoupper(' '.$chave.' '));
        if($count > 0){
            $dados[] = [$chave, $count];
        }
    }

    return $dados;
}

2. 优化图表渲染配置

调整图表参数,适配特殊字符和长标签:

<?php 
include_once ("class/ProcessaPdf.class.php");
require_once 'lib/phplot-5.5.0/phplot.php';

// 开启错误提示便于调试
error_reporting(E_ALL);
ini_set('display_errors', 1);

$words = ProcessaPdf::geraArrayPalavrasChave();

// 增大画布宽度适配长标签
$plot = new PHPlot(800 , 500); 

$plot->SetTitle('Grafico da analise de palavras-chave encontradas');
$plot->SetPlotType("bars");

$plot->SetXLabel("Palavras");
$plot->SetYLabel("Frequencia");

// 设置UTF-8字体支持重音字符,替换为实际字体路径
$plot->SetDefaultTTFont('lib/fonts/DejaVuSans.ttf');
$plot->SetXLabelFontSize(2);
$plot->SetAxisFontSize(2);

// 关闭X轴底部重复标签,改用柱子内数值标签
$plot->SetXTickLabelPos('none');
$plot->SetDataLabels('value');

$plot->SetDataValues($words);
$plot->SetYDataLabelPos('plotin');

$plot->DrawGraph();
?>

3. 调试步骤

  • 打印$words数组,确认是连续索引的二维数组,每个子数组包含字符串标签和数字值。
  • 开启PHP错误提示,查看PHPLOT是否抛出数据格式、字体缺失等错误。

内容的提问来源于stack exchange,提问作者Jeanfson Dutra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 17:53:29