View source
<?php
namespace Drupal\charts_c3\Plugin\chart;
use Drupal\charts\Plugin\chart\AbstractChart;
use Drupal\charts_c3\Settings\CThree\ChartGauge;
use Drupal\charts_c3\Settings\CThree\ChartPoints;
use Drupal\charts_c3\Settings\CThree\ChartType;
use Drupal\charts_c3\Settings\CThree\CThree;
use Drupal\charts_c3\Settings\CThree\ChartTitle;
use Drupal\charts_c3\Settings\CThree\ChartData;
use Drupal\charts_c3\Settings\CThree\ChartColor;
use Drupal\charts_c3\Settings\CThree\ChartAxis;
use Drupal\charts_c3\Settings\CThree\ChartLegend;
class C3 extends AbstractChart {
public function processChartType($type) {
if ($type == 'column') {
$type = 'bar';
}
return $type;
}
public function buildVariables(array $options, array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables, $chartId, array $customOptions = []) {
$c3 = new CThree();
$seriesCount = count($seriesData);
$attachmentCount = count($attachmentDisplayOptions);
$noAttachmentDisplays = $attachmentCount === 0;
$c3Chart = new ChartType();
$c3Chart
->setType($options['type']);
$c3ChartTitle = new ChartTitle();
$c3ChartTitle
->setText($options['title']);
$c3
->setTitle($c3ChartTitle);
$chartData = new ChartData();
$chartData
->setType($options['type']);
$c3
->setData($chartData);
$chartAxis = new ChartAxis();
if ($options['type'] == 'pie' || $options['type'] == 'donut') {
$chartColor = new ChartColor();
$seriesColors = [];
if ($seriesCount > 1) {
$c3Data = [];
for ($i = 0; $i < $seriesCount; $i++) {
$c3DataTemp = $seriesData[$i]['data'];
array_unshift($c3DataTemp, $seriesData[$i]['name']);
array_push($c3Data, $c3DataTemp);
$seriesColor = $seriesData[$i]['color'];
array_push($seriesColors, $seriesColor);
}
}
else {
$c3Data = [];
for ($i = 0; $i < count($seriesData[0]['data']); $i++) {
$c3DataTemp = $seriesData[0]['data'][$i];
$c3SeriesDataTemp = array_merge([
$categories[$i],
], [
$c3DataTemp,
]);
array_push($c3Data, $c3SeriesDataTemp);
}
}
$chartData
->setColumns($c3Data);
$chartColor
->setPattern($seriesColors);
$c3
->setColor($chartColor);
}
else {
$chartColor = new ChartColor();
$seriesColors = [];
for ($i = 0; $i < $seriesCount; $i++) {
$seriesColor = $seriesData[$i]['color'];
array_push($seriesColors, $seriesColor);
}
$chartColor
->setPattern($seriesColors);
$c3
->setColor($chartColor);
$c3Data = [];
for ($i = 0; $i < $seriesCount; $i++) {
$c3DataTemp = $seriesData[$i]['data'];
array_unshift($c3DataTemp, $seriesData[$i]['name']);
array_push($c3Data, $c3DataTemp);
}
if ($options['type'] == 'bar') {
$chartAxis
->setRotated(TRUE);
}
elseif ($options['type'] == 'column') {
$chartData
->setType('bar');
$chartAxis
->setRotated(FALSE);
}
if ($options['type'] == 'scatter') {
$fieldLabel = $seriesData[0]['name'];
array_shift($c3Data[0]);
$scatterFieldData = $c3Data[0];
$scatterFieldX = [];
$scatterFieldY = [];
for ($i = 0; $i < count($scatterFieldData); $i++) {
array_push($scatterFieldX, $scatterFieldData[$i][0]);
array_push($scatterFieldY, $scatterFieldData[$i][1]);
}
array_unshift($scatterFieldX, $fieldLabel . '_x');
array_unshift($scatterFieldY, $fieldLabel);
$c3Data[0] = $scatterFieldX;
$c3Data[1] = $scatterFieldY;
$chartAxis
->setX([
'tick' => [
'fit' => FALSE,
],
]);
$xs = new \stdClass();
$xs->{$fieldLabel} = $fieldLabel . '_x';
$chartData
->setXs($xs);
$chartData
->setX('');
}
else {
array_unshift($categories, 'x');
array_push($c3Data, $categories);
}
$chartData
->setColumns($c3Data);
$c3
->setAxis($chartAxis);
}
$types = [];
for ($i = 0; $i < $seriesCount; $i++) {
$types[$seriesData[$i]['name']] = $this
->processChartType($seriesData[$i]['type']);
}
$chartData->types = $types;
if (empty($options['data_labels'])) {
$chartData
->setLabels(FALSE);
}
$showAxis['show'] = TRUE;
$showAxis['label'] = $options['yaxis_title'];
$chartAxis->y = $showAxis;
if (!$noAttachmentDisplays && isset($attachmentDisplayOptions[0]['inherit_yaxis']) && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) {
$showSecAxis['show'] = TRUE;
$showSecAxis['label'] = $attachmentDisplayOptions[0]['style']['options']['yaxis_title'];
$chartAxis->y2 = $showSecAxis;
}
if (isset($options['grouping']) && $options['grouping'] === TRUE) {
$seriesNames = [];
for ($i = 0; $i < $seriesCount; $i++) {
array_push($seriesNames, $seriesData[$i]['name']);
}
$chartData
->setGroups([
$seriesNames,
]);
}
if ($options['type'] == 'gauge') {
$gauge = new ChartGauge();
$gauge
->setMin((int) $options['min']);
$gauge
->setMax((int) $options['max']);
$gauge
->setUnits($options['yaxis_suffix']);
$c3
->setGauge($gauge);
}
if ($options['type'] == 'line' || $options['type'] == 'spline') {
$points = new ChartPoints();
if ($options['data_markers'] == 'FALSE') {
$points
->setShow(FALSE);
}
else {
$points
->setShow(TRUE);
}
$c3
->setPoint($points);
}
$legend = new ChartLegend();
if (empty($options['legend_position'])) {
$legend
->setShow(FALSE);
}
else {
$legend
->setShow(TRUE);
}
$c3
->setLegend($legend);
$bindTo = '#' . $chartId;
$c3
->setBindTo($bindTo);
foreach ($customOptions as $option => $key) {
$setter = 'set' . ucfirst($option);
if (method_exists($c3, $setter)) {
$c3
->{$setter}($customOptions[$option]);
}
}
$variables['chart_type'] = 'c3';
$variables['content_attributes']['data-chart'][] = json_encode($c3);
$variables['attributes']['id'][0] = $chartId;
$variables['attributes']['class'][] = 'charts-c3';
}
}