Skip to content
Snippets Groups Projects
Commit 037aa55e authored by andileco's avatar andileco
Browse files

Add charts render interface.

parent 7f150408
No related branches found
No related tags found
No related merge requests found
...@@ -40,9 +40,6 @@ define('CHARTS_SINGLE_AXIS', 'y_only'); ...@@ -40,9 +40,6 @@ define('CHARTS_SINGLE_AXIS', 'y_only');
*/ */
define('CHARTS_DUAL_AXIS', 'xy'); define('CHARTS_DUAL_AXIS', 'xy');
// Store Charts preprocess theme functions in a separate .inc file.
\Drupal::moduleHandler()->loadInclude('charts', 'inc', 'charts.theme');
/** /**
* Retrieve a list of all charting libraries available. * Retrieve a list of all charting libraries available.
* *
...@@ -611,7 +608,7 @@ function charts_default_settings_form($form, $form_state) { ...@@ -611,7 +608,7 @@ function charts_default_settings_form($form, $form_state) {
*/ */
function charts_default_settings_form_submit($form, $form_state) { function charts_default_settings_form_submit($form, $form_state) {
\Drupal::state() \Drupal::state()
->set('charts_default_settings', $form_state['values']['charts_default_settings']); //was variable_set ->set('charts_default_settings', $form_state['values']['charts_default_settings']);
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\charts_c3\Charts; namespace Drupal\charts_c3\Charts;
use Drupal\charts\Charts\ChartsRenderInterface;
use Drupal\charts\Util\Util; use Drupal\charts\Util\Util;
use Drupal\charts_c3\Settings\CThree\ChartType; use Drupal\charts_c3\Settings\CThree\ChartType;
use Drupal\charts_c3\Settings\CThree\CThree; use Drupal\charts_c3\Settings\CThree\CThree;
...@@ -10,16 +11,10 @@ use Drupal\charts_c3\Settings\CThree\ChartData; ...@@ -10,16 +11,10 @@ use Drupal\charts_c3\Settings\CThree\ChartData;
use Drupal\charts_c3\Settings\CThree\ChartColor; use Drupal\charts_c3\Settings\CThree\ChartColor;
use Drupal\charts_c3\Settings\CThree\ChartAxis; use Drupal\charts_c3\Settings\CThree\ChartAxis;
class C3ChartsRender { class C3ChartsRender implements ChartsRenderInterface {
public function __construct($categories, $seriesData, $options, $attachmentDisplayOptions, &$variables, $chartId) {
public function __construct() {
Util::checkMissingLibrary('charts_c3', '/vendor/cthree/c3.min.js'); Util::checkMissingLibrary('charts_c3', '/vendor/cthree/c3.min.js');
$c3 = $this->charts_c3_render_charts($options, $categories, $seriesData, $chartId, $attachmentDisplayOptions);
$variables['chart_type'] = 'c3';
$variables['content_attributes']['data-chart'][] = json_encode($c3);
$variables['attributes']['id'][0] = $chartId;
$variables['attributes']['class'][] = 'charts-c3';
} }
/** /**
...@@ -31,7 +26,7 @@ class C3ChartsRender { ...@@ -31,7 +26,7 @@ class C3ChartsRender {
* *
* @return CThree * @return CThree
*/ */
private function charts_c3_render_charts($options, $categories = [], $seriesData = [], $chartId, $attachmentDisplayOptions = []) { public function charts_render_charts($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) {
$noAttachmentDisplays = count($attachmentDisplayOptions); $noAttachmentDisplays = count($attachmentDisplayOptions);
$yAxis = []; $yAxis = [];
...@@ -108,7 +103,9 @@ class C3ChartsRender { ...@@ -108,7 +103,9 @@ class C3ChartsRender {
$chartColor->setPattern($seriesColors); $chartColor->setPattern($seriesColors);
$c3->setColor($chartColor); $c3->setColor($chartColor);
return $c3; $variables['chart_type'] = 'c3';
$variables['content_attributes']['data-chart'][] = json_encode($c3);
$variables['attributes']['id'][0] = $chartId;
$variables['attributes']['class'][] = 'charts-c3';
} }
} }
...@@ -2,30 +2,16 @@ ...@@ -2,30 +2,16 @@
namespace Drupal\charts_google\Charts; namespace Drupal\charts_google\Charts;
use Drupal\charts\Charts\ChartsRenderInterface;
use Drupal\charts\Util\Util; use Drupal\charts\Util\Util;
use Drupal\charts_google\Settings\Google\GoogleOptions; use Drupal\charts_google\Settings\Google\GoogleOptions;
use Drupal\charts_google\Settings\Google\ChartType; use Drupal\charts_google\Settings\Google\ChartType;
use Drupal\charts_google\Settings\Google\ChartArea; use Drupal\charts_google\Settings\Google\ChartArea;
class GoogleChartsRender { class GoogleChartsRender implements ChartsRenderInterface {
private $googleData; public function __construct() {
private $googleOptions;
private $googleChartType;
private $chartId;
public function __construct($categories, $seriesData, $options, $attachmentDisplayOptions, &$variables, $chartId) {
Util::checkMissingLibrary('charts_google', '/vendor/google/loader.js'); Util::checkMissingLibrary('charts_google', '/vendor/google/loader.js');
$this->chartId = $chartId;
$this->googleData = $this->charts_google_render_charts($categories, $seriesData);
$this->googleOptions = $this->charts_google_create_charts_options($options, $seriesData, $attachmentDisplayOptions);
$this->googleChartType = $this->charts_google_create_chart_type($options);
$variables['chart_type'] = 'google';
$variables['attributes']['class'][0] = 'charts-google';
$variables['attributes']['id'][0] = $this->chartId;
$variables['content_attributes']['data-chart'][] = $this->googleData;
$variables['attributes']['google-options'][1] = json_encode($this->googleOptions);
$variables['attributes']['google-chart-type'][2] = json_encode($this->googleChartType);
} }
/** /**
...@@ -35,7 +21,7 @@ class GoogleChartsRender { ...@@ -35,7 +21,7 @@ class GoogleChartsRender {
* *
* @return json|string * @return json|string
*/ */
private function charts_google_render_charts($categories = [], $seriesData = []) { public function charts_render_charts($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) {
$dataTable = []; $dataTable = [];
for ($j = 0; $j < count($categories); $j++) { for ($j = 0; $j < count($categories); $j++) {
...@@ -56,7 +42,14 @@ class GoogleChartsRender { ...@@ -56,7 +42,14 @@ class GoogleChartsRender {
array_unshift($dataTableHeader, 'label'); array_unshift($dataTableHeader, 'label');
array_unshift($dataTable, $dataTableHeader); array_unshift($dataTable, $dataTableHeader);
return json_encode($dataTable); $googleOptions = $this->charts_google_create_charts_options($options, $seriesData, $attachmentDisplayOptions);
$googleChartType = $this->charts_google_create_chart_type($options);
$variables['chart_type'] = 'google';
$variables['attributes']['class'][0] = 'charts-google';
$variables['attributes']['id'][0] = $chartId;
$variables['content_attributes']['data-chart'][] = json_encode($dataTable);
$variables['attributes']['google-options'][1] = json_encode($googleOptions);
$variables['attributes']['google-chart-type'][2] = json_encode($googleChartType);
} }
/** /**
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\charts_highcharts\Charts; namespace Drupal\charts_highcharts\Charts;
use Drupal\charts\Charts\ChartsRenderInterface;
use Drupal\charts\Util\Util; use Drupal\charts\Util\Util;
use Drupal\charts_highcharts\Settings\Highcharts\ChartType; use Drupal\charts_highcharts\Settings\Highcharts\ChartType;
use Drupal\charts_highcharts\Settings\Highcharts\ChartTitle; use Drupal\charts_highcharts\Settings\Highcharts\ChartTitle;
...@@ -18,17 +19,10 @@ use Drupal\charts_highcharts\Settings\Highcharts\ChartCredits; ...@@ -18,17 +19,10 @@ use Drupal\charts_highcharts\Settings\Highcharts\ChartCredits;
use Drupal\charts_highcharts\Settings\Highcharts\ChartLegend; use Drupal\charts_highcharts\Settings\Highcharts\ChartLegend;
use Drupal\charts_highcharts\Settings\Highcharts\Highcharts; use Drupal\charts_highcharts\Settings\Highcharts\Highcharts;
class HighchartsChartsRender { class HighchartsChartsRender implements ChartsRenderInterface {
public function __construct($categories, $seriesData, $options, $attachmentDisplayOptions, &$variables, $chartId) {
public function __construct() {
Util::checkMissingLibrary('charts_highcharts', '/vendor/highcharts/highcharts.js'); Util::checkMissingLibrary('charts_highcharts', '/vendor/highcharts/highcharts.js');
$highchart = $this->charts_highcharts_render_charts($options, $categories, $seriesData, $attachmentDisplayOptions);
$variables['chart_type'] = 'highcharts';
$variables['content_attributes']['data-chart'][] = json_encode($highchart);
$variables['attributes']['id'][0] = $chartId;
$variables['attributes']['class'][] = 'charts-highchart';
} }
/** /**
...@@ -42,7 +36,7 @@ class HighchartsChartsRender { ...@@ -42,7 +36,7 @@ class HighchartsChartsRender {
* *
* @return Highcharts object to be used by highcharts javascripts visualization framework * @return Highcharts object to be used by highcharts javascripts visualization framework
*/ */
private function charts_highcharts_render_charts($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = []) { public function charts_render_charts($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) {
$chart = new ChartType(); $chart = new ChartType();
$chart->setType($options['type']); $chart->setType($options['type']);
...@@ -108,6 +102,9 @@ class HighchartsChartsRender { ...@@ -108,6 +102,9 @@ class HighchartsChartsRender {
$highchart->setLegend($chartLegend); $highchart->setLegend($chartLegend);
$highchart->setSeries($seriesData); $highchart->setSeries($seriesData);
return $highchart; $variables['chart_type'] = 'highcharts';
$variables['content_attributes']['data-chart'][] = json_encode($highchart);
$variables['attributes']['id'][0] = $chartId;
$variables['attributes']['class'][] = 'charts-highchart';
} }
} }
<?php
/**
* @file
*
*/
namespace Drupal\charts\Charts;
interface ChartsRenderInterface {
public function charts_render_charts($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId);
}
\ No newline at end of file
...@@ -34,7 +34,8 @@ class ModuleSelector { ...@@ -34,7 +34,8 @@ class ModuleSelector {
if ('charts_' . $moduleExist) { if ('charts_' . $moduleExist) {
$className = ucfirst($moduleName); $className = ucfirst($moduleName);
$moduleChartsRenderer = 'Drupal\charts_' . $moduleName . '\Charts\\' . ucfirst($moduleName) . 'ChartsRender'; $moduleChartsRenderer = 'Drupal\charts_' . $moduleName . '\Charts\\' . ucfirst($moduleName) . 'ChartsRender';
$chartingModule = new $moduleChartsRenderer($this->categories, $this->seriesData, $this->options, $this->attachmentDisplayOptions, $variables, $this->chartId); $chartingModule = new $moduleChartsRenderer();
$chartingModule->charts_render_charts($this->options, $this->categories, $this->seriesData, $this->attachmentDisplayOptions, $variables, $this->chartId);
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment