When adding a node reference field to Chart view, e.g. set it as X and count node ids, the output chart is rendering referenced nodes IDs. This is because of function _transform_data() in views_plugin_style_chart.inc:

  function _transform_data() {
    $db_rows = $this->view->result;

Calling $this->view->result returns views results in an array with no formatters applied to it, which leads to node ids of referenced nodes instead of titles/linked titles being returned.

Comments

gabidrg’s picture

A fast and ugly fix replaces on line 178:

      foreach ($cols as $key => $col ) {
        if ( trim($key) == trim($x_label_column) ) {
          $labels[] = $row->$key;
        }

with

      foreach ($cols as $key => $col ) {
        if ( trim($key) == trim($x_label_column) ) {
          //$labels[] = $row->$key;
         $load_refs = node_load($row->$key);
         $labels[] = $load_refs->title;
        }

This is surely an unhappy way to do it, but I see no other approach by now. Maybe assigning labels by using a theme_ function would do some good in the future regarding flexibility. Any other ideas?

irakli’s picture

Status: Active » Needs review

Thank you. Will look into it and get back to you.

killes@www.drop.org’s picture

Status: Needs review » Active

the same problem applies to fields that have rewriting enabled (through tokens or otherwise). Rather annoying.

Changing status as the suggested solution is not really feasible.

Daniel A. Beilinson’s picture

Fix #1 is badly for me because it brakes rows without node references. So this is my fast and ugly fix for foreach cycle from line 178:

foreach ($db_rows as $row ) {
      $cols = (array)$row;
      foreach ($cols as $key => $col ) {
        if ( trim($key) == trim($x_label_column) ) {
		 if (node_load($row->$key)) {
         $load_refs = node_load($row->$key);
         $labels[] = $load_refs->title;
		 }
		 else {
		 $labels[] = $row->$key;
		 }
        }
        if (array_key_exists($key, $fields_x_names)) {
          $full_name = $fields_x_names[$key];
          $rows[$full_name][] = (float)$col;
        }        
      }
    }
rsevero’s picture

Version: 6.x-1.0-alpha1 » 6.x-1.0-rc1
Assigned: Unassigned » rsevero
Status: Active » Fixed

I've created the following test situation:
Node types: news and newspaper.
The news content type references newspaper.
I created the view below to chart the number of news per newspaper.
It worked as expected, i.e., the chart have the aggregate values per newspaper and the newspaper names were present as the labels.
Please observe that you have to create a relationship between news and newspaper in the view. You can't use the ref field available in news directly otherwise the result won't be aggregated.
Please reopen if the problem persists.

$view = new view;
$view->name = 'test_views_charts_node_referenc';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'field_newspaper_ref_nid' => array(
    'label' => 'Newspaper',
    'required' => 0,
    'delta' => -1,
    'id' => 'field_newspaper_ref_nid',
    'table' => 'node_data_field_newspaper_ref',
    'field' => 'field_newspaper_ref_nid',
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => 'Title',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'field_newspaper_ref_nid',
  ),
  'nid' => array(
    'label' => 'Nid Count',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'none',
  ),
  'views_sql_groupedfields' => array(
    'label' => 'Group By Fields',
    'alter' => array(
      'alter_text' => FALSE,
      'text' => '',
      'make_link' => FALSE,
      'path' => '',
      'alt' => '',
      'link_class' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'trim' => FALSE,
      'max_length' => '',
      'word_boundary' => TRUE,
      'ellipsis' => TRUE,
      'strip_tags' => FALSE,
      'html' => FALSE,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'exclude' => '1',
    'id' => 'views_sql_groupedfields',
    'table' => 'views_groupby',
    'field' => 'views_sql_groupedfields',
    'relationship' => 'none',
    'views_groupby_fields_to_group' => array(
      'title' => 'title',
    ),
    'views_groupby_sql_function' => 'count',
    'views_groupby_fields_to_aggregate' => array(
      'nid' => 'nid',
    ),
    'views_groupby_field_sortby' => 'nid',
    'views_groupby_sortby_direction' => 'desc',
  ),
));
$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'news' => 'news',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('style_plugin', 'charts');
$handler->override_option('style_options', array(
  'views_charts_series_fields' => array(
    'nid' => 'nid',
  ),
  'views_charts_x_labels' => 'title',
  'width' => '500',
  'height' => '600',
  'engine' => 'amcharts',
  'type' => array(
    'amcharts' => 'pie',
    'bluff' => 'line',
    'google-charts' => 'line',
    'open-flash' => 'line',
  ),
  'wmode' => 'window',
  'y_min' => '0',
  'y_legend' => '',
));
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'test_views_charts_node_reference');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
rsevero’s picture

Forgot to mention that I did the above test without any of the code modifications suggested in this issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.