You are here

public function ChartsGraphsBluff::get_chart in Charts and Graphs 7

Same name and namespace in other branches
  1. 6.2 apis/charts_graphs_bluff/charts_graphs_bluff.class.inc \ChartsGraphsBluff::get_chart()

Function that renders data.

Overrides ChartsGraphsCanvas::get_chart

File

apis/charts_graphs_bluff/charts_graphs_bluff.class.inc, line 166
Implementation of abstract class ChartsGraphsCanvas for Bluff library.

Class

ChartsGraphsBluff
Implementation of abstract class ChartsGraphsCanvas 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_graphs_bluff');
  drupal_add_css($bluff_path . '/charts_graphs_bluff.css');
  $x_labels = $this->x_labels;
  $series = $this->series;
  $chart_id = 'bluffchart-' . charts_graphs_chart_id_generator();
  $table = array();
  $table[] = sprintf(<<<TABLE
      <table id="%s" class="bluff-data-table">
        <caption>%s</caption>
        <thead>
          <tr>
            <th scope="col"></th>
TABLE
, $chart_id, $this->title);
  $serie_keys = array_keys($series);
  foreach ($serie_keys as $col) {
    $table[] = sprintf("<th scope='col'>%s</th>\n", $col);
  }
  $table[] = "</tr></thead><tbody>\n";
  foreach ($x_labels as $key => $label) {
    $table[] = "<tr>\n";
    $cols = array(
      $label,
    );
    foreach ($serie_keys as $serie_key) {
      $cols[] = array_shift($series[$serie_key]);
    }
    $table[] = sprintf("<th scope='row'>%s</th>\n", array_shift($cols));
    foreach ($cols as $col) {
      $table[] = sprintf("<td>%s</td>\n", (string) $col);
    }
    $table[] = "</tr>\n";
  }
  $table[] = "</tbody></table>\n";
  $is_pie_chart = $this->type == 'pie';
  if ($this->orientation === NULL) {
    $this->orientation = $is_pie_chart ? 'rows' : 'auto';
  }
  $this
    ->_initialize_final_parameters($chart_id);
  $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");
          ';
  $javascript .= $this
    ->_get_encoded_parameters();
  $javascript .= '
          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 = 0;
            }
            legend.push("<li>");
            legend.push("<div style=\\"background-color: " + color + "\\"><\\/div>" + g_labels[i]);
            legend.push("<\\/li>");
          }

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

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

        jQuery(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,
    '%json_encode' => json_encode($is_pie_chart ? $x_labels : array_keys($series)),
  ));
  $element = array(
    '#markup' => $html . $javascript,
  );
  return $element;
}