【问题标题】:jpgraph textint y-axis returns 0f instead of numberjpgraph textint y 轴返回 0f 而不是数字
【发布时间】:2018-01-11 07:35:00
【问题描述】:

我正在尝试使用 jpgraph 创建一个简单的图形(这对我来说是一个新事物,我使用了他们网站上的条形图示例。)并希望 y 轴返回整数/四舍五入的数字. 所以我搜索了网络,发现我必须使用 textint。

所以现在我有了这两行:

$graph->SetScale("textint");
$graph->yaxis->SetTickPositions(array(0,1,2,3,4,5);

但不知何故,我现在在 y 轴上的每一步都得到 0f,而不是返回一个整数。 我只是无法弄清楚为什么它会导致 0f :( 有人对我有神奇的答案吗?我做错了什么以使其结果为 0f 值?

Example image here

更多代码:

$graphSettings = [
    'tick_positions' => [0, 1, 2, 3, 4, 5],
    'tick_labels' => ['Q1 2017', 'Q2 2017', 'Q3 2017', 'Q4 2017', 'Q1 2018'],
];

$graphs = [];
foreach ($questions as $key => $question) {
    $graphs[] = $this->generateGraph($graphSettings, $question, $key);
}

public function generateGraph($settings, $question, $key) {
    $data1y = $question['bar_plots']; // height of bars

    // Create the graph. These two calls are always required
    $graph = new \Graph(500, 200, 'auto');
    $graph->SetScale("textint");

    $theme_class = new \UniversalTheme;
    $graph->SetTheme($theme_class);

    $graph->yaxis->SetTickPositions($settings['tick_positions']); // array y numbers, array y between dashes
    $graph->SetBox(false);

    $graph->ygrid->SetFill(false);
    $graph->xaxis->SetTickLabels($settings['tick_labels']);
    $graph->yaxis->HideLine(false);
    $graph->yaxis->HideTicks(false, false);

    // Create the bar plots
    $b1plot = new \BarPlot($data1y);

    // ...and add it to the graPH
    $graph->Add($b1plot);

    $b1plot->SetColor("white");
    $b1plot->SetFillColor("#41b6e6");

    // Return the graph
    $contentType = 'image/png';
    ob_start();
    $graph->Stroke();
    $image_data = ob_get_clean();

    $str = "data:$contentType;base64," . base64_encode($image_data);
    return $str;
}

编辑: 我刚刚注意到,在更改图表的高度设置(现在是 500 x 180 而不是 500 x 200)时,它现在开始显示我期望的数字。这是插件本身的错误吗?

【问题讨论】:

    标签: php jpgraph


    【解决方案1】:

    这是插件的问题。 从 PHP5.6 迁移到 PHP7 时,我也遇到了这个问题。

    问题

    jpgraph.php 内的 LinearTicks 类中的 _doLabelFormat() 方法计算 $precision 值,然后将其用作 sprintf 调用中公式的一部分。这个$precision 值有时是一个负数,这使得 sprintf 公式在 PHP7 中无效。

    解决方案

    LinearTicks->_doLabelFormat() 方法中的最后一个else 调用如下所示:

    $l = sprintf('%01.'.$precision.'f',round($aVal,$precision));

    在我的文件中显示在 #4306 行附近

    只需在上方添加以下行:

    $precision = abs( $precision );

    这可确保您的 $precision 值始终为正数,并且应该解决传递给 sprintf 调用的所有格式问题。

    【讨论】:

    • 上帝保佑你先生(或女士;))。可惜 jpGraph 是一个非常优秀的库,而 PHP 世界的其他部分进入第 7 版时却一直处于闲置状态
    猜你喜欢
    • 2014-06-08
    • 2013-04-29
    • 2017-04-03
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多