Example #1
0
function create_graph($type, $range = null)
{
    global $db;
    // Do we have date range criteria?
    if ($range['end'] || $range['start']) {
        $start = (int) $range['start'];
        $end = (int) $range['end'];
    } else {
        $start = TIME_NOW - 60 * 60 * 24 * 30;
        $end = TIME_NOW;
    }
    $allowed_types = array('users', 'threads', 'posts');
    if (!in_array($type, $allowed_types)) {
        die;
    }
    require_once MYBB_ROOT . 'inc/class_graph.php';
    $points = $stats = $datelines = array();
    if ($start == 0) {
        $query = $db->simple_select("stats", "dateline,num{$type}", "dateline <= '" . (int) $end . "'", array('order_by' => 'dateline', 'order_dir' => 'desc', 'limit' => 2));
        while ($stat = $db->fetch_array($query)) {
            $stats[] = $stat['num' . $type];
            $datelines[] = $stat['dateline'];
            $x_labels[] = date("m/j", $stat['dateline']);
        }
        $points[$datelines[0]] = 0;
        $points[$datelines[1]] = $stats[0] - $stats[1];
        ksort($points, SORT_NUMERIC);
    } elseif ($end == 0) {
        $query = $db->simple_select("stats", "dateline,num{$type}", "dateline >= '" . (int) $start . "'", array('order_by' => 'dateline', 'order_dir' => 'asc', 'limit' => 2));
        while ($stat = $db->fetch_array($query)) {
            $stats[] = $stat['num' . $type];
            $datelines[] = $stat['dateline'];
            $x_labels[] = date("m/j", $stat['dateline']);
        }
        $points[$datelines[0]] = 0;
        $points[$datelines[1]] = $stats[1] - $stats[0];
        ksort($points, SORT_NUMERIC);
    } else {
        $query = $db->simple_select("stats", "dateline,num{$type}", "dateline >= '" . (int) $start . "' AND dateline <= '" . (int) $end . "'", array('order_by' => 'dateline', 'order_dir' => 'asc'));
        while ($stat = $db->fetch_array($query)) {
            $points[$stat['dateline']] = $stat['num' . $type];
            $datelines[] = $stat['dateline'];
            $x_labels[] = date("m/j", $stat['dateline']);
        }
    }
    sort($datelines, SORT_NUMERIC);
    // Find our year(s) label
    $start_year = date('Y', $datelines[0]);
    $last_year = date('Y', $datelines[count($datelines) - 1]);
    if ($last_year - $start_year == 0) {
        $bottom_label = $start_year;
    } else {
        $bottom_label = $start_year . " - " . $last_year;
    }
    // Create the graph outline
    $graph = new Graph();
    $graph->add_points(array_values($points));
    $graph->add_x_labels($x_labels);
    $graph->set_bottom_label($bottom_label);
    $graph->render();
    $graph->output();
}