Download & Extend

Wrong data on chart after adjust_resolution

Project:Google chart API
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (duplicate)

Issue Summary

I have the PIE chart with some values:

<?php
$chart
= array(
     
'#chart_id' => uniqid(),
     
'#type' => CHART_TYPE_PIE_3D,
     
'#size' => chart_size(500, 160),
     
'#adjust_resolution' => TRUE,
    );
$chart['#data'] = array([0] => 529 [1] => 102 [2] => 41 [3] => 25 [4] => 10 [5] => 5 [6] => 1 [7] => 1 );
$chart['#data_colors'] = array('78c0e9', 'ffe500', '50b432', 'ed561b');
echo
chart_render($chart);
?>

If I turn on "#adjust_resolution = true" I see a PIE with only 2 normal zones, and all other becomes zero-sized! Example:
http://chart.apis.google.com/chart?chd=t1%3A99%2C19%2C7%2C4%2C1%2C-1%2C-...

If I don't use "#adjust_resolution = true" option, I see the chart with bad size of zones (first zone must be much than 5 times bigger that other). Example:
http://chart.apis.google.com/chart?chd=t1%3A529%2C102%2C41%2C25%2C10%2C5...

Why after adjusing resolution i didn't see other zones and some values becomes -1?

Comments

#1

Correct php code is

<?php
$chart
= array(
     
'#chart_id' => uniqid(),
     
'#type' => CHART_TYPE_PIE_3D,
     
'#size' => chart_size(500, 160),
     
'#adjust_resolution' => TRUE,
    );
$chart['#data'] = array(529, 102, 41, 25, 10, 5, 1, 1);
$chart['#data_colors'] = array('78c0e9', 'ffe500', '50b432', 'ed561b');
echo
chart_render($chart);
?>

#2

If I change the line 447 in chart.module file from

<?php
               $data
[$k] = floor($v / $divider);
?>

to
<?php
               $data
[$k] = ceil($v / $divider);
?>

The pie looks normally:
http://chart.apis.google.com/chart?chd=t1%3A100%2C20%2C8%2C5%2C2%2C1%2C1...

#3

I have found the source of the problem!
In file chart.module function _chart_adjust_resolution() there are string:

<?php
       
if ($v != 'NULL' && $v > 0){
?>

NULL must not be in quotes, the correct code is:
<?php
       
if ($v != NULL && $v > 0){
?>

and another error in function _chart_encode_data($data)
<?php
       
if ($v != 'NULL'){
?>

must be
<?php
       
if ($v != NULL){
?>

but for normal show the zero values I recommend this variant:
<?php
       
if (is_numeric($v)){
?>

because in first variant the pie charts with zero values shows bad!
values: 8,0,100,13
chart with if ($v != NULL):
http://chart.apis.google.com/chart?chd=t1%3A8%2C-1%2C100%2C13&cht=p3&chs...
value string: 8,-1,100,13

chart with if (is_numeric($v)):
http://chart.apis.google.com/chart?chd=t1%3A8%2C0%2C100%2C13&cht=p3&chs=...
value string: 8,0,100,13

#4

Murz: I am using the 6.x-1.2 version of the Chart API module and had the same problem. Your fix described in #3 works great. Thanks a lot for figuring this out and sharing with us.

#5

Thanks for posting a fix, Murz!

I've taken the liberty of rolling that fix into a patch.

AttachmentSize
chart_fix_adjust_resolution.patch 639 bytes

#6

Version:6.x-1.x-dev» 7.x-1.x-dev
Status:active» needs review

Same patch for current 7.x-1.x-dev.

AttachmentSize
chart-numeric-617194-6.patch 690 bytes

#7

Version:7.x-1.x-dev» 6.x-1.x-dev
Status:needs review» patch (to be ported)

Patch applied to 7.x-1.x-dev. Needed in 6.x-1.x-dev.

#8

Status:patch (to be ported)» closed (duplicate)

Just realized, this is a dupe of #245532: formatting error for xy line charts, and issue with data that is equal to 0

nobody click here