You are here

public function Chartjs::buildVariables in Charts 8.3

Creates a JSON Object formatted for C3 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_chartjs/src/Plugin/chart/Chartjs.php, line 205

Class

Chartjs
Define a concrete class for a Chart.

Namespace

Drupal\charts_chartjs\Plugin\chart

Code

public function buildVariables(array $options, array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables, $chartId, array $customOptions = []) {

  // Create new instance of Chart.js.
  $chartjs = new ChartjsChart();
  $chartjsData = new ChartjsData();

  // Useful variables for loops.
  $seriesCount = count($seriesData);
  $attachmentCount = count($attachmentDisplayOptions);
  $noAttachmentDisplays = $attachmentCount === 0;

  // Set the chart type.
  $chartjs
    ->setType($this
    ->buildChartType($options));

  // Set the chart labels.
  $chartjsData
    ->setLabels($categories);

  // Populate the data object.
  $dataset = [];
  for ($i = 0; $i < $seriesCount; $i++) {
    $dataset[$i] = new \stdClass();
    $dataset[$i]->label = $seriesData[$i]['name'];
    $dataset[$i]->data = $seriesData[$i]['data'];
    $dataset[$i]->backgroundColor = $seriesData[$i]['color'];

    // Type is needed here for mixed charts.
    $dataset[$i]->type = $this
      ->buildChartType($seriesData[$i]);
    if ($seriesData[$i]['type'] == 'area') {
      $dataset[$i]->fill = 'origin';
    }
    else {
      $dataset[$i]->fill = FALSE;
    }
    if (!empty($options['polar']) && $options['polar'] == 1) {
      $dataset[$i]->borderColor = $seriesData[$i]['color'];
      $dataset[$i]->type = 'radar';
    }
    if ($dataset[$i]->type == 'linearGauge') {
      $dataset[$i]->offset = ($i + 1) * 10;
    }
    if ($dataset[$i]->type == 'scatter') {
      $data = $dataset[$i]->data;
      $scatterDataSet = [];
      for ($i = 0; $i < count($data); $i++) {
        $scatterData = new \stdClass();
        $scatterData->x = $data[$i][0];
        $scatterData->y = $data[$i][1];
        array_push($scatterDataSet, $scatterData);
      }
      $dataset[0]->data = $scatterDataSet;
    }
  }
  $chartjsData
    ->setDatasets($dataset);
  $chartjs
    ->setData($chartjsData);

  // Set Gauge settings
  if ($options['type'] == 'gauge') {
    $chartjs
      ->setScaleColorRanges($this
      ->buildGaugeOptions($options));
    $range = [];

    // $range->startValue = $options['min'].
    // $range->endValue = $options['max'].
    $range['startValue'] = 1;
    $range['endValue'] = 1000;
    $chartjs
      ->setRange($range);
  }
  $chartjs
    ->setOptions($this
    ->buildOptions($options));

  /*
   * Override Chart.js classes. These will only override what is in
   * charts_chartjs/src/Settings/Chartjs/ChartjsChart.php
   * but you can use more of the Chart.js API, as you are not constrained
   * to what is in this class. See:
   * charts_chartjs/src/Plugin/override/ChartjsOverrides.php
   */
  foreach ($customOptions as $option => $key) {
    $setter = 'set' . ucfirst($option);
    if (method_exists($chartjs, $setter)) {
      $chartjs
        ->{$setter}($customOptions[$option]);
    }
  }
  $variables['chart_type'] = 'chartjs';
  $variables['content_attributes']['data-chart'][] = json_encode($chartjs);
  $variables['attributes']['id'][0] = $chartId;
  $variables['attributes']['class'][] = 'charts-chartjs';
}