You are here

public function ChartsBluff::get_chart in Charts and Graphs 6

Function that renders data.

Overrides ChartCanvas::get_chart

File

apis/charts_bluff/charts_bluff.class.inc, line 81
Implementation of abstract class ChartCanvas for Bluff library.

Class

ChartsBluff
Implementation of abstract class ChartCanvas for Bluff library.

Code

public function get_chart() {
  $bluff_js_files = $this
    ->get_bluff_js_files();
  foreach ($bluff_js_files as $file_path) {
    drupal_add_js($file_path);
  }
  $bluff_path = drupal_get_path('module', 'charts_bluff');
  drupal_add_css($bluff_path . '/charts_bluff.css');
  $x_labels = $this->x_labels;
  $series = $this->series;
  $chart_id = 'bluffchart-' . chart_graphs_chart_id_generator();
  $table = array();
  $table[] = sprintf('
      <table id="%s" class="bluff-data-table">
        <caption>%s</caption>
        <thead>
          <tr>
            <th scope="col"></th>', $chart_id, $this->title);
  $cols = array_keys($series);
  foreach ($cols as $col) {
    $table[] = sprintf('<th scope="col">%s</th>', $col);
  }
  $table[] = '</tr></thead><tbody>';
  foreach ($x_labels as $key => $label) {
    $table[] = '<tr>';
    $cols = array(
      $label,
    );
    foreach ($series as $serie) {
      $cols[] = $serie[$key];
    }
    $table[] = sprintf('<th scope="row">%s</th>', array_shift($cols));
    foreach ($cols as $col) {
      $table[] = sprintf('<td>%s</td>', (string) $col);
    }
    $table[] = '</tr>';
  }
  $table[] = '</tbody></table>';
  $is_pie_chart = $this->type == 'pie';
  $transpose = isset($this->transpose) ? $this->transpose : $is_pie_chart;
  $html = implode('', $table);
  $javascript = '<canvas id="%chart_id-graph" width="%width" height="%height"></canvas>
      <script type="text/javascript">
        var ChartsAndGraphs = ChartsAndGraphs || {};

        ChartsAndGraphs.init = function() {
          var g = new Bluff.%type("%chart_id-graph", "%widthx%height");
          g.marker_font_size = 20;
          g.hide_legend = true;
          g.hide_title = false;
          g.sort = false;
          g.title_font_size = %title_font_size;
          g.theme_%theme();
          g.data_from_table("%chart_id", %transpose);
          g.draw();

          var g_labels = %json_encode;
          var legend = ["<ul class=\\"bluff-legend\\">"];

          for (var i = 0, j = 0, color; i < g_labels.length; i++, j++) {
            if (g.colors[j]) {
              color = g.colors[j]
            }
            else {
              g.colors[(0)];
              j = -1;
            }
            legend.push("<li>");
            legend.push("<div style=\\"background-color: " + color + "\\"><\\/div>" + g_labels[i]);
            legend.push("<\\/li>");
          }

          legend.push("<\\/ul>");

          $("#%chart_id-graph")
            .parent("div.bluff-wrapper")
            .append(legend.join(""))
            .css({height: "auto"});
        }

        $(window).load(ChartsAndGraphs.init);

        Drupal.behaviors.ChartsAndGraphs_init = function(context) {
          ChartsAndGraphs.init();
        }
      </script>';
  $javascript = strtr($javascript, array(
    '%chart_id' => $chart_id,
    '%type' => $this
      ->_get_translated_chart_type(),
    '%width' => $this->width,
    '%height' => $this->height,
    '%theme' => $this->theme,
    '%transpose' => $transpose ? 'true' : 'false',
    '%title_font_size' => (int) $this->title_font_size,
    '%json_encode' => json_encode($is_pie_chart ? $x_labels : array_keys($series)),
  ));
  return $html . $javascript;
}