Negative Data values?
mikestefff - March 14, 2008 - 08:44
| Project: | Chart API |
| Version: | 5.x-1.1-0 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
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..

#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
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
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...