Skip to content
Snippets Groups Projects
Commit 639a6a74 authored by andileco's avatar andileco
Browse files

Enable donut option for Highcharts and Google Charts (there's still work that...

Enable donut option for Highcharts and Google Charts (there's still work that needs to be done to these for multiple Chart attachments). Also adds a message to the Charts API Example module if the Charts default settings have not been configured.
parent 0a598f01
No related branches found
No related tags found
No related merge requests found
Showing with 92 additions and 27 deletions
......@@ -131,7 +131,7 @@ function hook_charts_info() {
// $chart renderable and printing a chart on the page.
'render' => '_my_charting_library_render',
// Specify the chart types your library is capable of providing.
'types' => ['area', 'bar', 'column', 'line', 'pie', 'scatter'],
'types' => ['area', 'bar', 'column', 'donut', 'line', 'pie', 'scatter'],
// If your callback function is in a separate file, specify it's location.
// 'file' => 'includes/my_charting_library.inc',
];
......
......@@ -18,6 +18,10 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
public function display() {
$library = $this->chartSettings['library'];
if (!isset($library)) {
drupal_set_message('You need to first configure Charts default
settings');
}
$options = [];
$options['type'] = $this->chartSettings['type'];
$options['title'] = $this->t('Chart title');
......@@ -25,7 +29,7 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
$options['yaxis_min'] = '';
$options['yaxis_max'] = '';
$options['xaxis_title'] = $this->t('X-Axis');
//sample data format
// Sample data format.
$categories = ["Category 1", "Category 2", "Category 3", "Category 4"];
$seriesData = [
["name" => "Series 1", "color" => "#0d233a", "type" => null, "data" => [250, 350, 400, 200]],
......
......@@ -13,7 +13,7 @@ use Drupal\charts\Theme\ChartsInterface;
function charts_c3_charts_info() {
$info['c3'] = [
'label' => t('C3 Charts'),
'types' => ['area', 'bar', 'column', 'line', 'pie', 'donut', 'scatter'],
'types' => ['area', 'bar', 'column', 'donut', 'line', 'pie', 'donut', 'scatter'],
];
return $info;
}
......
......@@ -5,9 +5,7 @@
* Charts module integration with Google Charts library.
*/
use Drupal\charts_google\Settings\Google\GoogleOptions;
use Drupal\charts_google\Settings\Google\ChartType;
use Drupal\charts_google\Settings\Google\ChartArea;
use Drupal\charts\Theme\ChartsInterface;
/**
* Implements hook_charts_info().
......@@ -16,10 +14,19 @@ function charts_google_charts_info() {
$info['google'] = [
'label' => t('Google Charts'),
'render' => '_charts_google_render',
'types' => ['area', 'bar', 'column', 'line', 'pie', 'scatter'],
'types' => ['area', 'bar', 'column', 'donut', 'line', 'pie', 'scatter'],
'file' => 'charts_google.inc',
];
return $info;
}
/**
* Implements hook_charts_type_info().
*/
function charts_google_charts_type_info() {
$chart_types['donut'] = [
'label' => t('Donut'),
'axis' => ChartsInterface::CHARTS_SINGLE_AXIS,
];
return $chart_types;
}
......@@ -33,6 +33,9 @@
case 'ColumnChart':
chart = new google.visualization.ColumnChart(document.getElementById(chartId));
break;
case 'DonutChart':
chart = new google.visualization.PieChart(document.getElementById(chartId));
break;
case 'PieChart':
chart = new google.visualization.PieChart(document.getElementById(chartId));
break;
......
......@@ -94,11 +94,14 @@ class GoogleChartsRender implements ChartsRenderInterface {
}
$googleOptions->setTitle($options['title']);
$googleOptions->vAxes = $vAxes;
//$vAxis['title'] = $options['yaxis_title'];
//$googleOptions->setVAxis($vAxis);
if (in_array('donut',$chartSelected)) {
$googleOptions->pieHole = '0.5';
}
$chartArea = new ChartArea();
$chartArea->setWidth(400);
// $googleOptions->setChartArea($chartArea);
$seriesColors = [];
for ($i = 0; $i < count($seriesData); $i++) {
$seriesColor = $seriesData[$i]['color'];
......
......@@ -4,6 +4,8 @@
* Charts module integration with Highcharts library.
*/
use Drupal\charts\Theme\ChartsInterface;
/**
* Implements hook_charts_info().
*/
......@@ -11,9 +13,20 @@ function charts_highcharts_charts_info() {
$info['highcharts'] = [
'label' => t('Highcharts'),
'render' => '_charts_highcharts_render',
'types' => ['area', 'bar', 'column', 'line', 'pie', 'scatter'],
'types' => ['area', 'bar', 'column', 'donut', 'line', 'pie', 'scatter'],
'file' => 'charts_highcharts.inc',
];
return $info;
}
/**
* Implements hook_charts_type_info().
*/
function charts_highcharts_charts_type_info() {
$chart_types['donut'] = [
'label' => t('Donut'),
'axis' => ChartsInterface::CHARTS_SINGLE_AXIS,
];
return $chart_types;
}
......@@ -38,9 +38,28 @@ class HighchartsChartsRender implements ChartsRenderInterface {
* @return Highcharts object to be used by highcharts javascripts visualization framework
*/
public function charts_render_charts($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) {
$noAttachmentDisplays = count($attachmentDisplayOptions) === 0;
$chart = new ChartType();
$chart->setType($options['type']);
$typeOptions = $options['type'];
// @todo: make this so that it happens if any display uses donut.
if ($typeOptions == 'donut'){
$typeOptions = 'pie';
// Remove donut from seriesData.
foreach ($seriesData as $key => &$value) {
$value = str_replace('donut','pie',$value);
}
// Add innerSize to differentiate between donut and pie.
foreach ($seriesData as $key => &$value) {
if ($typeOptions == 'pie') {
$innerSize['showInLegend'] = 'true';
$innerSize['innerSize'] = '40%';
$chartPlacement = array_search($value, $seriesData);
$seriesData[$chartPlacement] = array_merge($innerSize, $seriesData[$chartPlacement]);
}
}
}
$chart->setType($typeOptions);
$chartTitle = new ChartTitle();
$chartTitle->setText($options['title']);
$chartXaxis = new Xaxis();
......@@ -67,14 +86,13 @@ class HighchartsChartsRender implements ChartsRenderInterface {
$chartYaxis->setTitle($yAxisTitle);
array_push($yAxes, $chartYaxis);
// Chart libraries tend to supports only one secondary axis.
if ($attachmentDisplayOptions[0]['inherit_yaxis'] == 0) {
if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) {
$chartYaxisSecondary = new Yaxis();
$yAxisTitleSecondary = new YaxisTitle();
$yAxisTitleSecondary->setText($attachmentDisplayOptions[0]['style']['options']['yaxis_title']);
$chartYaxisSecondary->setTitle($yAxisTitleSecondary);
$chartYaxisSecondary->setLabels($yaxisLabels);
$chartYaxisSecondary->opposite = 'true';
if (!empty($attachmentDisplayOptions[0]['style']['options']['yaxis_min'])) {
$chartYaxisSecondary->min = $attachmentDisplayOptions[0]['style']['options']['yaxis_min'];
}
......@@ -86,8 +104,8 @@ class HighchartsChartsRender implements ChartsRenderInterface {
$dataLabelStatus = new DataLabelStatus();
$dataLabels = new DataLabels();
$dataLabels->setDataLabels($dataLabelStatus);
$barPlotOptns = new PlotOptions();
$barPlotOptns->setBar($dataLabels);
$plotOptions = new PlotOptions();
$plotOptions->setPlotType($dataLabels);
$chartTooltip = new Tooltip();
$chartCredits = new ChartCredits();
$chartLegend = new ChartLegend();
......@@ -96,10 +114,9 @@ class HighchartsChartsRender implements ChartsRenderInterface {
$highchart->setChart($chart);
$highchart->setTitle($chartTitle);
$highchart->setXAxis($chartXaxis);
//$highchart->setYAxis($chartYaxis);
$highchart->yAxis = $yAxes;
$highchart->setTooltip($chartTooltip);
$highchart->setPlotOptions($barPlotOptns);
$highchart->setPlotOptions($plotOptions);
$highchart->setCredits($chartCredits);
$highchart->setLegend($chartLegend);
$highchart->setSeries($seriesData);
......
......@@ -25,6 +25,10 @@ class ChartType implements \JsonSerializable {
public function jsonSerialize() {
$vars = get_object_vars($this);
if ($vars['type'] == 'pie' || $vars['type'] == 'donut') {
unset($vars['x']);
}
return $vars;
}
......
......@@ -11,6 +11,8 @@ class Highcharts implements \JsonSerializable {
private $plotOptions;
private $legend;
private $credits;
private $innerSize = '%20';
private $series;
/**
* @return mixed
......@@ -124,6 +126,20 @@ class Highcharts implements \JsonSerializable {
$this->credits = $credits;
}
/**
* @return mixed
*/
public function getInnerSize() {
return $this->innerSize;
}
/**
* @param mixed $innerSize
*/
public function setInnerSize($innerSize) {
$this->innerSize = $innerSize;
}
/**
* @return mixed
*/
......@@ -138,8 +154,6 @@ class Highcharts implements \JsonSerializable {
$this->series = $series;
}
private $series;
/**
* @return array
*/
......
......@@ -3,20 +3,20 @@
namespace Drupal\charts_highcharts\Settings\Highcharts;
class PlotOptions implements \JsonSerializable {
private $bar;
private $plotType;
/**
* @return mixed
*/
public function getBar() {
return $this->bar;
public function getPlotType() {
return $this->plotType;
}
/**
* @param mixed $bar
* @param mixed $plotType
*/
public function setBar($bar) {
$this->bar = $bar;
public function setPlotType($plotType) {
$this->plotType = $plotType;
}
/**
......
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