-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchart_js.theme.inc
56 lines (43 loc) · 1019 Bytes
/
chart_js.theme.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* @file
* Theme functions for chart types.
*/
/**
* Chart JS theme.
*/
function theme_chart_js(&$vars) {
static $chart_no = 0;
$chart_no++;
$chart_id = 'chart_js-' . $chart_no;
if (empty($vars['data'])) {
return t('No data provided.');
}
$g = (object) $vars;
// Sanitize data if html is not set to TRUE.
$d_labels= json_encode($vars['labels']);
$d_dataset = '';
foreach ($vars['data']['datasets'] as $k => $v){
$d_dataset .= json_encode($v) .',';
}
drupal_add_library('chart_js', 'chart_js');
drupal_add_js("new Chart(document.getElementById(\"$chart_id\"), {
type: '$g->chart_type',
data: {
labels: $d_labels,
datasets: [
$d_dataset
]
},
options: {
legend: { display: false },
title: {
display: true,
text: '$g->title'
}
}
});"
, array('type' => 'inline', 'scope' => JS_DEFAULT));
$html = "<canvas id=\"$chart_id\" width=\"800\" height=\"450\"></canvas>";
return $html;
}