You are here

function template_preprocess_views_view_flot_views_time in Flot 8

Implements template_preprocess_TEMPLATE().

File

flot_views_time/flot_views_time.theme.inc, line 12
Template, theme, and preprocess functions for the flot_views_time mosule.

Code

function template_preprocess_views_view_flot_views_time(&$variables) {

  // View options set by user.
  $options = $variables['view']->style_plugin->options;

  // Initialize the data variable.
  $data = [];
  $view = $variables['view'];
  foreach ($options['columns']['x'] as $id => $values) {
    $series = [];

    // The actual x-y values for the one series.
    $values = [];
    $x_column = $options['columns']['x'][$id];
    $y_column = $options['columns']['y'][$id];
    if ($options['info'][$id]['points'] == 1) {
      $series['points'] = [
        'show' => TRUE,
      ];
    }
    if ($options['info'][$id]['lines'] == 1) {
      $series['lines'] = [
        'show' => TRUE,
      ];
    }
    foreach ($view->result as $row) {
      $datetime_text = $view->field[$x_column]
        ->getValue($row);

      // Sometimes datetimes are unix timestamps, and some times they are
      // ISO strings. There has to be a better way to do this.
      $datetime_stringed_int = '' . intval($datetime_text);

      //If these are equal, we had an int in the database.
      if ($datetime_text == $datetime_stringed_int) {
        $datetime_text = '@' . $datetime_text;
      }
      $datetime_object = new DateTime($datetime_text, new DateTimeZone('UTC'));
      $datetime_object
        ->setTimezone(new DateTimeZone('America/New_York'));
      $unix_time = $datetime_object
        ->getTimestamp();
      $value = $view->field[$y_column]
        ->getValue($row);
      if ($view->field[$y_column]->multiple) {
        $value = $view->field[$y_column]
          ->getItems($row)[0]['rendered']['#markup'];
      }
      $values[] = [
        // Convert unix time to JS time (seconds to milliseconds)
        $unix_time * 1000,
        $value,
      ];
    }
    if ($options['info'][$id]['second_axis'] == 1) {
      $series['yaxis'] = 2;
    }
    $series['label'] = $y_column;
    $series['data'] = $values;
    $data[] = $series;
  }

  // Update options for twig.
  $flot_options = [
    'xaxis' => [
      'mode' => 'time',
    ],
  ];
  $variables['flot_views_time'] = [
    '#type' => 'flot',
    '#data' => $data,
    '#options' => $flot_options,
  ];
}