You are here

function system_charts_build in Google Chart Tools: Image Charts 7

Same name and namespace in other branches
  1. 5 contrib/system_charts/system_charts.module \system_charts_build()
  2. 6 contrib/system_charts/system_charts.module \system_charts_build()

Gather data and build a chart API structure.

Return value

array chart API structure.

1 call to system_charts_build()
system_charts_display in system_charts/system_charts.module
Display charts and content in context to the current page.

File

system_charts/system_charts.module, line 132
Provides primary Drupal hook implementations.

Code

function system_charts_build($type) {
  $chart = array(
    '#chart_id' => "system_charts_{$type}",
  );
  $now = isset($_GET['year']) && isset($_GET['month']) ? mktime(0, 0, 0, $_GET['month'], 30, $_GET['year']) : REQUEST_TIME;
  switch ($type) {
    case 'node_counts':
    case 'node_counts_published':
    case 'node_counts_unpublished':
    case 'node_counts_today':
      $query = db_select('node', 'n');
      $query
        ->addExpression('COUNT(*)', 'count');
      $query
        ->fields('n', array(
        'type',
      ));
      switch ($type) {
        case 'node_counts':
          $title = t('Total');
          break;
        case 'node_counts_published':
          $title = t('Published');
          $query
            ->condition('n.status', '1', '=');
          break;
        case 'node_counts_unpublished':
          $title = t('Unpublished');
          $query
            ->condition('n.status', '0', '=');
          break;
      }
      $query
        ->orderBy('type');
      $query
        ->groupBy('type');
      $results = $query
        ->execute();
      while ($result = $results
        ->fetchAssoc()) {
        $chart['#data'][] = $result['count'];
        $chart['#labels'][] = $result['type'] . ': ' . $result['count'];
        $chart['#data_colors'][] = chart_unique_color($result['type']);
      }
      $chart['#chart_id'] = $type;
      $chart['#title'] = chart_title($title);
      $chart['#type'] = CHART_TYPE_PIE;
      $chart['#size'] = chart_size(600, 350);
      break;
    case 'node_activity':
      $query = db_select('node', 'n');
      $query
        ->fields('n', array(
        'type',
        'created',
      ));
      $query
        ->condition('n.created', $now, '<');
      $query
        ->condition('n.created', mktime(0, 0, 0, date('m', $now), 1, date('Y', $now)), '>');
      $query
        ->orderBy('created');
      $max = array();
      $counts = array();
      $types = array();
      $results = $query
        ->execute();
      while ($result = $results
        ->fetchAssoc()) {
        $day = ltrim(date('d', $result['created']), '0');
        $types[$result['type']] = $result['type'];
        $counts[$day][$result['type']] = isset($counts[$day][$result['type']]) ? $counts[$day][$result['type']] + 1 : 0;
        $max[$result['type']] = isset($max[$result['type']]) ? $max[$result['type']] + 1 : 0;
      }

      // Generate data and labels
      if (count($counts) && count($types)) {
        for ($i = 0; $i <= date('d', $now); $i++) {
          $chart['#labels'][] = $i;
          foreach ($types as $type) {
            $chart['#data'][$type][] = isset($counts[$i][$type]) ? $counts[$i][$type] : 0;
          }
        }
      }

      // Data colors, legends, line styles, and labels
      if (count($types)) {
        foreach ($types as $type) {
          $chart['#data_colors'][] = chart_unique_color($type);
          $chart['#legends'][] = $type;
          $chart['#line_styles'][] = chart_line_style(2);
        }
      }
      $max = count($max) ? max($max) : 0;
      $chart['#chart_id'] = 'node_activity';
      $chart['#title'] = chart_title(t('Node Activity for !date', array(
        '!date' => date('F Y', $now),
      )));
      $chart['#type'] = CHART_TYPE_LINE;
      $chart['#size'] = chart_size(620, 250);
      $chart['#grid_lines'] = chart_grid_lines(25, 9.5, 1, 3);
      $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
      $chart['#adjust_resolution'] = TRUE;
      break;
    case 'watchdog_counts':
      $query = db_select('watchdog', 'w');
      $query
        ->addExpression('COUNT(*)', 'count');
      $query
        ->fields('w', array(
        'type',
      ));
      $query
        ->groupBy('type');
      $query
        ->orderBy('type');
      $results = $query
        ->execute();
      while ($result = $results
        ->fetchAssoc()) {
        $chart['#data'][] = $result['count'];
        $chart['#labels'][] = $result['type'] . ': ' . $result['count'];
        $chart['#data_colors'][] = chart_unique_color($result['type']);
      }
      $chart['#chart_id'] = 'watchdog_counts';
      $chart['#title'] = chart_title(t('Watchdog Messages'));
      $chart['#type'] = CHART_TYPE_PIE;
      $chart['#size'] = chart_size(600, 350);
      break;
    case 'watchdog_severity':
      $query = db_select('watchdog', 'w');
      $query
        ->addExpression('COUNT(*)', 'count');
      $query
        ->fields('w', array(
        'severity',
      ));
      $query
        ->groupBy('severity');
      $query
        ->orderBy('severity');
      $results = $query
        ->execute();
      while ($result = $results
        ->fetchAssoc()) {
        $severity_label = _system_charts_watchdog_severity_label($result['severity']);
        $chart['#data'][] = $result['count'];
        $chart['#labels'][] = $severity_label . ': ' . $result['count'];
        $chart['#data_colors'][] = chart_unique_color($severity_label, 'watchdog_severity');
      }
      $chart['#chart_id'] = 'watchdog_severity';
      $chart['#title'] = chart_title(t('Message Severity'));
      $chart['#type'] = CHART_TYPE_PIE;
      $chart['#size'] = chart_size(600, 350);
      break;
    case 'users_per_role':
      $query = db_select('users_roles', 'ur');
      $query
        ->join('users', 'u', 'ur.uid = u.uid');
      $query
        ->join('role', 'r', 'r.rid = ur.uid');
      $query
        ->fields('r', array(
        'rid',
        'name',
      ));
      $query
        ->addExpression('COUNT(*)', 'count');
      $query
        ->groupBy('r.rid');
      $query
        ->orderBy('r.name');
      $results = $query
        ->execute();
      while ($result = $results
        ->fetchAssoc()) {
        $chart['#data'][] = $result['count'];
        $chart['#labels'][] = $result['name'] . ': ' . $result['count'];
        $chart['#data_colors'][] = chart_unique_color('role_' . $result['name']);
      }
      $chart['#chart_id'] = 'users_per_role';
      $chart['#title'] = chart_title('Users Per Role');
      $chart['#type'] = CHART_TYPE_PIE;
      $chart['#size'] = chart_size(600, 350);
      break;
    case 'user_status':
      $query = db_select('users', 'u');
      $query
        ->addExpression('COUNT(*)', 'count');
      $query
        ->fields('u', array(
        'status',
      ));
      $query
        ->condition('uid', 0, '!=');
      $query
        ->groupBy('status');
      $query
        ->orderBy('status');
      $results = $query
        ->execute();
      while ($result = $results
        ->fetchAssoc()) {
        $chart['#data'][] = $result['count'];
        $chart['#labels'][] = _system_charts_user_status_label($result['status']) . ': ' . $result['count'];
        $chart['#data_colors'][] = chart_unique_color('status_' . $result['status']);
      }
      $chart['#chart_id'] = 'user_status';
      $chart['#title'] = chart_title('User Status');
      $chart['#type'] = CHART_TYPE_PIE;
      $chart['#size'] = chart_size(600, 350);
      break;
  }
  return theme('chart', array(
    'chart' => $chart,
  ));
}