0

I'm using the Charts module with hightcharts to create charts in views, but I'm unable to create stacked one's.

Is this possible in views? Or is there any way I can override it with a custom module to achieve the same result?

Here is an example of what I need.

Also, my view:

$view = new view();
$view->name = 'stackedchart';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'StackedChart';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'My Stacked Chart';
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '50';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['pager']['options']['quantity'] = '9';
$handler->display->display_options['style_plugin'] = 'views_bootstrap_table_plugin_style';
$handler->display->display_options['style_options']['grouping'] = array(
  0 => array(
    'field' => 'field_x_qc_testset',
    'rendered' => 1,
    'rendered_strip' => 0,
  ),
);
$handler->display->display_options['style_options']['columns'] = array(
  'field_x_qc_week' => 'field_x_qc_week',
  'field_x_qc_status' => 'field_x_qc_status',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'field_x_qc_week' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'field_x_qc_status' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
$handler->display->display_options['style_options']['bootstrap_styles'] = array(
  'striped' => 0,
  'bordered' => 0,
  'hover' => 0,
  'condensed' => 0,
);
/* Field: Content: x_qc_week */
$handler->display->display_options['fields']['field_x_qc_week']['id'] = 'field_x_qc_week';
$handler->display->display_options['fields']['field_x_qc_week']['table'] = 'field_data_field_x_qc_week';
$handler->display->display_options['fields']['field_x_qc_week']['field'] = 'field_x_qc_week';
$handler->display->display_options['fields']['field_x_qc_week']['label'] = 'Week';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'xreport_qc' => 'xreport_qc',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page_2');
$handler->display->display_options['defaults']['group_by'] = FALSE;
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['defaults']['pager'] = FALSE;
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'chart';
$handler->display->display_options['style_options']['library'] = 'highcharts';
$handler->display->display_options['style_options']['type'] = 'column';
$handler->display->display_options['style_options']['label_field'] = 'field_x_qc_week';
$handler->display->display_options['style_options']['data_fields'] = array(
  'field_x_qc_status' => 'field_x_qc_status',
  'field_x_qc_week' => 0,
);
$handler->display->display_options['style_options']['field_colors'] = array(
  'field_x_qc_week' => '#2f7ed8',
  'field_x_qc_status' => '#0d233a',
);
$handler->display->display_options['style_options']['data_labels'] = 'TRUE';
$handler->display->display_options['style_options']['tooltips'] = 'TRUE';
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: x_qc_week */
$handler->display->display_options['fields']['field_x_qc_week']['id'] = 'field_x_qc_week';
$handler->display->display_options['fields']['field_x_qc_week']['table'] = 'field_data_field_x_qc_week';
$handler->display->display_options['fields']['field_x_qc_week']['field'] = 'field_x_qc_week';
$handler->display->display_options['fields']['field_x_qc_week']['label'] = 'Week';
/* Field: Content: x_qc_status */
$handler->display->display_options['fields']['field_x_qc_status']['id'] = 'field_x_qc_status';
$handler->display->display_options['fields']['field_x_qc_status']['table'] = 'field_data_field_x_qc_status';
$handler->display->display_options['fields']['field_x_qc_status']['field'] = 'field_x_qc_status';
$handler->display->display_options['fields']['field_x_qc_status']['group_type'] = 'count';
$handler->display->display_options['fields']['field_x_qc_status']['separator'] = ', ';
$handler->display->display_options['defaults']['sorts'] = FALSE;
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['defaults']['filters'] = FALSE;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'xreport_qc' => 'xreport_qc',
);
$handler->display->display_options['path'] = 'stackedchart';

If it can't be made with views, doing it with a custom module using EntityFieldQuery is also a good solution. My only issue is how to show the data in different x-axis values (field value) not knowing all the values of that field.

EDIT: Tried Pierre suggestion. dsm is outputing the name of the chart and I use it as $chart_id. No stack is produced in the chart.

The fields I have are week and status. I have multiple nodes, with different weeks and status and I need to stack the status for each week. Below is the chart WITHOUT stacking.

enter image description here

1
  • Pierre, I solved the problem. I already had a view with multiple displays. Each one was filtered by x_qc_status (like different 'Joe'). I was assuming that, with the chart_alter function I was able to do it with only one views page. So I recreated this with multiple displays + chart_alter and now the chart shows as I need. Many thanks for your support.
    – zephirus
    Oct 27, 2015 at 12:45

1 Answer 1

1

Option 1

Refer to the issue about Stacking column/bar charts for an example of how you can do this with a custom module. Comment #3 in it contains a sample like so:

<?php
function MYMODULE_chart_alter(&$chart, $chart_id) {
  dsm($chart_id); // Need Developer module enabled for dsm to work
  if ($chart_id == 'MYCHARTVIEW__DISPLAY') { // Watch out for those double underscores
    $chart['#stacking'] = TRUE;
  }
}
?>

This should work for both Google Charts and Highcharts as the charting library (engine).

Option 2

Your x_qc_status is like the equivalent of "Joe" in the sample you referred to. But you don't have any equivalent of at least "John" (and maybe also "Jane")? In other words: I think you only have 1 dataset to be shown in your "stacked" bar chart, and that your problem is not in the chart, but in the related "view" for which you want to create a "stacked" chart.

Therefor try the "bonus tip" mentioned in Steps to create charts using the views UI. That may help to refine your view before actually creating the chart.

3
  • Pierre, that doesn't work. I already tried.
    – zephirus
    Oct 27, 2015 at 10:59
  • Can you enhance your question to explain the "that doesn't work"? That might help to get you going. Oct 27, 2015 at 11:14
  • Sorry Pierre, it was not well explained. I edited and added more information. Hope it's enought to understand what's the issue. And thanks again for the support.
    – zephirus
    Oct 27, 2015 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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