Project:Google chart API
Version:6.x-1.3
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review

Issue Summary

Can these charts handle negative values? I'm having trouble configuring a bar chart. Basically there will be two values, each inserted dynamically. The values can be positive or negative (or one of each). The values will most likely be between -1 and 1 but could theoretically span to any amount. I assumed the handle negative numbers the y-axis would place 0 in the middle and branch the charts accordingly. Also, how do you get the y-axis values to be listed? I tried using the mixed_axis_label thing but it just placed given values on there that were meaningly.

Thanks..

Comments

#1

Might as well just add to this thread..

Why aren't three different colors showing up...??

$chart = array(
'#chart_id' => 'positions',
'#title' => t('Comments (') . $stats['comments'] . t(')'),
'#type' => CHART_TYPE_BAR_V,
'#size' => chart_size(400, 150),
'#grid_lines' => chart_grid_lines(50, 50),
'#bar_size' => chart_bar_size(80, 45),
'#adjust_resolution' => TRUE,
);
    
$chart['#data'][] = $stats['comments_neutral'];
$chart['#data'][] = $stats['comments_positive'];
$chart['#data'][] = $stats['comments_negative'];
  $chart['#labels'][] = t('Neutral');
$chart['#labels'][] = t('Positive');
$chart['#labels'][] = t('Negative');
$chart['#data_colors'][] = 'fff835';
$chart['#data_colors'][] = '168a16';
$chart['#data_colors'][] = 'ff1717';
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, max($stats['comments_neutral'], max($stats['comments_positive'], $stats['comments_negative'])));
$output .= chart_render($chart);

#2

Two more problems..i think these might be on your end..

'#adjust_resolution' => FALSE, (still adjusts resolution)

I noticed line 254 had a problem if ($chart['#adjust_resolution'] === TRUE){

i fixed that and the problem still exists. obviously if i didn't want to adjust resolution i'd leave that out but the chart data is dynamic so i was trying to use...

'#adjust_resolution' => $stats['comments'] != '0' ? TRUE : FALSE,

The problem seems to be in the statement below

elseif ($chart['#adjust_resolution']['#adjust'] = TRUE){
_chart_adjust_resolution($chart['#chart_id'], $chart['#data'], $chart['#adjust_resolution']['#max']);
}

I removed it.

#3

Hello,

The bar graphs are a little misleading due to Googles different bar graph types. CHART_TYPE_BAR_V and CHART_TYPE_BAR_H both assume that the data sets are being repeated which is why the colors are the same throughout. You will want to use CHART_TYPE_BAR_V_GROUPED or CHART_TYPE_BAR_H_GROUPED.

I will try and document on this issue soon so it is less confusing ( it confuses myself sometimes too haha ).

As for negative numbers many of the chart types do not support displaying negative numbers.

Also in this case you would probably want to use #legends instead of #labels

And your right about the resolution adjustment, I had a typo there, I fix that right away.

Here is my code which is similar to yours there, hopefully this will help a bit, let me know if/when it is working correctly I will document some more examples on the wiki.

  $chart = array(
    '#chart_id' => 'positions',
    '#title' => t('Comments'),
    '#type' => CHART_TYPE_BAR_V_GROUPED,
    '#size' => chart_size(400, 150),
    '#grid_lines' => chart_grid_lines(50, 50),
    '#bar_size' => chart_bar_size(80, 45),
    '#adjust_resolution' => FALSE,
  );
   
  $chart['#data']['group1'][] = 6;
  $chart['#data']['group2'][] = 23;
  $chart['#data']['group3'][] = 4;
  $chart['#legends'][] = t('Neutral');
  $chart['#legends'][] = t('Positive');
  $chart['#legends'][] = t('Negative');
  $chart['#data_colors'][] = 'fff835';
  $chart['#data_colors'][] = '168a16';
  $chart['#data_colors'][] = 'ff1717';
  $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(-1, 1);
  $output .= chart_render($chart);

#4

Thank you for the response. The different colors work now. Using -1, 1 as the range label doesn't work though, as suspected. If you look at the code I pasted, i use a few max()'s to determine the largest value of the three, and then make the range from zero to that. As for negative numbers, I figured the best solution would be to use a handful of if's and make the color red if its negative and green if its positive. If its negative i'd use absolute value to the numbers stay positive. Makes sense I think...

#5

Status:active» closed (fixed)

Hmm the negative range worked for me in the example I posted, displaying -75 -50 - 25 0 25 50 75 etc. Its to bad Google does not support negative values on the bar charts I am surprised they dont yet. Let me know how it goes, open another issue if you need.

#6

Status:closed (fixed)» active

I have the same issue with the colors. I tried using the V_GROUPED graph type, but then instead of colors malfunctioning, the labels malfunction. I wonder how I can get both labels and colors correctly working on my bar graph...

#7

Status:active» postponed (maintainer needs more info)

Is this still an issue?

#8

Status:postponed (maintainer needs more info)» closed (cannot reproduce)

Seems to support this in 6.x and 7.x (which are all that is supported). Please reopen if not the case and change the version.

#9

Version:5.x-1.1-0» 6.x-1.3
Category:support request» bug report
Status:closed (cannot reproduce)» needs review

I'm using 6.x-1.3 and found problems in the module regarding negative values. I found several places in _chart_adjust_resolution which assume the values are all positive – in particular, negative values were not getting adjusted at all.

This patch finds the entire range of values in the input data, then adjusts and maps it to the positive range 0 - 100.

I'd appreciate some reviews as I'm new to Chart API and I'm only using a line & bar graph - definitely have not tested extensively but this works well for my case.

N.B. This patch also adds support for bar chart group spacing & margins, unrelated to negative values.

AttachmentSize
234127.patch 6.26 KB

#10

Ha it's been three years and there's like 6 patches, and still no commit.

#11

I'll see if I can get some time to review this and commit.

@mikestefff: feel free to help things along by reviewing. this module just doesn't have a full time maintainer. I volunteered because I wanted to get it caught up, but I don't have the time to maintain long term.

#12

I wasn't pointing fingers out or anything..just saying.. it's funny how people keep submitting variations of the same patch. And I actually just noticed that this isn't the issue that I thought it was.. I thought it was #594202: Division by zero error.

#13

Code in #1213796: _chart_adjust_resolution calculation could be used instead of above patch. TBD

nobody click here