예제 #1
0
    static function trend_chart($table, $filter = "day", $name, $type, $color)
    {
        $trendstrl = _t('Statistics.TRENDS', 'Trends');
        $legendtrl = _t('Statistics.LEGEND', 'Legend');
        $top = <<<HTML
<div id="trendchart" style="display: none">
<h2>{$trendstrl}</h2>
<div><canvas id="chart" height="400" width="700"></canvas></div>
<div id="chart_legend"><legend>{$legendtrl}</legend></div>
</div>
\t\t<script type="text/javascript">

HTML;
        $bot = <<<HTML
var chart = new Plotr.{$type}Chart('chart',options);

\t\tchart.addDataset(chartdata);


\t\tchart.render();

\t\tchart.addLegend(\$('chart_legend'));

\t\t</script>
HTML;
        $ds = "var chartdata = { \n";
        foreach ($table as $class) {
            $record = DataObject::get($class, "", $class . ".Created DESC");
            $total = $record->TotalItems();
            $props = $record->toArray();
            $props = $props[0]->toMap();
            $startyear = new SSDatetime($props['Created']);
            $startyear = $startyear->Format('Y');
            $startmonth = new SSDatetime($props['Created']);
            $startmonth = $startmonth->Format('m');
            if ($filter == "day") {
                $days = new SSDatetime($props['Created']);
                $days = $days->Format('t');
                $sum = 0;
                $ds .= "{$class}: [";
                for ($i = 1; $i <= $days; $i++) {
                    foreach ($record as $v) {
                        $props = $v->toMap();
                        $currdate = new SSDatetime($props['Created']);
                        $curryear = $currdate->Format('Y');
                        $currmonth = $currdate->Format('m');
                        $currday = $currdate->Format('j');
                        if ($curryear == $startyear && $currmonth == $startmonth && $currday == $i) {
                            $sum++;
                        }
                    }
                    $ds .= "[" . ($i - 1) . ", {$sum}], ";
                }
                $ds .= "[]],\n";
            } else {
                if ($filter == "month") {
                    $sum = 0;
                    $ds .= "{$class}Set: [";
                    for ($i = 0; $i <= 11; $i++) {
                        $imonth = date('F', mktime(0, 0, 0, $i + 1, 1, 1));
                        foreach ($record as $v) {
                            $props = $v->toMap();
                            $currdate = new SSDatetime($props['Created']);
                            $curryear = $currdate->Format('Y');
                            $currmonth = $currdate->Format('m');
                            $currday = $currdate->Format('j');
                            if ($curryear == $startyear && $currmonth == $i) {
                                $sum++;
                            }
                        }
                        $ds .= "[{$i}, {$sum}], ";
                    }
                    $ds .= "[]],\n";
                }
            }
        }
        $xt = "xTicks: [";
        if ($filter == "month") {
            for ($i = 0; $i <= 11; $i++) {
                $imonth = date('F', mktime(0, 0, 0, $i + 1, 1, 1));
                $xt .= "{v:{$i}, label:'{$imonth}'}, ";
            }
        } else {
            if ($filter == "day") {
                for ($i = 1; $i <= $days; $i++) {
                    $xt .= "{v:" . ($i - 1) . ", label:'{$i}'}, ";
                }
            }
        }
        $opts = <<<HTML
var options = {

\taxisLabelFontSize:\t\t10,

\tpadding: {left: 30, right: 0, top: 10, bottom: 30},

\tbackgroundColor: '#cccccc',

\tcolorScheme: '{$color}',



HTML;
        $opts .= $xt . "]\n};";
        return $top . $ds . "\n};\n\n" . $opts . "\n\n" . $bot;
    }