You are here

class ChartsGraphsBluff 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

Implementation of abstract class ChartsGraphsCanvas for Bluff library.

Hierarchy

Expanded class hierarchy of ChartsGraphsBluff

1 string reference to 'ChartsGraphsBluff'
charts_graphs_bluff_chartgraph_provider in apis/charts_graphs_bluff/charts_graphs_bluff.module
Implements hook_chartgraph_provider().

File

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

View source
class ChartsGraphsBluff extends ChartsGraphsCanvas {
  var $width = 450;
  var $height = 200;
  var $title = '';
  var $title_font_size = 32;

  /**
   * Parameters set directly by the user.
   *
   * @var <array>
   */
  var $parameters = array();

  /**
   * Optional parameter.
   *
   * It recognizes 3 different settings:
   * - auto: default behaviour where Bluff tries to identify the proper
   *   data orientation;
   * - rows: rows are mapped as data series;
   * - columns: columns are mapped as data series.
   *
   * @var string
   */
  var $orientation = NULL;

  /**
   * Converts the standard Charts and Graphs chart type to Bluff chart type.
   *
   * @return string
   */
  protected function _get_translated_chart_type() {
    switch ($this->type) {
      case 'mini_bar':
        $type = 'Mini.Bar';
        break;
      case 'mini_pie':
        $type = 'Mini.Pie';
        break;
      case 'mini_side_bar':
        $type = 'Mini.SideBar';
        break;
      case 'side_bar':
        $type = 'SideBar';
        break;
      case 'stacked_side_bar':
        $type = 'SideStackedBar';
        break;
      case 'stacked_area':
        $type = 'StackedArea';
        break;
      case 'stacked_bar':
        $type = 'StackedBar';
        break;
      default:
        $type = ucfirst($this->type);
    }
    return $type;
  }
  protected function _initialize_final_parameters($chart_id) {
    $parameters = array();
    $parameters['marker_font_size'] = 20;
    $parameters['hide_legend'] = 'true';
    $parameters['hide_title'] = 'false';
    $parameters['sort'] = 'false';
    $parameters['title_font_size'] = (int) $this->title_font_size;
    $parameters['tooltips'] = 'true';

    /**
     * Applying user defined min, max and step for y axis values.
     */
    if ($this->y_min) {
      $parameters['minimum_value'] = $this->y_min;
    }
    if ($this->y_max) {
      $parameters['maximum_value'] = $this->y_max;
    }
    if ($this->y_step) {
      $parameters['y_axis_increment'] = $this->y_step;
    }
    if (isset($this->theme) && !empty($this->theme)) {
      $parameters['theme_' . $this->theme] = array();
    }
    else {
      $series_colours = "['" . implode("', '", $this->series_colours) . "']";
      $marker_colour = '#aea9a9';
      $font_colour = '#000000';
      $background_colour = isset($this->colour) ? array(
        $this->colour,
        $this->colour,
      ) : array(
        '#d1edf5',
        '#ffffff',
      );
      $background_colour = "['" . implode("', '", $background_colour) . "']";
      $set_theme = sprintf("{\n          colors: %s,\n          marker_color: '%s',\n          font_color: '%s',\n          background_colors: %s\n        }", $series_colours, $marker_colour, $font_colour, $background_colour);
      $parameters['set_theme'] = array(
        $set_theme,
      );
    }
    $parameters['data_from_table'] = array(
      '"' . $chart_id . '"',
      "{orientation: '" . ($this->orientation ? $this->orientation : 'auto') . "'}",
    );
    if (is_array($this->parameters)) {
      foreach ($this->parameters as $user_parameter_key => $user_parameter) {
        $parameters[$user_parameter_key] = $user_parameter;
      }
    }
    $this->final_parameters = $parameters;
  }
  protected function _get_encoded_parameters() {
    $output = "\n";
    foreach ($this->final_parameters as $key => $value) {
      if (is_array($value)) {
        $function_parameters = '';
        foreach ($value as $val) {
          $function_parameters .= sprintf(', %s', $val);
        }
        if (strlen($function_parameters) > 0) {
          $function_parameters = substr($function_parameters, 2);
        }
        $output .= sprintf("\tg.%s(%s);\n", $key, $function_parameters);
      }
      else {
        $output .= sprintf("\tg.%s = %s;\n", $key, $value);
      }
    }
    return $output;
  }

  /**
   * Function that renders data.
   */
  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;
  }

  /**
   * Cache list of javascript files for performance.
   */
  function get_bluff_js_files() {
    static $js_files = NULL;
    if (is_array($js_files)) {
      return $js_files;
    }
    $bluff_path = drupal_get_path('module', 'charts_graphs_bluff');
    $bluff_files = array_map('basename', glob(dirname(__FILE__) . '/bluff/*.js'));
    rsort($bluff_files);
    $js_files = array();
    foreach ($bluff_files as $bluff_file) {
      $file_path = sprintf('%s/bluff/%s', $bluff_path, $bluff_file);
      $js_files[] = $file_path;
    }
    return $js_files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChartsGraphsBluff::$height property Overrides ChartsGraphsCanvas::$height
ChartsGraphsBluff::$orientation property Optional parameter.
ChartsGraphsBluff::$parameters property Parameters set directly by the user.
ChartsGraphsBluff::$title property Overrides ChartsGraphsCanvas::$title
ChartsGraphsBluff::$title_font_size property
ChartsGraphsBluff::$width property Overrides ChartsGraphsCanvas::$width
ChartsGraphsBluff::get_bluff_js_files function Cache list of javascript files for performance.
ChartsGraphsBluff::get_chart public function Function that renders data. Overrides ChartsGraphsCanvas::get_chart
ChartsGraphsBluff::_get_encoded_parameters protected function
ChartsGraphsBluff::_get_translated_chart_type protected function Converts the standard Charts and Graphs chart type to Bluff chart type.
ChartsGraphsBluff::_initialize_final_parameters protected function
ChartsGraphsCanvas::$modulename protected property
ChartsGraphsCanvas::$series property An array of series of values. Each element in a series (which is also an array in itself) is a numeric value of "measurement".
ChartsGraphsCanvas::$series_colours property Holds the colours to be used for each data serie.
ChartsGraphsCanvas::$showlegend property If true, shows a legend (if the library supports it).
ChartsGraphsCanvas::$type property
ChartsGraphsCanvas::$unique_id protected property
ChartsGraphsCanvas::$x_labels property Identifies each value in a data series. The same identification is used for all data series.
ChartsGraphsCanvas::$x_type property
ChartsGraphsCanvas::$y_legend property Text presented next to the left Y axis.
ChartsGraphsCanvas::$y_max property
ChartsGraphsCanvas::$y_min property
ChartsGraphsCanvas::$y_step property
ChartsGraphsCanvas::$zoom property Allows you to zoom in and out of a graph, if the library (flot does) supports it.
ChartsGraphsCanvas::getModuleName function
ChartsGraphsCanvas::getUnique_ID function
ChartsGraphsCanvas::series_colours function
ChartsGraphsCanvas::set_data function
ChartsGraphsCanvas::__construct function Constructor function.