1

I'm using Charts module to create some charts using the API. Depending on some variable i can create a pie chart or a column chart.

That variable is chart_type:

$chart_type = 'column';
$chart_type = 'pie';

This is the code I have:

$chart = array(
    '#type' => 'chart',
    '#title' => 'title',
    '#chart_type' => $chart_type,
    '#chart_library' => 'google',
    '#legend' => FALSE,
    '#tooltips' => TRUE,
    '#height' => 234,
    '#colors' => array('#006400', '#c11d0b'),
);

$chart['Status'] = array(
    '#type' => 'chart_data',
    '#title' => 'title',
    '#data' => array(500, 100),
    '#show_in_legend' => FALSE,
    '#show_labels' => FALSE,
);

$chart['xaxis'] = array(
    '#type' => 'chart_xaxis',
    '#labels' => array('Good', 'Bad'),
);

$my_chart['chart'] = $chart;

return drupal_render($my_chart);

When the chart is of type Pie, I can see the two colors (green and red) correctly assigned to the slices of the pie. However, I can't assign the same principle to the column chart - in this case, both bars are green.

What I need is to have the colunn chart with the first bar green and the second one, red.

EDIT: Also, on the pie chart, I get two circles and I want to get rid of them. I thought it was a legend but it's not so... how can I remove them also?

0

It's hard to tell (without access to your site) what's causing your issue. But here is a working sample of a chart using the Charts API (disclosure: I'm a co-maintainer ...):

<?php
  echo '<p>&nbsp;</p>';
  $chart = array(
    '#type' => 'chart',
    '#chart_type' => 'column', // pie, bar, line, column, area, scatter
    '#chart_library' => 'google', // Allowed values: 'google' or 'highcharts'
    '#title' => t('Meals by store'),
   '#colors' => array('#006400', '#000064', '#640000', '#c11d0b'),
  );
  $chart['store1'] = array(
    '#type' => 'chart_data',
    '#title' => t('Store 1'),
    '#data' => array(16, 11, 13),
    '#suffix' => ' Meals',
  );
  $chart['store2'] = array(
    '#type' => 'chart_data',
    '#title' => t('Store 2'),
    '#data' => array(12, 14, 17),
    '#suffix' => ' Meals',
  );
  $chart['store3'] = array(
    '#type' => 'chart_data',
    '#title' => t('Store 3'),
    '#data' => array(9, NULL, NULL),
    '#suffix' => ' Meals',
  );
  $chart['store4'] = array(
    '#type' => 'chart_data',
    '#title' => t('Store 4'),
    '#data' => array(5, 10, 20),
    '#suffix' => ' Meals',
  );
  $chart['xaxis'] = array(
    '#type' => 'chart_xaxis',
    '#labels' => array('Breakfasts', 'Light Meals', 'Sandwiches'),
  );

  // dpm ($chart);

  $example['chart'] = $chart;

 return drupal_render($example);
?>

If you have the (dangerous) PHP filter enabled, just paste it all in the body of node, and see what happens (make sure you have a working internet connection if you use the Google library).

It should show 3 column charts (1 for each type of "meal"), with each chart having up to 4 columns (1 for each "store").

BTW, make sure you're using the most DEV version (I'm using Version: 7.x-2.0-rc1+5-dev).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.