public function Google::buildVariables in Charts 8.3
Creates a JSON Object formatted for Google Charts JavaScript to use.
Parameters
array $options: Options.
array $categories: Categories.
array $seriesData: Series data.
array $attachmentDisplayOptions: Attachment display options.
array $variables: Variables.
string $chartId: Chart ID.
array $customOptions: Overrides.
Overrides ChartInterface::buildVariables
File
- modules/
charts_google/ src/ Plugin/ chart/ Google.php, line 59
Class
- Define a concrete class for a Chart.
Namespace
Drupal\charts_google\Plugin\chartCode
public function buildVariables(array $options, array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables, $chartId, array $customOptions = []) {
$categoriesCount = count($categories);
$seriesCount = count($seriesData);
// Creates an array of the length of the series data.
$dataCount = [];
for ($x = 0; $x < $seriesCount; $x++) {
$dataCountTemp = count($seriesData[$x]['data']);
array_push($dataCount, $dataCountTemp);
}
/**
* 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
* categories. If only one data field is selected, then the label field
* will serve as the categories. If multiple data fields are selected,
* they will become the categories.
* */
if ($options['type'] == 'pie' || $options['type'] == 'donut') {
if ($seriesCount > 1) {
$dataTable = [];
for ($j = 0; $j < $seriesCount; $j++) {
$rowDataTable = [];
$rowDataTabletemp = array_sum($seriesData[$j]['data']);
array_push($rowDataTable, $rowDataTabletemp);
array_unshift($rowDataTable, $seriesData[$j]['name']);
array_push($dataTable, $rowDataTable);
}
$dataTableHeader = [
'label',
'value',
];
array_unshift($dataTable, $dataTableHeader);
}
else {
$dataTable = [];
foreach ($categories as $j => $category) {
$rowDataTable = [];
for ($i = 0; $i < $seriesCount; $i++) {
$rowDataTabletemp = $seriesData[$i]['data'][$j];
array_push($rowDataTable, $rowDataTabletemp);
}
array_unshift($rowDataTable, $categories[$j]);
array_push($dataTable, $rowDataTable);
}
$dataTableHeader = [];
for ($r = 0; $r < $seriesCount; $r++) {
array_push($dataTableHeader, $seriesData[$r]['name']);
}
array_unshift($dataTableHeader, 'label');
array_unshift($dataTable, $dataTableHeader);
}
}
elseif ($options['type'] == 'scatter') {
// You will want to use the Scatter Field in the charts_fields module.
$dataTable = [];
foreach ($categories as $j => $category) {
$rowDataTable = [];
for ($i = 0; $i < $seriesCount; $i++) {
// @todo: make work for multiple series.
$rowDataTabletemp[0] = $seriesData[$i]['data'][$j][0];
$rowDataTabletemp[1] = $seriesData[$i]['data'][$j][1];
$rowDataTabletemp[2] = $categories[$j] . ': ' . json_encode($seriesData[$i]['data'][$j]);
$rowDataTable = $rowDataTabletemp;
}
array_push($dataTable, $rowDataTable);
}
$dataTableHeader = [];
for ($r = 0; $r < $seriesCount; $r++) {
array_push($dataTableHeader, $seriesData[$r]['name']);
}
$role = new \stdClass();
$role->role = 'tooltip';
array_push($dataTableHeader, $role);
array_unshift($dataTableHeader, 'label');
array_unshift($dataTable, $dataTableHeader);
}
else {
$dataTable = [];
foreach ($categories as $j => $category) {
$rowDataTable = [];
for ($i = 0; $i < $seriesCount; $i++) {
if (isset($seriesData[$i]['data'][$j])) {
$rowDataTabletemp = $seriesData[$i]['data'][$j];
array_push($rowDataTable, $rowDataTabletemp);
}
else {
$rowDataTabletemp = 0;
array_push($rowDataTable, $rowDataTabletemp);
}
}
array_unshift($rowDataTable, $categories[$j]);
array_push($dataTable, $rowDataTable);
}
$dataTableHeader = [];
for ($r = 0; $r < $seriesCount; $r++) {
array_push($dataTableHeader, $seriesData[$r]['name']);
}
array_unshift($dataTableHeader, 'label');
array_unshift($dataTable, $dataTableHeader);
}
$googleOptions = $this
->createChartsOptions($options, $seriesData, $attachmentDisplayOptions);
$googleChartType = $this
->createChartType($options);
// Override Google classes. These will only override what is in
// charts_google/src/Settings/Google/GoogleOptions.php
// but you can use more of the Google Charts API, as you are not constrained
// to what is in this class. See:
// charts_google/src/Plugin/override/GoogleOverrides.php
foreach ($customOptions as $option => $key) {
$setter = 'set' . ucfirst($option);
if (method_exists($googleOptions, $setter)) {
$googleOptions
->{$setter}($customOptions[$option]);
}
}
$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);
}