diff --git a/charts.module b/charts.module index 6f7674c..6cd199f 100644 --- a/charts.module +++ b/charts.module @@ -103,6 +103,7 @@ function charts_element_info() { '#max' => NULL, // Integer max value on this axis. '#min' => NULL, // Integer minimum value on this axis. '#opposite' => FALSE, // Display axis on opposite normal side. + '#continuous' => FALSE, // Options: FALSE, TRUE, or 'date'. ); $info['chart_xaxis'] = array( ) + $axis_properties; @@ -413,6 +414,14 @@ function charts_cast_element_integer_values(&$element) { } } +/** + * Given a date value, attempt to standardize the input. + */ +function charts_convert_date_value($date_string) { + $timezone = new DateTimeZone('UTC'); + $date = new DateTime($date_string, $timezone); + return $date->format('U'); +} /** * Output the chart renderable as a string. diff --git a/includes/charts.examples.inc b/includes/charts.examples.inc index 8cd56dd..3220ca5 100644 --- a/includes/charts.examples.inc +++ b/includes/charts.examples.inc @@ -161,6 +161,39 @@ function _charts_examples_bar_simple() { return $example; } +function _charts_examples_line() { + $chart = array( + '#type' => 'chart', + '#chart_type' => 'line', + '#title' => t('Line with discrete axis'), + ); + // Test with a gap in the data. + $chart['male'] = array( + '#type' => 'chart_data', + '#title' => t('Male'), + '#data' => array(array(0, 0), array(20, 40), array(22, 30), array(36, 30), array(40, 40)), + ); + $chart['female'] = array( + '#type' => 'chart_data', + '#title' => t('Female'), + '#data' => array(array(0, 5), array(20, 14), array(22, 16), array(36, 36), array(40, 48)), + ); + + $example['chart'] = $chart; + + return $example; +} + +function _charts_examples_line_continuous() { + $example = _charts_examples_line(); + $example['chart']['#title'] = t('Line with continuous axis'); + $example['chart']['xaxis'] = array( + '#type' => 'chart_xaxis', + '#continuous' => TRUE, + ); + return $example; +} + function _charts_examples_line_gap() { $chart = array( '#type' => 'chart', diff --git a/includes/charts.pages.inc b/includes/charts.pages.inc index 8433966..bdad86c 100644 --- a/includes/charts.pages.inc +++ b/includes/charts.pages.inc @@ -209,6 +209,18 @@ function charts_settings_form($form, $defaults = array(), $field_options = array '#default_value' => $options['xaxis_title'], '#parents' => array_merge($parents, array('xaxis_title')), ); + $form['xaxis']['continuous'] = array( + '#title' => t('Axis data type'), + '#type' => 'radios', + '#options' => array( + 0 => t('Discrete'), + 1 => t('Continuous'), + 2 => t('Continuous date') + ), + '#default_value' => $options['xaxis_continuous'], + '#description' => t('A continuous axis will space points based on their values. See these examples that elaborate on the different axis types.'), + '#parents' => array_merge($parents, array('xaxis_continuous')), + ); $form['xaxis']['labels_rotation'] = array( '#title' => t('Labels rotation'), '#type' => 'select', @@ -371,6 +383,7 @@ function charts_default_settings() { $defaults['height'] = NULL; $defaults['xaxis_title'] = ''; + $defaults['xaxis_continuous'] = 0; $defaults['xaxis_labels_rotation'] = 0; $defaults['yaxis_title'] = ''; diff --git a/modules/charts_google/charts_google.inc b/modules/charts_google/charts_google.inc index 36a83cc..13d15db 100644 --- a/modules/charts_google/charts_google.inc +++ b/modules/charts_google/charts_google.inc @@ -114,7 +114,17 @@ function _charts_google_populate_chart_axes($chart, $chart_definition) { // In Google, the row column of data is used as labels. if ($chart[$key]['#labels'] && $chart[$key]['#type'] === 'chart_xaxis') { foreach ($chart[$key]['#labels'] as $label_key => $label) { - $chart_definition['data'][$label_key + 1][0] = $label; + if ($chart[$key]['#continuous']) { + if ($chart[$key]['#continuous'] === 'date') { + $chart_definition['data'][$label_key + 1][0] = charts_convert_date_value($label); + } + else { + $chart_definition['data'][$label_key + 1][0] = $label + 0; + } + } + else { + $chart_definition['data'][$label_key + 1][0] = $label; + } } } $axis['textStyle']['color'] = $chart[$key]['#labels_color']; diff --git a/modules/charts_highcharts/charts_highcharts.inc b/modules/charts_highcharts/charts_highcharts.inc index 0aaacc9..215715c 100644 --- a/modules/charts_highcharts/charts_highcharts.inc +++ b/modules/charts_highcharts/charts_highcharts.inc @@ -152,6 +152,14 @@ function _charts_highcharts_populate_chart_axes($chart, $chart_definition) { $axis['min'] = $chart[$key]['#min']; $axis['opposite'] = $chart[$key]['#opposite']; + // Change axis_type to datetime and delete categories when needed. + if ($chart[$key]['#continuous']) { + $axis['categories'] = ''; + if ($chart[$key]['#continuous'] === 'date') { + $axis['type'] = 'datetime'; + } + } + // Dealing with axis rotation in a reasonable manner is complicated in // Highcharts. We want the label to be reasonably positioned on the // outside of the chart when labels are rotated. @@ -206,6 +214,22 @@ function _charts_highcharts_populate_chart_data(&$chart, $chart_definition) { $series['yAxis'] = $axis_index; } + // Change data string to number when continuous axis is needed. + foreach (element_children($chart) as $axis_key) { + if ($chart[$axis_key]['#type'] === 'chart_xaxis') { + if (!empty($chart[$axis_key]['#continuous'])) { + foreach ($chart[$axis_key]['#labels'] as $label_index => $label) { + if ($chart[$axis_key]['#continuous'] === 'date') { + $series['data'][$label_index][0] = 'U' . charts_convert_date_value($label); + } + else { + $series['data'][$label_index][0] = $label + 0; + } + } + } + } + } + // Allow data to provide the labels. This will override the axis settings. if ($chart[$key]['#labels']) { foreach ($chart[$key]['#labels'] as $label_index => $label) { diff --git a/views/charts_plugin_style_chart.inc b/views/charts_plugin_style_chart.inc index c9c5eba..44c9375 100644 --- a/views/charts_plugin_style_chart.inc +++ b/views/charts_plugin_style_chart.inc @@ -197,6 +197,7 @@ class charts_plugin_style_chart extends views_plugin_style { $chart['xaxis'] = array( '#type' => 'chart_xaxis', '#title' => $this->options['xaxis_title'] ? $this->options['xaxis_title'] : FALSE, + '#continuous' => $this->options['xaxis_continuous'], '#labels_rotation' => $this->options['xaxis_labels_rotation'], ); $chart['yaxis'] = array(