2

I'm trying to create a simple graph with jpgraph (which is a new thing for me, I've used the example for the bars graph from their website.) and want the y-axis to return whole/rounded numbers. So I've searched the web and found out I have to use textint.

So now I've got these 2 lines:

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

But somehow instead of returning a whole number I now get 0f for every step on the y-axis. I just can't figure out why it results into a 0f :( Does somebody have the magical answer for me? What am I doing wrong or so to make it result into the value 0f?

Example image here

More code:

$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;
}

Edit: I just noticed while changing the height setting of the graphs (now 500 by 180 instead of 500 by 200) that it now started to show the numbers I was expecting. Is this a bug in the plugin itself?

Miranda Breekweg
  • 207
  • 1
  • 2
  • 11

1 Answers1

4

This is a problem with the plugin. I ran into this issue as well when moving from PHP5.6 to PHP7.

The problem:

The method _doLabelFormat() in class LinearTicks inside of jpgraph.php calculates a $precision value and then uses that as part of the formula in sprintf call. This $precision value is sometimes a negative number which makes the sprintf formula invalid in PHP7.

The solution:

The last else call in the method LinearTicks->_doLabelFormat() looks like this:

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

and in my file shows up around line #4306

just add the following line above:

$precision = abs( $precision );

This makes sure your $precision value is always a positive number and should fix all of the formatting issues that were getting passed into that call of sprintf.

K.B.
  • 452
  • 2
  • 9
  • God bless you sir (or ma'am ;)). Its a shame that jpGraph, an otherwise excellent library, has remained fallow while the rest of the PHP world marches into version 7 – Ifedi Okonkwo Apr 10 '18 at 17:04