class Billboard in Charts 8.3
Define a concrete class for a Chart.
Plugin annotation
@Chart(
id = "billboard",
name = @Translation("Billboard.js")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\charts\Plugin\chart\AbstractChart implements ChartInterface, ContainerFactoryPluginInterface uses StringTranslationTrait
- class \Drupal\charts_billboard\Plugin\chart\Billboard
- class \Drupal\charts\Plugin\chart\AbstractChart implements ChartInterface, ContainerFactoryPluginInterface uses StringTranslationTrait
Expanded class hierarchy of Billboard
File
- modules/
charts_billboard/ src/ Plugin/ chart/ Billboard.php, line 24
Namespace
Drupal\charts_billboard\Plugin\chartView source
class Billboard extends AbstractChart {
/**
* Process the chart type.
*
* @param array $options
* The chart options, which includes the type.
*
* @return string $type
* The chart type.
*/
public function processChartType($options) {
if ($options['type'] == 'column') {
$type = 'bar';
}
else {
$type = $options['type'];
}
// If Polar is checked, then convert to Radar chart type.
if (isset($options['polar']) && $options['polar'] == 1) {
$type = 'radar';
}
return $type;
}
/**
* Creates a JSON Object formatted for Billboard.js Charts JavaScript to use.
*
* @param array $options
* Options.
* @param array $categories
* Categories.
* @param array $seriesData
* Series data.
* @param array $attachmentDisplayOptions
* Attachment display options.
* @param array $variables
* Variables.
* @param string $chartId
* Chart ID.
* @param array $customOptions
* Overrides.
*/
public function buildVariables(array $options, array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables, $chartId, array $customOptions = []) {
// Create new instance of CThree.
$bb = new BillboardChart();
$seriesCount = count($seriesData);
$attachmentCount = count($attachmentDisplayOptions);
$noAttachmentDisplays = $attachmentCount === 0;
$types = [];
// Set the chart type.
$type = $this
->processChartType($options);
$bbChart = new ChartType();
$bbChart
->setType($type);
// Set the chart title.
$bbChartTitle = new ChartTitle();
$bbChartTitle
->setText($options['title']);
$bb
->setTitle($bbChartTitle);
// Set up the chart data object.
$chartData = new ChartData();
$chartData
->setType($type);
$bb
->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
* 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 ($type == 'pie' || $type == 'donut') {
// Set the charts colors.
$chartColor = new ChartColor();
$seriesColors = [];
if ($seriesCount > 1) {
$bbData = [];
for ($i = 0; $i < $seriesCount; $i++) {
$bbDataTemp = $seriesData[$i]['data'];
array_unshift($bbDataTemp, $seriesData[$i]['name']);
array_push($bbData, $bbDataTemp);
$seriesColor = $seriesData[$i]['color'];
array_push($seriesColors, $seriesColor);
}
}
else {
$bbData = [];
for ($i = 0; $i < count($seriesData[0]['data']); $i++) {
$bbDataTemp = $seriesData[0]['data'][$i];
$bbSeriesDataTemp = array_merge([
$categories[$i],
], [
$bbDataTemp,
]);
array_push($bbData, $bbSeriesDataTemp);
}
}
$chartData
->setColumns($bbData);
$chartColor
->setPattern($seriesColors);
$bb
->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);
$bb
->setColor($chartColor);
// Set up the chart data object.
$bbData = [];
for ($i = 0; $i < $seriesCount; $i++) {
$bbDataTemp = $seriesData[$i]['data'];
array_unshift($bbDataTemp, $seriesData[$i]['name']);
array_push($bbData, $bbDataTemp);
}
// Billboard does not use bar, so column must be used.
if ($options['type'] == 'bar') {
$chartAxis
->setRotated(TRUE);
}
elseif ($options['type'] == 'column') {
$chartData
->setType('bar');
$chartAxis
->setRotated(FALSE);
}
if ($options['type'] == 'scatter') {
/**
* Old code: $chartAxis->setX(['tick' => ['fit' => FALSE]]);
* New code: uses the Scatter Field in charts_fields. Still needs
* plenty of work.
*/
$fieldLabel = $seriesData[0]['name'];
array_shift($bbData[0]);
$scatterFieldData = $bbData[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);
$bbData[0] = $scatterFieldX;
$bbData[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($bbData, $categories);
}
$chartData
->setColumns($bbData);
$bb
->setAxis($chartAxis);
}
// Set the chart types.
for ($i = 0; $i < count($seriesData); $i++) {
$type = $this
->processChartType($seriesData[$i]);
if (isset($options['polar']) && $options['polar'] == 1) {
$type = 'radar';
}
$types[$seriesData[$i]['name']] = $type;
}
$chartData->types = $types;
// Set labels to FALSE if disabled in form.
if (empty($options['data_labels'])) {
$chartData
->setLabels(FALSE);
}
// Sets the primary y axis.
$showAxis['show'] = TRUE;
$showAxis['label'] = $options['yaxis_title'];
$chartAxis->y = $showAxis;
// Sets secondary axis from the first attachment only.
if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) {
$showSecAxis['show'] = TRUE;
$showSecAxis['label'] = $attachmentDisplayOptions[0]['style']['options']['yaxis_title'];
$chartAxis->y2 = $showSecAxis;
}
// Determines if chart is stacked.
if (!empty($options['grouping'] && $options['grouping'] == TRUE)) {
$seriesNames = [];
for ($i = 0; $i < $seriesCount; $i++) {
array_push($seriesNames, $seriesData[$i]['name']);
}
$chartData
->setGroups([
$seriesNames,
]);
}
// Set gauge options.
if ($type == 'gauge') {
$gauge = new ChartGauge();
$gauge
->setMin((int) $options['min']);
$gauge
->setMax((int) $options['max']);
$gauge
->setUnits($options['yaxis_suffix']);
$bb
->setGauge($gauge);
}
// Set markers (points)
if ($type == 'line' || $type == 'spline') {
$points = new ChartPoints();
if ($options['data_markers'] == 'FALSE') {
$points
->setShow(FALSE);
}
else {
$points
->setShow(TRUE);
}
$bb
->setPoint($points);
}
// Set legend.
$legend = new ChartLegend();
if (empty($options['legend_position'])) {
$legend
->setShow(FALSE);
}
else {
$legend
->setShow(TRUE);
}
$bb
->setLegend($legend);
$bindTo = '#' . $chartId;
$bb
->setBindTo($bindTo);
/**
* Override Billboard classes. These will only override what is in
* charts_billboard/src/Settings/CThree/CThree.php
* but you can use more of the Billboard API, as you are not constrained
* to what is in this class. See:
* charts_billboard/src/Plugin/override/BillboardOverrides.php
*/
foreach ($customOptions as $option => $key) {
$setter = 'set' . ucfirst($option);
if (method_exists($bb, $setter)) {
$bb
->{$setter}($customOptions[$option]);
}
}
$variables['chart_type'] = 'billboard';
$variables['content_attributes']['data-chart'][] = json_encode($bb);
$variables['attributes']['id'][0] = $chartId;
$variables['attributes']['class'][] = 'charts-bb';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AbstractChart:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
AbstractChart:: |
public | function |
Get Chart Name. Overrides ChartInterface:: |
|
Billboard:: |
public | function |
Creates a JSON Object formatted for Billboard.js Charts JavaScript to use. Overrides ChartInterface:: |
|
Billboard:: |
public | function | Process the chart type. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |