2 bar chart 2 colors

decibel.places - August 26, 2008 - 01:12
Project:Chart API
Version:5.x-1.2
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

Great work, could use some more documentation, I spent a lot of time in the module to get it to work.

One thing I could not solve was how to have a chart with 2 distinct bars with 2 different colors.

I believe it might thave something to do with the default is chd:t and chd:s is not generated.

Bar chart:

http://chart.apis.google.com/chart?chd=t%3A55.8%2C28.9&cht=bhs&chs=280x1...

I was successful in creating a scatter plot of userpoints by pid # - Khalil has asked me to make it into a contrib module for userpoints.

http://chart.apis.google.com/chart?chd=t%3A12.5%2C25%2C37.5%2C50%2C62.5%...

the Drupal code is here:

http://snippets.dzone.com/posts/show/5985

#1

decibel.places - August 26, 2008 - 01:13

oops, my bad, I meant "Khalid"!

#2

tjholowaychuk - August 26, 2008 - 03:36

hello,
Another user had the same issue, I think the issue may have been closed though ( was quite old ). this was due to the data passed being a single dataset. I didnt have a look at your code but you may need to simply 'nest' the data arrays another level.

http://code.google.com/p/drupal-chart-api/wiki/Examples

in one of those examples I created you will see:

$chart['#data'][] = array(40, 50, 70);
$chart['#data'][] = array(40, 60, 20);

$chart['#data'][] = 40
$chart['#data'][] = 50

#3

decibel.places - August 26, 2008 - 20:44

Yeah I tried that - it will work for multiple data in each array but not for one data in each color... it will only show both bars in the first color

If more than one datapoint is in each array, then the colors sort - but I only want 2 bars (tried making the extra data 0 too...)

tried CHART_TYPE_BAR_H_GROUPED, CHART_TYPE_BAR_H, $chart['#data_colors']['solo'] = 'ff0000';, $chart['#data_colors'][] = 'ff0000';

http://chart.apis.google.com/chart?chd=t%3A55.8%2C28.9&cht=bhg&chs=500x2...

$solopts and $avgpoints are positive integers, in this case 558 and 289 respectively...

$chart = array(
      '#chart_id' => 'flybar',
      '#title' => chart_title(t('flyDollars'), '6d769f', 25),
      '#type' => CHART_TYPE_BAR_H_GROUPED,
      '#size' => chart_size(500, 200),
  '#bar_size' => chart_bar_size(30, 5),
);

$chart['#data']['solo'] = $solopts;
$chart['#data']['avg'] = $avgpts;

$chart['#data_colors']['solo'] = 'ff0000';
$chart['#data_colors']['avg'] = '00ff00';

$grid = array(10,10,1,1);
$chart['#grid_lines'] = $grid;

$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_range_label(0, 1000);
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t(' '));   
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t(' '));
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t(' '));   

$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][2][] =  chart_mixed_axis_label(t('average $'));
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][2][] =  chart_mixed_axis_label(t('my $'));
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][2][] =  chart_mixed_axis_label(t(' '));


//label colors
$chart['#mixed_axis_label_styles'][] = array(0,'000000',12);
$chart['#mixed_axis_label_styles'][] = array(1,'6d769f',18);
$chart['#mixed_axis_label_styles'][] = array(2,'6d769f',18);

echo chart_render($chart);

#4

iLikeSunshine - September 29, 2008 - 05:11

I'm having the same issue described here - I want to draw a bar chart with the bars colored according to the value of a node property. But, the only color displayed is the first one that I assign to $chart['#data_colors'][X] , where X is any number, or a blank space.

#5

foxtrotcharlie - March 15, 2009 - 09:04

I've figured out that for a normal (non-grouped) bar chart, if the chart colours that are passed in the url are separated by a pipe (|) and not a comma (,) then each bar gets a different colour.

On my site, because I'm only ever using a single bar chart, I hacked the chart.module function chart_build line 280 from:

  _chart_append('chtm', $chart['#georange'],                $data);
        
  $charts[$chart['#chart_id']] = drupal_query_string_encode($data);      
                 
  return $charts[$chart['#chart_id']];

to:

  _chart_append('chtm', $chart['#georange'],                $data);
 
  // For standard (non-grouped) bar charts, we need to use a pipe separator
  // instead of a comma otherwise we lose the different colours.
  if ($chart['#type'] == CHART_TYPE_BAR_V || $chart['#type'] == CHART_TYPE_BAR_H) {
    $data['chco'] = str_replace(',', '|', $data['chco']);
  }
 
  $charts[$chart['#chart_id']] = drupal_query_string_encode($data);      
                 
  return $charts[$chart['#chart_id']];

I haven't tested this code extensively to see if it affects the other graph types (it shouldn't), so take care when using it.

IF this looks like a good solution to the module maintainer(s), then I'm happy to create a patch - let me know, or let me know of a better way of doing this.

 
 

Drupal is a registered trademark of Dries Buytaert.