5 * Views integration for google_data_visualization module .
9 * Implements hook_views_plugins().
11 function google_data_visualization_views_plugins() {
12 $plugins['style']['google_data_visualization'] = array(
13 'title' => t('google_data_visualization'),
14 'help' => t('Display rows in a chart via google_data_visualization . '),
15 'handler' => 'google_data_visualization_style_plugin',
16 'path' => drupal_get_path('module', 'google_data_visualization') .
'/includes',
17 'theme' => 'google_data_visualization_view',
18 'theme path' => drupal_get_path('module', 'google_data_visualization') .
'/includes',
19 'uses row plugin' => TRUE
,
20 'uses options' => TRUE
,
21 'uses grouping' => FALSE
,
28 * Menu callback; Handle AJAX Views requests for charts .
30 function google_data_visualization_views_ajax() {
31 if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
32 $name = $_REQUEST['view_name'];
33 $display_id = $_REQUEST['view_display_id'];
34 $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ?
explode('/', $_REQUEST['view_args']) : array();
35 $path = isset($_REQUEST['view_path']) ?
$_REQUEST['view_path'] : NULL
;
36 $dom_id = isset($_REQUEST['google_data_visualization_dom_id']) ?
intval($_REQUEST['google_data_visualization_dom_id']) : NULL
;
37 $first = isset($_REQUEST['first']) ?
intval($_REQUEST['first']) : NULL
;
38 $last = isset($_REQUEST['last']) ?
intval($_REQUEST['last']) : NULL
;
39 views_include('ajax');
40 $object = new
stdClass();
42 $object->status
= FALSE
;
43 $object->display
= '';
45 $arg = explode('/', $_REQUEST['view_path']);
48 if ($view = views_get_view($name)) {
49 $view->set_display($display_id);
51 if ($view->access($display_id)) {
53 // Fix 'q' for paging.
58 // Disable the pager, render between the start and end values.
60 if (isset($view->pager
)) {
61 $view->pager
['use_pager'] = FALSE
;
62 $view->pager
['offset'] = $first;
63 $view->pager
['items_per_page'] = $last - $first;
64 $view->display
[$display_id]->handler
->set_option('use_pager', 0);
65 $view->display
[$display_id]->handler
->set_option('offset', $first);
66 $view->display
[$display_id]->handler
->set_option('items_per_page', $last - $first);
70 $view->set_items_per_page($last - $first);
71 $view->set_offset($first);
73 // Redundant but apparently needed.
74 $view->items_per_page
= $last - $first;
75 $view->offset
= $first;
78 // Reuse the same DOM id so it matches that in Drupal settings.
79 $view->google_data_visualization_dom_id
= $dom_id;
81 $errors = $view->validate();
82 if ($errors === TRUE
) {
83 $object->status
= TRUE
;
84 $object->title
= $view->get_title();
85 $object->display .
= $view->preview($display_id, $args);
88 foreach ($errors as
$error) {
89 drupal_set_message($error, 'error');
94 $messages = theme('status_messages');
95 $object->messages
= $messages ?
'<div class="views-messages">' .
$messages .
'</div>' : '';
97 drupal_json_output($object);
102 * Adds necessary CSS and JS for Views-based charts.
104 function google_data_visualization_views_add($view, $display_id = NULL
) {
105 static
$dom_counter = 0;
107 if (!isset($display_id)) {
108 $display_id = empty($view->current_display
) ?
'default' : $view->current_display
;
111 // Save the settings for the chart, these will be used by the JavaScript.
114 // Keep track of each settings addition and give a unique ID . This can be
115 // useful when displaying the same view multiple times with different
116 // arguments (i . e . such as in a panel).
117 $options['view_options'] = array(
118 'view_args' => check_plain(implode('/', $view->args
)),
119 'view_path' => check_plain($_GET['q']),
120 'view_base_path' => $view->get_path(),
121 'view_display_id' => $display_id,
122 'view_name' => $view->name
,
123 'google_data_visualization_dom_id' => isset($view->google_data_visualization_dom_id
) ?
$view->google_data_visualization_dom_id
: ++$dom_counter,
126 foreach ($view->style_plugin
->options as
$key => $option) {
128 $options[$key] = is_numeric($option) ?
(int) $option : $option;
132 // By default limit the scrolling to the same number of items as are visible
133 // to avoid display glitches.
134 if (empty($options['scroll']) && !empty($options['visible'])) {
135 $options['scroll'] = $options['visible'];
138 // By default limit the scrolling to the same number of items as are visible
139 // to avoid display glitches.
140 if (empty($options['scroll']) && !empty($options['visible'])) {
141 $options['scroll'] = $options['visible'];
144 // Get the total number of items in this view.
145 $count_query = $view->build_info
['count_query']->countQuery();
146 $count = $count_query->execute()->fetchField();
148 // Views may already populate total_rows later, but since we've already
149 // generated this value, we might as well make it available.
150 $view->total_rows
= $count;
152 // If there is just one item disable the auto-scroll and rotation.
154 $options['graph_type'] = NULL
;
155 $options['auto'] = 0;
158 // Determine AJAX functionality in a backwards-compatible way . Versions prior
159 // to google_data_visualization 2 . 6 used the view-level "Use AJAX"
160 // option instead of a style
161 // setting . We check $view->style_options here intentionally instead of
162 // $view->style_plugin->options.
163 $use_ajax = isset($view->style_options
['ajax']) ?
$view->style_options
['ajax'] : $view->use_ajax
;
165 // If using AJAX, adjust the view's positioning based on the current page.
167 $options['ajax'] = TRUE
;
168 $options['size'] = $count;
171 if (isset($view->pager
)) {
172 // Enable and adjust the pager to get the correct page.
173 $use_pager = $view->pager
['use_pager'];
174 $view->pager
['use_pager'] = TRUE
;
175 $view->build($display_id);
176 $view->pager
['use_pager'] = $use_pager;
178 // Create generic variable names.
179 $pager_current_page = $view->pager
['current_page'];
180 $pager_items_per_page = $view->pager
['items_per_page'];
181 $pager_offset = $view->pager
['offset'];
185 // Adjusting the query is not necessary.
186 $view->build($display_id);
188 // Create generic variable names.
189 $pager_current_page = $view->current_page
;
190 $pager_items_per_page = $view->items_per_page
;
191 $pager_offset = $view->offset
;
194 // If starting in the middle of a view, initialize the chart at that
195 // position . Strangely the chart must pre-load empty LI items all the way
196 // up until the active item, making this inefficient for large lists.
197 if ($pager_current_page) {
198 // TODO: Pagers and charts do not work well together.
199 // google_data_visualization should
200 // give items the class "google_data_visualization-item-[offset]",
201 // but instead it always
202 // starts with "1", making it impossible to define a prepopulated list
203 // as the middle of an AJAX view.
204 $options['start'] = ($pager_current_page * $pager_items_per_page) + ($pager_offset + 1);
205 $options['offset'] = ($pager_current_page * $pager_items_per_page) + ($pager_offset + 1);
207 elseif ($pager_offset) {
208 $options['start'] = $pager_offset + 1;
209 $options['offset'] = $pager_offset + 1;
213 $identifier = drupal_clean_css_identifier('google_data_visualization_dom_' .
$options['view_options']['google_data_visualization_dom_id']);
214 return google_data_visualization_add($identifier, $options);
218 * Preprocess function for google_data_visualization-view . tpl . php .
220 function template_preprocess_google_data_visualization_view(&$variables) {
222 $view = $variables['view'];
223 $display_id = empty($view->current_display
) ?
'default' : $view->current_display
;
225 // Add necessary JavaScript and CSS.
226 $attachments = google_data_visualization_views_add($view, $display_id);
228 // Find the JavaScript settings in the returned array.
229 foreach ($attachments['js'] as
$data) {
230 if (isset($data['type']) && $data['type'] == 'setting') {
231 $settings = reset($data['data']['google_data_visualization']['charts']);
235 // Build the list of classes for the chart.
236 $options = $view->style_plugin
->options
;
237 $variables['google_data_visualization_classes_array'] = array(
238 'google_data_visualization',
239 drupal_clean_css_identifier('google_data_visualization-view--' .
$view->name .
'--' .
$display_id),
240 // drupal_clean_css_identifier('google_data_visualization-dom-'.
241 // $settings['view_options']['google_data_visualization_dom_id']),
243 if (empty($options['graph_type'])) {
244 $variables['google_data_visualization_classes_array'][] = 'google_data_visualization-skin-' .
$options['graph_type'];
246 if (empty($options['graph_width'])) {
247 $options['graph_width'] = 500;
249 if (empty($options['graph_height'])) {
250 $options['graph_height'] = 400;
252 if (empty($options['fontfamily'])) {
253 $options['fontfamily'] = "thahoma";
255 if (empty($options['fontsize'])) {
256 $options['fontsize'] = 12;
258 $variables['google_data_visualization_classes'] = implode(' ', $variables['google_data_visualization_classes_array']);
259 // Views 2/3 compatibility.
260 $pager_offset = isset($view->pager
['offset']) ?
$view->pager
['offset'] : $view->offset
;
262 // Give each item a class to identify where in the chart it belongs.
263 foreach ($variables['rows'] as
$id => $row) {
264 $number = $id + 1 + $pager_offset;
265 $zebra = ($number %
2 == 0) ?
'even' : 'odd';
266 $variables['row_classes'][$id] = 'google_data_visualization-item-' .
$number .
' ' .
$zebra;
268 for ($i = 0; $i < count($variables['rows']); $i++) {
269 if ($variables['rows'][$i] == NULL
) {
270 $variables['rows'][$i] = 0;
273 $records = $view->display
[$view->current_display
]->handler
->get_option('fields');
277 $result = $variables['result'] = $variables['rows'];
278 $vars['rows'] = array();
279 $vars['field_classes'] = array();
280 $vars['header'] = array();
281 foreach ($records as
$key => $val) {
282 if (!$val['exclude']) {
284 $header .
= "'" .
$val['label'] .
"',";
287 $header = substr($header , 0 , strlen($header) - 1 );
288 $dat_array = '[[' .
$header .
'],';
289 $view_re = $variables['view'];
290 $rowfields = $rowfields_scatter = '';
292 foreach ($view_re->result as
$key => $val) {
294 $rowfields = $rowfields .
$pre;
295 $rowfields_scatter = $rowfields_scatter .
$pre;
297 foreach ($view->field as
$id => $field) {
298 // render this even if set to exclude so it can be used elsewhere.
299 $field_output = $view->style_plugin
->get_field($j, $id);
300 if (empty($field->options
['exclude'])) {
301 if (!is_numeric($field_output)) {
304 if (empty($field_output)) {
308 $rowfields .
= $field_output .
",";
311 $rowfields .
= "'" .
$field_output .
"',";
313 $rowfields_scatter .
= $field_output .
",";
314 $empty = $field->is_value_empty($field_output , $field->options
['empty_zero']);
315 $object = new
stdClass();
316 $object->handler
= &$view->field
[$id];
317 $object->inline
= !empty($vars['options']['inline'][$id]);
321 $rowfields_scatter = substr($rowfields_scatter , 0 , strlen($rowfields_scatter) - 1 );
322 $rowfields_scatter =$rowfields_scatter .
'],';
323 $rowfields = substr($rowfields , 0 , strlen($rowfields) - 1 );
324 $rowfields = $rowfields .
'],';
327 $rowfields = substr($rowfields,0, strlen($rowfields) - 1 );
328 $rowfields_scatter = substr($rowfields_scatter,0,strlen($rowfields_scatter) - 1);
329 $dat_array_scatter = $dat_array .
$rowfields_scatter .
']';
330 $dat_array = $dat_array .
$rowfields .
']';
331 $dat_array = substr($dat_array,0, strlen($dat_array) - 1 );
332 $dat_array = $dat_array .
']';
333 $variables['datasvals'] = $dat_array;
334 $variables['records'] = $records;
335 $variables['graph_options'] = array();
336 $variables['graph_chart'] = array();
337 $variables['graph_data'] = array();
338 $variables['graph_header'] = array();
339 $variables['graph_id'] = array();
340 $variables['setonloadcallback'] = 'google . setOnLoadCallback(' .
$view->name .
'_' .
$view->current_display .
'_drawChart);';
341 $variables['callbackfunction'] = $view->name .
'_' .
$view->current_display .
'_drawChart()';
344 $data_var = $dat_array;
345 $data_var_scatter = $dat_array_scatter;
346 foreach($options['graph_type'] as
$key => $val) {
350 $variables['graph_data'][$val] = 'var ' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
"= google . visualization . arrayToDataTable(" .
$data_var_scatter .
");";
351 $variables['graph_header'][$val] = 'var ' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
' = new google . visualization . DataTable();';
352 $variables['graph_id'][$val] = $view->name .
'_' .
$view->current_display .
'_' .
$val;
353 $variables['graph_options'][$val] = 'var ' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
'_options = {
354 title: "' .
$options["graph_title"] .
'",
355 width: ' .
$options["graph_width"] .
' ,
356 height:' .
$options["graph_height"] .
' ,
357 hAxis: {title: "' .
$options['hAxis'] .
'"},
358 hAxis: {color:"red"},
359 vAxis: {color:"red"},
360 vAxis: {title: "' .
$options['vAxis'] .
'"},
361 fontSize: ' .
$options["fontsize"] .
',
362 fontName: "' .
$options["fontfamily"] .
'",
363 titleTextStyle: {color: "#FF0000"}
365 $variables['graph_chart'][$val] = 'var chart = new google . visualization . ' .
$val .
'(document . getElementById("' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
'"));
366 chart . draw(' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
',' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
'_options);';
369 $variables['graph_data'][$val] = 'var ' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
"= google . visualization . arrayToDataTable(" .
$data_var .
");";
370 $variables['graph_header'][$val] = 'var ' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
' = new google . visualization . DataTable();';
371 $variables['graph_id'][$val] = $view->name .
'_' .
$view->current_display .
'_' .
$val;
372 $variables['graph_options'][$val] = 'var ' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
'_options = {
373 title: "' .
$options["graph_title"] .
'",
374 width: ' .
$options["graph_width"] .
' ,
375 height:' .
$options["graph_height"] .
' ,
376 hAxis: {title: "' .
$options["hAxis"] .
'"},
377 hAxis: {color:"red"},
378 vAxis: {color:"red"},
379 vAxis: {title: "' .
$options["vAxis"] .
'"},
380 fontSize: ' .
$options["fontsize"] .
',
381 fontName: "' .
$options["fontfamily"] .
'",
382 titleTextStyle: {color: "#FF0000"}
384 $variables['graph_chart'][$val] = 'var chart = new google . visualization . ' .
$val .
'(document . getElementById("' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
'"));
385 chart . draw(' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
',' .
$view->name .
'_' .
$view->current_display .
'_' .
$val .
'_options);';