You are here

function flot_views_plugin_style::preprocess in Flot 7

Same name and namespace in other branches
  1. 6 views/flot_views_plugin_style.inc \flot_views_plugin_style::preprocess()

Theme template preprocessor.

1 method overrides flot_views_plugin_style::preprocess()
flot_views_plugin_summary_style::preprocess in flot_views/views/flot_views_plugin_summary_style.inc
Theme template preprocessor.

File

flot_views/views/flot_views_plugin_style.inc, line 95

Class

flot_views_plugin_style

Code

function preprocess(&$vars) {

  // Get flot field, and bail if not present.
  $flot_field = $this
    ->get_flot_field();
  if (!$flot_field) {
    return;
  }
  $view = $this->view;
  $options = $this->options;

  // Parameters
  $type = !empty($options['type']) ? $options['type'] : 'line';
  $size = !empty($options['size']) ? explode('x', $options['size']) : array(
    '200',
    '100',
  );

  // DOM element options
  $element = array();
  $element['style'] = is_numeric($size[0]) ? "width:{$size[0]}px;" : "width:{$size[0]};";
  $element['style'] .= is_numeric($size[1]) ? "height:{$size[1]}px;" : "height:{$size[1]};";
  $vars['element'] = $element;
  $series = array();
  $range = array(
    'min' => NULL,
    'max' => NULL,
  );
  $ticks = array();

  // Iterate over results to build data and ticks
  foreach ($vars['rows'] as $id => $row) {
    $datapoint = $view->field[$flot_field]
      ->flot_render($row);
    $value = $datapoint['value'];
    $label = isset($datapoint['label']) ? $datapoint['label'] : $datapoint['value'];
    $series[] = array(
      $value[0],
      $value[1],
    );
    $ticks[] = array(
      $value[0],
      $label[0],
    );
    if (!isset($range['min']) || $value[1] < $range['min']) {
      $range['min'] = $value[1];
    }
    if (!isset($range['max']) || $value[1] > $range['max']) {
      $range['max'] = $value[1];
    }
  }
  $series = new flotData($series);
  $vars['data'] = array(
    $series,
  );

  // Set up the type class, set axes
  switch ($options['type']) {
    case 'point':
      $style = new flotStylePoint();
      break;
    case 'bar':
      $style = new flotStyleBar();
      break;
    case 'pie':
      $style = new flotStylePie();
      break;
    case 'line':
    default:
      $style = new flotStyleLine();
      break;
  }

  // Format Y Axis
  $granularity = 0;

  // If max is too small Flot barfs -- set a minimum value
  $range['max'] = $range['max'] < 5 ? 5 : $range['max'];

  // Pad Y axis if necessary
  if ($options['y']['pad']) {
    $range['min'] = 0;
    $range['max'] = floor($range['max'] + $range['max'] * 0.1);
  }
  switch ($options['y']['granularity']) {
    case 'endpoints':
      $yticks = array(
        array(
          $range['min'],
          $range['min'],
        ),
        array(
          $range['max'],
          $range['max'],
        ),
      );
      $style
        ->axis_ticks('yaxis', $yticks);
      break;
    case 'auto':
      $style
        ->axis_range('yaxis', $range);
      break;
    default:
      $style
        ->axis_range('yaxis', $range, $options['yaxis']);
      break;
  }

  // Format X Axis
  if ($options['x']['granularity'] == 'endpoints' && count($ticks) > 1) {
    $simplified_ticks = array();
    $simplified_ticks[] = array_shift($ticks);
    $simplified_ticks[] = array_pop($ticks);
    $ticks = $simplified_ticks;
  }
  $style
    ->axis_ticks('xaxis', $ticks);
  $vars['options'] = $style;
}