From fe0d715c6825fa42abd696f749ad76bbf409ee85 Mon Sep 17 00:00:00 2001 From: Nathan Haug <nate@lullabot.com> Date: Mon, 17 Mar 2014 17:19:14 -0700 Subject: [PATCH] Issue #2130039: Interpolate missing data points instead of rendering as zeros. --- includes/charts.examples.inc | 23 +++++++++++++++++++ modules/charts_google/charts_google.inc | 1 + .../charts_highcharts/charts_highcharts.inc | 15 ++++++++---- views/charts_plugin_style_chart.inc | 18 +++++++++++++-- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/includes/charts.examples.inc b/includes/charts.examples.inc index bcdf68e..8cd56dd 100644 --- a/includes/charts.examples.inc +++ b/includes/charts.examples.inc @@ -161,6 +161,29 @@ function _charts_examples_bar_simple() { return $example; } +function _charts_examples_line_gap() { + $chart = array( + '#type' => 'chart', + '#chart_type' => 'line', + '#title' => t('Line with interpolated gap'), + ); + // Test with a gap in the data. + $chart['male'] = array( + '#type' => 'chart_data', + '#title' => t('Male'), + '#data' => array(array(0, 0), array(10, NULL), array(20, 30), array(30, 30), array(40, 40)), + ); + $chart['female'] = array( + '#type' => 'chart_data', + '#title' => t('Female'), + '#data' => array(array(0, 5), array(10, 14), array(20, 16), array(30, 36), array(40, 48)), + ); + + $example['chart'] = $chart; + + return $example; +} + function _charts_examples_scatter() { $chart = array( '#type' => 'chart', diff --git a/modules/charts_google/charts_google.inc b/modules/charts_google/charts_google.inc index afc7548..36a83cc 100644 --- a/modules/charts_google/charts_google.inc +++ b/modules/charts_google/charts_google.inc @@ -73,6 +73,7 @@ function _charts_google_populate_chart_options($chart, $chart_definition) { $chart_definition['options']['pieSliceText'] = $chart['#data_labels'] ? NULL : 'none'; $chart_definition['options']['legend']['position'] = $chart['#legend_position'] ? $chart['#legend_position'] : 'none'; $chart_definition['options']['legend']['alignment'] = 'center'; + $chart_definition['options']['interpolateNulls'] = TRUE; // TODO: Legend title (and thus these properties) not supported by Google. $chart_definition['options']['legend']['title'] = $chart['#legend_title']; diff --git a/modules/charts_highcharts/charts_highcharts.inc b/modules/charts_highcharts/charts_highcharts.inc index 924e52b..0aaacc9 100644 --- a/modules/charts_highcharts/charts_highcharts.inc +++ b/modules/charts_highcharts/charts_highcharts.inc @@ -184,6 +184,7 @@ function _charts_highcharts_populate_chart_data(&$chart, $chart_definition) { foreach (element_children($chart) as $key) { if ($chart[$key]['#type'] === 'chart_data') { $series = array(); + $series_data = array(); // Make sure defaults are loaded. if (empty($chart[$key]['#defaults_loaded'])) { @@ -208,17 +209,17 @@ function _charts_highcharts_populate_chart_data(&$chart, $chart_definition) { // Allow data to provide the labels. This will override the axis settings. if ($chart[$key]['#labels']) { foreach ($chart[$key]['#labels'] as $label_index => $label) { - $series['data'][$label_index][0] = $label; + $series_data[$label_index][0] = $label; } } // Populate the data. foreach ($chart[$key]['#data'] as $data_index => $data) { - if (isset($series['data'][$data_index])) { - $series['data'][$data_index][] = $data; + if (isset($series_data[$data_index])) { + $series_data[$data_index][] = $data; } else { - $series['data'][$data_index] = $data; + $series_data[$data_index] = $data; } } @@ -227,6 +228,7 @@ function _charts_highcharts_populate_chart_data(&$chart, $chart_definition) { $series['color'] = $chart[$key]['#color']; $series['marker']['radius'] = $chart[$key]['#marker_radius']; $series['showInLegend'] = $chart[$key]['#show_in_legend']; + $series['connectNulls'] = TRUE; $series['tooltip']['valueDecimals'] = $chart[$key]['#decimal_count']; $series['tooltip']['xDateFormat'] = $chart[$key]['#date_format']; @@ -241,8 +243,11 @@ function _charts_highcharts_populate_chart_data(&$chart, $chart_definition) { $chart_definition['yAxis'][$yaxis_index]['labels']['format'] = $chart[$key]['#prefix'] . "{value$decimal_formatting}" . $chart[$key]['#suffix']; } - // Add the series to the main chart definition. + // Remove unnecessary keys to trim down the resulting JS settings. charts_trim_array($series); + $series['data'] = $series_data; + + // Add the series to the main chart definition. $chart_definition['series'][$key] = $series; // Merge in any point-specific data points. diff --git a/views/charts_plugin_style_chart.inc b/views/charts_plugin_style_chart.inc index 472ef5a..c9c5eba 100644 --- a/views/charts_plugin_style_chart.inc +++ b/views/charts_plugin_style_chart.inc @@ -172,7 +172,14 @@ class charts_plugin_style_chart extends views_plugin_style { if ($label_field_key) { $data_row[] = $renders[$row_number][$label_field_key]; } - $data_row[] = (float) $renders[$row_number][$data_field_key]; + // Convert empty strings to NULL. + if ($renders[$row_number][$data_field_key] === '') { + $value = NULL; + } + else { + $value = (float) $renders[$row_number][$data_field_key]; + } + $data_row[] = $value; $data[] = $data_row; } @@ -217,7 +224,14 @@ class charts_plugin_style_chart extends views_plugin_style { $chart['xaxis']['#labels'][] = $renders[$row_number][$label_field_key]; } foreach ($data_fields as $field_key => $field_handler) { - $chart[$this->view->current_display . '__' . $field_key]['#data'][] = (float) $renders[$row_number][$field_key]; + // Convert empty strings to NULL. + if ($renders[$row_number][$field_key] === '') { + $value = NULL; + } + else { + $value = (float) $renders[$row_number][$field_key]; + } + $chart[$this->view->current_display . '__' . $field_key]['#data'][] = $value; } } } -- GitLab