Skip to content
Snippets Groups Projects
Commit 242ef85b authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Clean up C3 class

parent a32f7531
No related branches found
No related tags found
No related merge requests found
......@@ -47,6 +47,7 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
'yaxis_min' => '',
'yaxis_max' => '',
'three_dimensional' => FALSE,
//'grouping' => TRUE,
];
// Sample data format.
......
......@@ -37,11 +37,16 @@ class C3 extends AbstractChart {
* Chart Id.
*/
public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) {
$noAttachmentDisplays = count($attachmentDisplayOptions) === 0;
$types = [];
// @todo - make this work for more that one attachment.
for ($i = 1; $i <= count($attachmentDisplayOptions); $i++) {
// Create new instance of CThree.
$c3 = new CThree();
$seriesCount = count($seriesData);
$attachmentCount = count($attachmentDisplayOptions);
$noAttachmentDisplays = $attachmentCount === 0;
$types = [];
for ($i = 1; $i <= $attachmentCount; $i++) {
if ($attachmentDisplayOptions[$i - 1]['style']['options']['type'] == 'column') {
$types[$seriesData[$i]['name']] = 'bar';
}
......@@ -50,6 +55,21 @@ class C3 extends AbstractChart {
}
}
// Set the chart type.
$c3Chart = new ChartType();
$c3Chart->setType($options['type']);
// Set the chart title.
$c3ChartTitle = new ChartTitle();
$c3ChartTitle->setText($options['title']);
$c3->setTitle($c3ChartTitle);
// Set up the chart data object.
$chartData = new ChartData();
$chartData->setType($options['type']);
$c3->setData($chartData);
$chartAxis = new ChartAxis();
/**
* For pie and donut chart types, depending on the number of data fields,
* the charts will either use data fields or label fields for the
......@@ -58,12 +78,17 @@ class C3 extends AbstractChart {
* they will become the categories.
* */
if ($options['type'] == 'pie' || $options['type'] == 'donut') {
if (count($seriesData) > 1) {
// Set the charts colors.
$chartColor = new ChartColor();
$seriesColors = [];
if ($seriesCount > 1) {
$c3Data = [];
for ($i = 0; $i < count($seriesData); $i++) {
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 = [];
......@@ -74,26 +99,53 @@ class C3 extends AbstractChart {
}
array_pop($c3Data);
}
$chartData->setColumns($c3Data);
$chartColor->setPattern($seriesColors);
$c3->setColor($chartColor);
}
else {
// Set the charts colors.
$chartColor = new ChartColor();
$seriesColors = [];
for ($i = 0; $i < $seriesCount; $i++) {
$seriesColor = $seriesData[$i]['color'];
array_push($seriesColors, $seriesColor);
}
$chartColor->setPattern($seriesColors);
$c3->setColor($chartColor);
// Set up the chart data object.
$c3Data = [];
for ($i = 0; $i < count($seriesData); $i++) {
for ($i = 0; $i < $seriesCount; $i++) {
$c3DataTemp = $seriesData[$i]['data'];
array_unshift($c3DataTemp, $seriesData[$i]['name']);
array_push($c3Data, $c3DataTemp);
}
// C3 does not use bar, so column must be used.
if ($options['type'] == 'bar') {
$chartAxis->setRotated(TRUE);
array_unshift($categories, 'x');
array_push($c3Data, $categories);
$chartData->setColumns($c3Data);
}
elseif ($options['type'] == 'column') {
$chartData->setType('bar');
$chartAxis->setRotated(FALSE);
array_unshift($categories, 'x');
array_push($c3Data, $categories);
$chartData->setColumns($c3Data);
}
else {
array_unshift($categories, 'x');
array_push($c3Data, $categories);
$chartData->setColumns($c3Data);
}
$c3->setAxis($chartAxis);
}
// Set the chart types.
$chartData->types = $types;
$c3Chart = new ChartType();
$c3Chart->setType($options['type']);
$c3ChartTitle = new ChartTitle();
$c3ChartTitle->setText($options['title']);
$chartAxis = new ChartAxis();
$c3 = new CThree();
$bindTo = '#' . $chartId;
$c3->setBindTo($bindTo);
$c3->setTitle($c3ChartTitle);
$chartData = new ChartData();
// Set labels to FALSE if has attachments.
if ($noAttachmentDisplays > 0) {
$chartData->setLabels(FALSE);
}
......@@ -110,55 +162,17 @@ class C3 extends AbstractChart {
$chartAxis->y2 = $showSecAxis;
}
// Sets the chart type.
$chartData->setType($options['type']);
$c3->setData($chartData);
if ($options['type'] == 'bar') {
$chartAxis->setRotated(TRUE);
array_unshift($categories, 'x');
array_push($c3Data, $categories);
$chartData->setColumns($c3Data);
}
elseif ($options['type'] == 'column') {
$chartData->setType('bar');
$chartAxis->setRotated(FALSE);
array_unshift($categories, 'x');
array_push($c3Data, $categories);
$chartData->setColumns($c3Data);
}
elseif ($options['type'] == 'pie' || $options['type'] == 'donut') {
$chartData->setColumns($c3Data);
}
else {
array_unshift($categories, 'x');
array_push($c3Data, $categories);
$chartData->setColumns($c3Data);
}
$chartData->types = $types;
if ($options['type'] != 'pie' && $options['type'] != 'donut') {
$c3->setAxis($chartAxis);
}
// Determines if chart is stacked.
if (!empty($options['grouping'] && $options['grouping'] == TRUE)) {
$seriesNames = [];
for ($i = 0; $i < count($seriesData); $i++) {
for ($i = 0; $i < $seriesCount; $i++) {
array_push($seriesNames, $seriesData[$i]['name']);
}
$chartData->setGroups([$seriesNames]);
}
$chartColor = new ChartColor();
$seriesColors = [];
for ($i = 0; $i < count($seriesData); $i++) {
$seriesColor = $seriesData[$i]['color'];
array_push($seriesColors, $seriesColor);
}
$chartColor->setPattern($seriesColors);
$c3->setColor($chartColor);
$bindTo = '#' . $chartId;
$c3->setBindTo($bindTo);
$variables['chart_type'] = 'c3';
$variables['content_attributes']['data-chart'][] = json_encode($c3);
$variables['attributes']['id'][0] = $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