Example #1
0
/**
 * Return the chartjs structured array data for a line graph
 *
 * @param   array  $data  Array of information to graph
 *                        Includes: 'data': associative array of point label -> data point
 *                                  'labels': labels for the lines
 *                                  'colours': custom colours from the function to use instead of the defaults
 *                                  'labellang': the lang file to find the label string translation
 *                                  'configs': associative array of config overrides
 * @param   array  $colours    Custom colours from js to use instead of the defaults
 * @param   bool   $cron       If function is called from cron we don't want to reply via js
 *
 * @return  array  $graphdata  An array structure that can be encoded to json for chartjs
 */
function get_line_graph_json($data, $colours = null, $cron = false)
{
    if (empty($data['data'])) {
        $data['empty'] = true;
        if ($cron) {
            return $data;
        }
        json_reply(false, array('data' => $data));
    }
    $data['colours'] = get_graph_colours($data, $colours);
    $graphdata = array();
    $x = 0;
    $graphdata['labels'] = $data['labels'];
    foreach ($data['data'] as $key => $value) {
        $dataobj['fillColor'] = "rgba(" . $data['colours'][$x] . ",0.2)";
        $dataobj['strokeColor'] = "rgba(" . $data['colours'][$x] . ",1)";
        $dataobj['pointColor'] = "rgba(" . $data['colours'][$x] . ",1)";
        $dataobj['pointStrokeColor'] = "rgba(255,255,255,1)";
        $dataobj['pointHighlightFill'] = "rgba(255,255,255,1)";
        $dataobj['pointHighlightStroke'] = "rgba(" . $data['colours'][$x] . ",1)";
        $dataobj['label'] = !empty($data['labellang']) ? get_string($key, $data['labellang']) : $key;
        $dataobj['data'] = is_array($value) ? array_values($value) : array($value);
        $graphdata['datasets'][] = $dataobj;
        $x = empty($data['colours'][$x + 1]) ? 0 : $x + 1;
    }
    $configs = isset($data['configs']) ? $data['configs'] : (object) array();
    return array($graphdata, $configs);
}
Example #2
0
         $x = 0;
         foreach ($jsondata[0] as $key => $option) {
             foreach ($option as $optkey => $optval) {
                 if (preg_match('/^rgba\\(/', $optval)) {
                     $jsondata[0][$key]->{$optkey} = preg_replace('/\\((.*\\,)/', '(' . $colours[$x] . ',', $optval);
                 }
             }
             $x = empty($colours[$x + 1]) ? 0 : $x + 1;
         }
     }
     $data['datastr'] = json_encode($jsondata[0]);
     $data['configstr'] = json_encode($data['configs']);
     json_reply(false, array('data' => $data));
 }
 $graphdata = array();
 $data['colours'] = get_graph_colours($data, $colours);
 // Now covert it to something Chart.js can understand
 switch ($data['graph']) {
     case 'Pie':
     case 'PolarArea':
     case 'Doughnut':
         list($graphdata, $configs) = get_circular_graph_json($data, $colours);
         break;
     case 'Bar':
         list($graphdata, $configs) = get_bar_graph_json($data, $colours);
         break;
     case 'Line':
         list($graphdata, $configs) = get_line_graph_json($data, $colours);
         break;
     default:
 }