-1

以下代码使用库 phplot.ph 并通过对“medallas”表的 SQL 查询获取数据。一旦查询返回结果;我确实将结果转换为 php 关联 array() 结构,以便可以绘制它们。我得到的结果是一个数据正确绘制的图表,但是在 x 轴上没有显示月份的名称,例如 Ene、Feb、...、Dic(它是西班牙语)。

我已经查看了源代码,但找不到这种行为的原因。我附上源代码和屏幕上获得的结果。

php源代码:

<?php

# PHPlot Example: Stacked Bars, unshaded, with Y data labels
require_once 'phplot-6.2.0/phplot.php';

require 'db_connect.php';

# build the associative array() in php

$sql = "SELECT * FROM medallas";
$result = $conn->query($sql);
$datas = array();
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
     $datas[] = $row;
}
}

$data = $datas;

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain');
$plot->SetPlotType('stackedbars');
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
$plot->SetTitle(utf8_decode('Número de medallas por mes y por disciplina deportiva'));
$plot->SetYTitle(utf8_decode('Número de medallas'));
# No shading:
$plot->SetShading(0);
$plot->SetLegend(array(utf8_decode('Natación'), 'Pesas', 'Taekwondo', 'Atletismo Carreras'));
# Make legend lines go bottom to top, like the bar segments (PHPlot > 5.4.0)
$plot->SetLegendReverse(True);
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');
# Turn on Y Data Labels: Both total and segment labels:
$plot->SetYDataLabelPos('plotstack');
$plot->DrawGraph();
?>

附上得到的结果:

在此处输入图像描述

作为了解代码行为的测试,我对图表数据进行了硬编码,得到的结果符合我的预期。即,一年中月份的标签显示在 X 轴上。包含硬编码数据的代码之间的唯一区别是我不使用从存储在 mysql 数据库中的表中获取数据的查询,也不使用 php.ini 将该数据转换为关联数组。

以下是包含硬编码数据的 php 源代码:

<?php

# PHPlot Example: Stacked Bars, unshaded, with Y data labels
require_once 'phplot-6.2.0/phplot.php';
$data = array(
 array('Ene', 40, 5, 10, 3), array('Feb', 90, 8, 15, 4),
 array('Mar', 50, 6, 10, 4), array('Abr', 40, 3, 20, 4),
 array('May', 75, 2, 10, 5), array('Jun', 45, 6, 15, 5),
 array('Jul', 40, 5, 20, 6), array('Ago', 35, 6, 12, 6),
 array('Sep', 50, 5, 10, 7), array('Oct', 45, 6, 15, 8),
 array('Nov', 35, 6, 20, 9), array('Dic', 40, 7, 12, 9),
);
$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain');
$plot->SetPlotType('stackedbars');
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
$plot->SetTitle(utf8_decode('Número de medallas por mes y por disciplina deportiva'));
$plot->SetYTitle(utf8_decode('Número de medallas'));
# No shading:
$plot->SetShading(0);
$plot->SetLegend(array(utf8_decode('Natación'), 'Pesas', 'Taekwondo', 'Atletismo Carreras'));
# Make legend lines go bottom to top, like the bar segments (PHPlot > 5.4.0)
$plot->SetLegendReverse(True);
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');
# Turn on Y Data Labels: Both total and segment labels:
$plot->SetYDataLabelPos('plotstack');
$plot->DrawGraph();
?>

附件是带有在 x 轴上显示月份名称的图表的结果。

在此处输入图像描述

对此的任何帮助或指导将不胜感激。谢谢。

4

0 回答 0