You are here

function flot_handler_field_datapoint::pre_render in Flot 7

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

Optional method that allows the data source to determine axis bounds.

Overrides views_handler_field::pre_render

File

flot_views/views/flot_handler_field_datapoint.inc, line 182

Class

flot_handler_field_datapoint

Code

function pre_render($result) {
  if ($this->use_date) {

    // First grab endpoints from any date filters
    $filters = $this->view->display_handler
      ->get_handlers('filter');
    foreach ($filters as $filter => $handler) {
      if (strpos(get_class($handler), '_date') !== FALSE) {
        $min = REQUEST_TIME + intval(strtotime($handler->value['min'], 0));
        $max = REQUEST_TIME + intval(strtotime($handler->value['max'], 0));
        $value = intval(strtotime($handler->value['value'], 0));
        switch ($handler->operator) {
          case 'between':
            $start = $min;
            $end = $max;
            break;
          case '>=':
          case '>':
            $start = $min;
            $end = strtotime(end($result)->{$this->series_field});
            break;
          case '<=':
          case '<':
            $start = strtotime($result[0]->{$this->series_field});
            $end = $max;
            break;
        }
        break;
      }
    }
    $units = array(
      'Y-m-d-H' => '+1 hour',
      'Y-m-d-3' => '+3 hours',
      'Y-m-d-6' => '+6 hours',
      'Y-m-d-A' => '+12 hours',
      'Y-m-d' => '+1 day',
      'Y-m' => '+1 month',
      'Y' => '+1 year',
    );
    $grouping = $this->options['series']['grouping'];
    $grouping = isset($units[$grouping]) ? $grouping : 'Y-m-d';

    // Fill in default values in processed array.
    $processed = array();
    while ($start < $end) {
      $start = strtotime($units[$grouping], $start);
      $start_formatted = $this
        ->format_date($start, $grouping);
      $blank = new stdClass();
      $blank->{$this->series_field} = $start;
      $blank->{$this->value_field} = 0;
      $processed[$start_formatted] = $blank;
    }
    foreach ($result as $row) {
      $timestamp_formatted = $this
        ->format_date($row->{$this->series_field}, $grouping);
      if (isset($processed[$timestamp_formatted])) {
        if ($this->options['value']['format'] == "raw") {
          $processed[$timestamp_formatted]->values[] = $row->{$this->value_field};
        }
        elseif ($this->options['value']['format'] == "count") {
          $processed[$timestamp_formatted]->{$this->value_field}++;
        }
      }
    }

    // Average out the values.
    if ($this->options['value']['format'] == "raw") {
      foreach ($processed as &$item) {
        $values = isset($item->values) ? $item->values : array();
        if (count($values)) {
          $item->{$this->value_field} = array_sum($values) / count($values);
        }
      }
    }
    $this->view->result = array_values($processed);
  }
}