<?php
/**
 * @file morris.inc
 *   Defines the Morris class.
 *
 * @author Adrian Rollett
 * 
 * @date 2012-02-29
 */

class Morris {
  var $element = 'morris'; // The ID of (or a reference to) the element into which to insert the graph.
  var $data = array(); // The data to plot. This is an array of objects, containing x and y attributes as described by the xkey and ykeys options.
  var $xkey = NULL; // A string containing the name of the attribute that contains date (X) values.
  var $ykeys = array(); // A list of strings containing names of attributes that contain Y values (one for each series of data to be plotted).
  var $labels = array(''); // A list of strings containing labels for the data series to be plotted (corresponding to the values in the ykeys option).
  var $lineWidth = 2; // Optional: Width of the series lines, in pixels.
  var $pointSize = 2; // Optional: Diameter of the series points, in pixels.
  var $ymax = 'auto'; // Optional: Max. bound for Y-values. Alternatively, set this to 'auto' to compute automatically, or 'auto [num]' to automatically compute and ensure that the max y-value is at least [num].
  var $smooth = TRUE; // Optional: Set to false to disable line smoothing.
  // var $lineColors = array('blue', 'black'); // Optional: Array containing colors for the series lines/points.

  function dump() {
    if (function_exists('dsm')) {
      dsm($this);
    }
  }

  function display() {
    if (!$this->xkey) {
      throw new exception('Graph keys not set.');
    }
    else {
      // @TODO: this should be more robust.
      // http://drupal.org/node/756722
      drupal_add_js('sites/all/libraries/morris/raphael-min.js', 'file');
      drupal_add_js('sites/all/libraries/morris/morris.min.js', 'file');
      drupal_add_js(array('morrisGraph' => array('setting' => $this)), 'setting');
    }
  }

  /**
   * Add data to the Morris object.
   *
   * @param $x
   *   array: Time data points.
   * @param $y
   *   array: Y data points, as arrays.
   * @param $labels
   *   array: Labels for Y series.
   * @param $xkey
   * @param $ykeys
   * @param $lineWidth
   * @param $lineColor
   */
  function setData($x, $y, $labels = array(), $xkey = NULL, $ykeys = array(), $lineWidth = NULL, $lineColors = array()) {
    // Set up keys if not defined.
    if (!$xkey) {
      $xkey = 'X';
    }
    if (count($ykeys) == 0) {
      for ($i = 0; $i < count($y); $i++) {
        $ykeys[] = 'Y' . $i;
      }
    }
    if (count($labels) == 0) {
      for ($i = 0; $i < count($y); $i++) {
        $labels[] = 'Y' . $i;
      }
    }
    if (count($lineColors)) {
      $this->lineColors = $lineColors;
    }
    if ($lineWidth) {
      $this->lineWidth = $lineWidth;
    }
    $this->xkey = $xkey;
    $this->ykeys = $ykeys;
    $this->labels = $labels;

    for ($i = 0; $i < count($x); $i++) {
      $data_point = array($xkey => $x[$i]);
      for ($j = 0; $j < count($y); $j++) {
        $data_point[$ykeys[$j]] = $y[$j][$i];
      }
      $this->data[] = (object) $data_point;
    }
  }
}
