You are here

function template_preprocess_flot_graph in Flot 7

File

./flot.module, line 54

Code

function template_preprocess_flot_graph(&$variables) {
  static $n = 0;
  $zoom = isset($variables['addselectionfilter']) && $variables['addselectionfilter'] || isset($variables['zoom']) && $variables['zoom'];
  $data = array();
  if (isset($variables['data'])) {
    $data = $variables['data'];
  }
  if (!isset($variables['element']['style'])) {
    $variables['element']['style'] = "width:100%;height:200px";
  }
  $options = new stdClass();
  if (isset($variables['options'])) {
    $options = (object) $variables['options'];
  }
  $variables['element']['class'] = isset($variables['element']['class']) ? $variables['element']['class'] . ' flot' : 'flot ';
  if ($zoom) {
    $variables['element']['class'] = isset($variables['element']['class']) ? $variables['element']['class'] . ' flot-with-zoom' : 'flot-with-zoom';
    $variables['element']['id'] = 'flot-view-zoomable-' . $n++;
    $options->selection['mode'] = 'x';
  }
  else {
    if (!isset($variables['element']['id'])) {
      $n++;
      $variables['element']['id'] = 'flot-auto-identifier-' . $n;
    }
  }
  if (isset($variables['legend']) && $variables['legend']) {
    if (!isset($options->legend)) {
      $options->legend = new stdClass();

      // Default position bottom
      $options->legend->show = TRUE;
      $options->legend->position = 'ne';
    }
    else {
      $options->legend->show = TRUE;
      if (!isset($options->legend->position)) {
        $options->legend->position = 'ne';
      }
    }
  }
  else {
    if (!isset($options->legend)) {
      $options->legend = new stdClass();
    }
    $options->legend->show = FALSE;
  }
  $variables['options'] = $options;
  $legend_element = array();
  if (isset($options->legend) && isset($options->legend->position) && $options->legend->position == 'bottom' && !isset($options->legend->container)) {
    $variables['element']['legendid'] = $variables['element']['id'] . '-legend';
    $options->legend->container = '#' . $variables['element']['legendid'];
    $options->legend->containerid = $variables['element']['legendid'];
    $legend_element = array(
      'id' => $variables['element']['legendid'],
      'class' => array(
        'legend',
        'legend-bottom',
      ),
    );
  }
  flot_add_js('core');
  if (isset($options->selection)) {
    flot_add_js('selection');
  }
  if (isset($options->series->pie->show) && $options->series->pie->show || isset($options->pie) && $options->pie) {
    $options = (object) ((array) new flotStylePie() + (array) $options);
    flot_add_js('pie');
  }
  foreach ($variables['data'] as $s => $serie) {
    if (isset($serie->stack)) {
      flot_add_js('stack');
    }
    if (isset($serie->bars->order)) {
      flot_add_js('orderBars');
    }
  }
  $jsdata = '
    jQuery(document).ready(function() {
      Drupal.flot[\'' . str_replace('-', '_', $variables['element']['id']) . '\'] = {};
      Drupal.flot[\'' . str_replace('-', '_', $variables['element']['id']) . '\'][\'flot\'] = jQuery.plot(jQuery("#' . $variables['element']['id'] . '"), ' . drupal_json_encode((array) $data) . ', ' . drupal_json_encode($options) . ');
      Drupal.flot[\'' . str_replace('-', '_', $variables['element']['id']) . '\'][\'options\'] = ' . drupal_json_encode((array) $options) . ';
      Drupal.flot[\'' . str_replace('-', '_', $variables['element']['id']) . '\'][\'data\'] = ' . drupal_json_encode((array) $data) . ';
    });
  ';
  drupal_add_js($jsdata, array(
    'type' => 'inline',
    'scope' => 'footer',
  ));
  $variables['element'] = array(
    'graph' => array(
      'style' => $variables['element']['style'],
      'class' => isset($variables['element']['class']) ? array(
        $variables['element']['class'],
      ) : array(),
      'id' => $variables['element']['id'],
    ),
  );
  $zoom_element = array();
  if ($zoom && (!isset($options->series->pie->show) || $options->series->pie->show == FALSE)) {
    $zoom_options = clone $options;
    $zoom_data = $data;
    if (!isset($zoom_options->series)) {
      $zoom_options->series = new stdClass();
    }
    if (!isset($zoom_options->grid)) {
      $zoom_options->grid = new stdClass();
    }
    if (!isset($zoom_options->series->points)) {
      $zoom_options->series->points = new stdClass();
    }
    if (!isset($zoom_options->series->lines)) {
      $zoom_options->series->lines = new stdClass();
    }
    $zoom_options->series->points->show = FALSE;
    $zoom_options->series->lines->lineWidth = 1;
    $zoom_options->series->shadowSize = 0;
    $zoom_options->grid->hoverable = FALSE;
    $zoom_options->selection['mode'] = 'x';
    $zoom_options->legend->show = FALSE;
    if (isset($zoom_options->xaxis)) {
      unset($zoom_options->xaxis->min);
      unset($zoom_options->xaxis->max);
    }
    if (isset($zoom_options->yaxis)) {
      unset($zoom_options->yaxis->min);
      unset($zoom_options->yaxis->max);
      unset($zoom_options->yaxis->autoscaleMargin);
    }
    foreach ($zoom_data as &$zdata) {
      if (isset($zdata->lines) || isset($zdata->bars) || isset($zdata->pie)) {
        if (!isset($zdata->points)) {
          $zdata->points = new stdClass();
        }
        $zdata->points->show = FALSE;
      }
    }
    $jszoomdata = '
      jQuery(document).ready(function() {
        Drupal.flot[\'' . str_replace('-', '_', $variables['element']['graph']['id'] . '-zoom') . '\'] = {};
        Drupal.flot[\'' . str_replace('-', '_', $variables['element']['graph']['id'] . '-zoom') . '\'][\'flot\'] = jQuery.plot(jQuery("#' . $variables['element']['graph']['id'] . '-zoom' . '"), ' . drupal_json_encode((array) $zoom_data) . ', ' . drupal_json_encode($zoom_options) . ');
        Drupal.flot[\'' . str_replace('-', '_', $variables['element']['graph']['id'] . '-zoom') . '\'][\'options\'] = ' . drupal_json_encode((array) $zoom_options) . ';
        Drupal.flot[\'' . str_replace('-', '_', $variables['element']['graph']['id'] . '-zoom') . '\'][\'data\'] = ' . drupal_json_encode((array) $zoom_data) . ';
      });
    ';
    drupal_add_js($jszoomdata, array(
      'type' => 'inline',
      'scope' => 'footer',
    ));
    $width = array();
    preg_match('/width:(.*)px;/', $variables['element']['graph']['style'], $width);
    $zoom_element = array(
      'id' => $variables['element']['graph']['id'] . '-zoom',
      'style' => 'width:' . $width[1] . 'px;height:100px',
      'class' => array(
        'flot-zoom',
      ),
    );
  }
  $variables['element']['zoom'] = $zoom_element;
  $variables['element']['legend'] = $legend_element;
}