You are here

function system_charts_build in Google Chart Tools: Image Charts 6

Same name and namespace in other branches
  1. 5 contrib/system_charts/system_charts.module \system_charts_build()
  2. 7 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 contrib/system_charts/system_charts.module
Display charts and content in context to the current page.

File

contrib/system_charts/system_charts.module, line 119
Provides core Drupal chart implementations.

Code

function system_charts_build($type) {
  $chart = array();
  $now = isset($_GET['year']) && isset($_GET['month']) ? mktime(0, 0, 0, $_GET['month'], 30, $_GET['year']) : time();
  switch ($type) {
    case 'node_counts':
    case 'node_counts_published':
    case 'node_counts_unpublished':
    case 'node_counts_today':
      $sql_where = '';
      switch ($type) {
        case 'node_counts':
          $title = t('Total');
          break;
        case 'node_counts_published':
          $title = t('Published');
          $sql_where = " WHERE status = '1' ";
          break;
        case 'node_counts_unpublished':
          $title = t('Unpublished');
          $sql_where = " WHERE status = '0' ";
          break;
      }
      $results = db_query("\n          SELECT COUNT(*) as count, type\n          FROM {node}\n          " . $sql_where . "\n          GROUP BY type\n          ORDER BY type\n        ");
      while ($result = db_fetch_array($results)) {
        $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':
      $results = db_query("\n          SELECT type, created\n          FROM {node}\n          WHERE created  < %d AND created > %d\n          ORDER BY created\n        ", $now, mktime(0, 0, 0, date('m', $now), 1, date('Y', $now)));
      $max = array();
      $counts = array();
      $types = array();
      while ($result = db_fetch_array($results)) {
        $day = ltrim(date('d', $result['created']), '0');
        $types[$result['type']] = $result['type'];
        $counts[$day][$result['type']]++;
        $max[$result['type']]++;
      }

      // Generate data and labels
      if (count($counts) && count($types)) {
        for ($i = 0; $i <= date('d', $now); $i++) {
          $chart['#labels'][] = $i;
          foreach ($types as $type) {
            if ($counts[$i][$type]) {
              $chart['#data'][$type][] = $counts[$i][$type];
            }
            else {
              $chart['#data'][$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':
      $results = db_query("\n          SELECT COUNT(*) as count, type\n          FROM {watchdog}\n          GROUP BY type\n          ORDER BY type\n        ");
      while ($result = db_fetch_array($results)) {
        $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':
      $results = db_query("\n          SELECT COUNT(*) as count, severity\n          FROM {watchdog}\n          GROUP BY severity\n          ORDER BY severity\n        ");
      while ($result = db_fetch_array($results)) {
        $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':
      $results = db_query("\n          SELECT COUNT(*) as count, r.*\n          FROM {users_roles} ur\n          LEFT JOIN {users} u ON ur.uid = u.uid\n          LEFT JOIN {role} r ON r.rid = ur.rid\n          GROUP BY r.rid, r.name\n          ORDER BY r.name\n        ");
      while ($result = db_fetch_array($results)) {
        $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':
      $results = db_query("\n          SELECT COUNT(*) as count, status\n          FROM {users}\n          WHERE uid != 0\n          GROUP BY status\n          ORDER BY status\n        ");
      while ($result = db_fetch_array($results)) {
        $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 chart_render($chart);
}