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

CommentFileSizeAuthor
#9 234127.patch6.26 KBlaken

Comments

mstef’s picture

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);
mstef’s picture

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.

tjholowaychuk’s picture

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);
mstef’s picture

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

tjholowaychuk’s picture

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.

iLikeSunshine’s picture

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

boombatower’s picture

Status: Active » Postponed (maintainer needs more info)

Is this still an issue?

boombatower’s picture

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.

laken’s picture

Version: 5.x-1.1-0 » 6.x-1.3
Category: support » bug
Status: Closed (cannot reproduce) » Needs review
StatusFileSize
new6.26 KB

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.

mstef’s picture

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

boombatower’s picture

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.

mstef’s picture

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.

13rac1’s picture

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

Pierre.Vriens’s picture

Issue summary: View changes
Parent issue: » #2371567: Chart 6.x-2.x Release

+1 for #10 ... which is like 4 years ago again. So is it now 7 years?

+1 for #11 ... and thanks for "approving" a few days ago to give me an opportunity to "try" to work on what I suggested in #2368793: Chart 2.0.

+1 for #12 ... note the parent link I added here ...

About #13: can anybody help me on the TBD there?

I want to get "this" issue in status fixed. And for that I first need to figure out which of the about 6 patches (as in #10, haven't counted them) "we" want to continue moving forward with. Any help or suggestions about that might speed up things to reach my goal. While waiting for that: where is George? ... the one from the keynote from Dries at DrupalCon Amsterdam 2014

avpaderno’s picture

Version: 6.x-1.3 » 6.x-1.x-dev
Status: Needs review » Closed (outdated)

I am closing this issue, as Drupal 6 is no longer supported.