You are here

function flot_handler_field_datapoint::pre_render in Flot 6

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

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

File

views/flot_handler_field_datapoint.inc, line 183

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 = $handler->value['min'];
        $min = !is_numeric($min) ? time() + intval(strtotime($min, 0)) : $min;
        $max = $handler->value['max'];
        $max = !is_numeric($max) ? time() + intval(strtotime($max, 0)) : $max;
        $value = intval(strtotime($handler->value['value'], 0));
        switch ($handler->operator) {
          case 'between':
            $start = $min;
            $end = $max;
            break;
          case '>=':
          case '>':
            $start = $min;

            // We can't count on sort order of the result set but do
            // assume that things have been sorted.
            $front = reset($result)->{$this->series_field};
            $front = !is_numeric($front) ? strtotime($front) : $front;
            $back = end($result)->{$this->series_field};
            $back = !is_numeric($back) ? strtotime($back) : $back;
            $end = $front > $back ? $front : $back;
            break;
          case '<=':
          case '<':
            $front = reset($result)->{$this->series_field};
            $front = !is_numeric($front) ? strtotime($front) : $front;
            $back = end($result)->{$this->series_field};
            $back = !is_numeric($back) ? strtotime($back) : $back;
            $start = $front < $back ? $front : $back;
            $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])) {
        $processed[$timestamp_formatted]->{$this->value_field}++;
      }
    }
    $this->view->result = array_values($processed);
  }
}