You are here

public function HighchartsHandler::render in Visualization API 7

Renders a chart using Highcharts.

Overrides VisualizationHandlerInterface::render

File

includes/plugins/highcharts.inc, line 27
Library plugin for Visualization implementing support for Highcharts.

Class

HighchartsHandler
@file Library plugin for Visualization implementing support for Highcharts.

Code

public function render($chart_id, $data, $options) {

  // Chart options.
  $highchart = new stdClass();

  // Chart.
  $highchart->chart = (object) array(
    'plotBackgroundColor' => NULL,
    'plotBorderWidth' => NULL,
    'plotShadow' => FALSE,
  );

  // Set title.
  $highchart->title = new stdClass();
  $highchart->title->text = $options['title'];
  $x_axis = array();
  if (!empty($options['xAxis']['labelField'])) {
    foreach ($data as $row) {
      $x_axis[] = html_entity_decode((string) $row[$options['xAxis']['labelField']], ENT_QUOTES);
    }
  }
  if (!empty($x_axis)) {
    $highchart->xAxis = (object) array(
      'categories' => $x_axis,
    );
  }
  if (!empty($options['yAxis']['title'])) {
    $highchart->yAxis = (object) array(
      'title' => (object) array(
        'text' => $options['yAxis']['title'],
      ),
    );
  }

  // Series.
  $highchart->series = array();
  foreach ($options['fields'] as $name => $column) {
    if (!empty($column['enabled'])) {
      $serie = new stdClass();
      $serie->name = $column['label'];
      $serie->type = $options['type'];
      $serie->data = array();
      foreach ($data as $row) {
        $value = (int) $row[$name];
        if (!empty($column['enabled'])) {
          $serie->data[] = (object) array(
            'name' => (string) html_entity_decode($row[$options['xAxis']['labelField']], ENT_QUOTES),
            'y' => $value,
          );
        }
        else {
          $serie->data[] = $value;
        }
      }
      $highchart->series[] = $serie;
    }
  }
  $highchart->plotOptions = new stdClass();
  $highchart->plotOptions->pie = (object) array(
    'allowPointSelect' => TRUE,
    'cursor' => 'pointer',
    'dataLabels' => (object) array(
      'enabled' => TRUE,
    ),
    'showInLegend' => TRUE,
  );
  $highchart->plotOptions->bar = (object) array(
    'dataLabels' => (object) array(
      'enabled' => TRUE,
    ),
  );
  $highchart->credits = new stdClass();
  $highchart->credits->enabled = FALSE;

  // Rendering.
  $highchart->chart->renderTo = $chart_id;
  $information = array(
    'library' => 'highcharts',
    'options' => $highchart,
  );

  // Add Drupal.settings for this chart.
  drupal_add_js(array(
    'visualization' => array(
      $chart_id => $information,
    ),
  ), array(
    'type' => 'setting',
  ));
}