2017-08-03 28 views
0

我试图用jpgraph创建一个简单的图形(这对我来说是一个新东西,我用他们网站上的条形图示例)并且想要y轴返回整个/四舍五入的数字。 所以我搜查了网页,发现我必须使用textint。jpgraph textint y轴返回0f而不是数字

所以,现在我已经有了这两条线:

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

但不知何故,而不是返回一个整数的我现在得到0F关于y轴的每一步。 我只是想不通为什么它会导致成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; 
} 

编辑: 我而改变高度SETT只注意到现在开始显示我期望的数字(现在500乘以180而不是500乘以200)。这是插件本身的错误吗?

回答

0

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

问题

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

解决方案

最后else调用的方法LinearTicks - > _ doLabelFormat()看起来是这样的:

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

,并在我的文件显示周围行#4306

只需添加以下行以上

$precision = abs($precision);

这可以确保您的$precision值始终是正数,应该可以解决所有这一切都拿到传入的sprintf该呼叫的格式问题。