-
Notifications
You must be signed in to change notification settings - Fork 1
/
chartjs.module
229 lines (211 loc) · 5.06 KB
/
chartjs.module
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* @file
* Module file for chartjs.
*/
/**
* Implements hook_menu().
*/
function chartjs_menu() {
$items = array();
$items['admin/config/chartjs'] = array(
'title' => 'Global settings for chart.js',
'page callback' => 'drupal_get_form',
'page arguments' => array('chartjs_admin'),
'access arguments' => array('administer chartjs'),
'type' => MENU_NORMAL_ITEM,
'file' => 'chartjs.admin.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function chartjs_permission() {
return array(
'administer chartjs' => array(
'title' => t('Access administration interface for chartjs.'),
'description' => t('Allow users to access chartjs administration interface.'),
),
);
}
/**
* Implements hook_libraries_info().
*/
function chartjs_libraries_info() {
$libraries = array();
$libraries['Chart.js'] = array(
'name' => 'Chart.js',
'vendor url' => 'http://www.chartjs.org/',
'download url' => 'https://github.com/nnnick/Chart.js',
'version callback' => 'chartjs_library_version_callback',
'files' => array(
'js' => array(
'Chart.js',
),
),
);
return $libraries;
}
/**
* Chartjs library version.
*
* @returns string
* String representing the version number for the installed chartjs library.
*/
function chartjs_library_version_callback() {
return TRUE;
}
/**
* Implements hook_js_alter().
*/
function chartjs_js_alter(&$javascript) {
// If the chartjs library is loaded, add global settings.
if (isset($javascript['sites/all/libraries/Chart.js/Chart.js'])) {
$settings = chartjs_get_global_settings_js();
drupal_add_js($settings, array(
'type' => 'inline',
'scope' => 'header',
'every_page' => FALSE,
));
}
}
/**
* Fetch global settings for chartjs.
*
* @return array
* Array of global settings.
*/
function chartjs_get_global_settings() {
$properties = array(
'animation',
'animationSteps',
'animationEasing',
'showScale',
'scaleOverride',
'scaleSteps',
'scaleStepWidth',
'scaleStartValue',
'scaleLineColor',
'scaleLineWidth',
'scaleShowLabels',
'scaleLabel',
'scaleIntegersOnly',
'scaleBeginAtZero',
'scaleFontFamily',
'scaleFontSize',
'scaleFontStyle',
'scaleFontStyle',
'scaleFontColor',
'responsive',
'showTooltips',
'customTooltips',
'tooltipFillColor',
'tooltipFontFamily',
'tooltipFontSize',
'tooltipFontStyle',
'tooltipFontColor',
'tooltipTitleFontFamily',
'tooltipTitleFontSize',
'tooltipTitleFontStyle',
'tooltipTitleFontColor',
'tooltipYPadding',
'tooltipXPadding',
'tooltipCaretSize',
'tooltipCornerRadius',
'tooltipXOffset',
'tooltipTemplate',
'multiTooltipTemplate',
'legendTemplate',
);
$global_settings = array();
foreach ($properties as $property) {
$global_settings[$property] = variable_get('chartjs_' . $property);
}
return $global_settings;
}
/**
* Fetch global settings json for chartjs.
*
* @return string
* String of global settings js.
*/
function chartjs_get_global_settings_js() {
$settings = chartjs_get_global_settings();
return 'Chart.defaults.global = jQuery.extend({}, Chart.defaults.global, ' . chartjs_json_encode($settings) . ');';
}
/**
* Returns a chart dom and js needed to render a chart.
*
* @param string $type
* Type of chart that should be returned.
* @param array $data
* Data that should be charted.
* @param string $id
* Unique ID of chart.
* @param array $options
* Options that override global defaults.
* @param int $width
* Width of chart.
* @param int $height
* Height of chart.
*
* @return string
* String of global settings js.
*/
function chartjs_get_chart($type, $data, $id, $options = array(), $width = 400, $height = 400) {
return theme('chartjs', array(
'type' => $type,
'data' => $data,
'chart_id' => $id,
'options' => $options,
'width' => $width,
'height' => $height,
));
}
/**
* Implements hook_theme().
*/
function chartjs_theme() {
return array(
'chartjs' => array(
'template' => 'tpl/chartjs',
'variables' => array(
'type' => 'Doughnut',
'data' => array(),
'chart_id' => 'RandomChart',
'options' => array(),
'width' => 400,
'height' => 400,
),
),
);
}
/**
* Converts an array to a json array with proper typing.
*
* @param array $data
* Data that should be converted to json.
*
* @return string
* String of json with proper types.
*/
function chartjs_json_encode($data) {
$typed_array = array();
foreach ($data as $key => $value) {
// Convert 'true' and 'false into TRUE and FALSE.
if ($value === 'true' || $value === 'false') {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
// Convert 'null' to NULL.
elseif ($value === 'null') {
$value = NULL;
}
// Convert numeric strings to ints.
elseif (is_numeric($value)) {
$value = (int)$value;
}
$typed_array[$key] = $value;
}
return json_encode($typed_array);
}