How to use render-arrays to programmatically create a chart in Drupal 8 block

by Meglio   Last Updated November 12, 2017 12:07 PM

I'm programming a block in Drupal 8:

class B extends BlockBase implements BlockPluginInterface {
  public function build() {
    return []; // cannot figure out what to return here
  }
}

I read that in D8 a block's build function should return a render-array, like this:

return ['#markup' => 'Some HTML'];

Then in the Charts source code I found some examples here and there, especially in PHPDOC comments, so I built a render-array like this:

$return = [
  $chart = [
    '#type' => 'chart',
    '#chart_type' => 'line',
    '#chart_library' => 'c3',
    'xaxis' => [
                    '#type' => 'chart_xaxis',
                    '#title' => 'XTitle',
                    '#labels' => range(1, 10),
                ],
    'yaxis' => [
                    '#type' => 'chart_yaxis',
                    '#title' => 'YTitle'
                ],
    'series_1' => ['#type' => 'chart_data', '#title' => $seriesTitle,
                    '#data' => range(1, 10) ],
    // more series here keyed series_2, series_3 etc
  ]
];

Nothing is output (only block title). No JS errors in console. No error logs. Example chart in demo module works, C3 is installed.

What am I doing wrong?

Versions:

  • Drupal: 8.4.2
  • Charts: charts-8.x-3.0-alpha6
Tags : 8 blocks charts


Related Questions



D7 Print Dynamic Google Chart in Custom Block Module

Updated October 09, 2017 00:07 AM