1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Implements hook_theme(). |
---|
5 | */ |
---|
6 | function highcharts_theme() { |
---|
7 | return array( |
---|
8 | 'highcharts_chart' => array( |
---|
9 | 'arguments' => array('attributes' => array()), |
---|
10 | ), |
---|
11 | ); |
---|
12 | } |
---|
13 | |
---|
14 | /** |
---|
15 | * Returns HTML for a highcharts container. |
---|
16 | * |
---|
17 | * @param $attributes |
---|
18 | * An associative array containing valid HTML attributes to be applied to the |
---|
19 | * highchart container: |
---|
20 | * - id: Required. Must match the 'renderTo' option in the 'Highcharts.Chart' |
---|
21 | * object. |
---|
22 | * - style: Default: 'margin 0 auto' is appended to any existing style. |
---|
23 | * - class: Default: 'highcharts-chart' is appended to any existing class. |
---|
24 | * |
---|
25 | * @ingroup themeable |
---|
26 | * |
---|
27 | * @return |
---|
28 | * The chart div, ready for the highchart JS to render to. |
---|
29 | */ |
---|
30 | function theme_highcharts_chart($attributes = array()) { |
---|
31 | if ($attributes['id']) { |
---|
32 | $attributes['style'] = isset($attributes['style']) |
---|
33 | ? $attributes['style'] . 'margin: 0 auto' |
---|
34 | : 'margin: 0 auto'; |
---|
35 | |
---|
36 | $attributes['class'] = isset($attributes['class']) |
---|
37 | ? $attributes['class'] . ' highcharts-chart' |
---|
38 | : 'highcharts-chart'; |
---|
39 | |
---|
40 | $output = '<div ' . drupal_attributes($attributes) . '></div>'; |
---|
41 | } |
---|
42 | |
---|
43 | return $output; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Render a highchart. |
---|
48 | * |
---|
49 | * @param object $options |
---|
50 | * By reference. A highcharts options object. |
---|
51 | * |
---|
52 | * Any callbacks are specially handled as Drupal.settings only stores strings. |
---|
53 | * |
---|
54 | * Also includes: |
---|
55 | * - chart_id: added by reference. |
---|
56 | * |
---|
57 | * @see the highcharts API http://www.highcharts.com/ref/ |
---|
58 | */ |
---|
59 | function highcharts_render(&$options) { |
---|
60 | // Load common JS only once. |
---|
61 | static $js_added = false; |
---|
62 | if (!$js_added) { |
---|
63 | // Add highcharts library JS. |
---|
64 | module_load_include('module', 'libraries', 'libraries'); |
---|
65 | $path = libraries_get_path('highcharts'); |
---|
66 | if (!empty($path)) { |
---|
67 | drupal_add_js($path . '/js/highcharts.js'); |
---|
68 | } |
---|
69 | // Add highcharts behavior. This allows us to leverage the entire highcharts |
---|
70 | // API without needing to define each type separately. |
---|
71 | drupal_add_js(drupal_get_path('module', 'highcharts') . '/js/highcharts.js', 'module', 'footer'); |
---|
72 | // Flag common JS loaded status. |
---|
73 | $js_added = TRUE; |
---|
74 | } |
---|
75 | |
---|
76 | // Generate a unique chart ID. |
---|
77 | $chart_id = 'highcharts_' . uniqid(); |
---|
78 | |
---|
79 | // Pass this chart ID by reference to the options object. |
---|
80 | $options->chart->renderTo = $chart_id; |
---|
81 | // Add Drupal.settings for this chart. |
---|
82 | drupal_add_js(array('highcharts' => array($chart_id => $options)), 'setting'); |
---|
83 | |
---|
84 | // Add highchart options object methods, which can't be passed through |
---|
85 | // Drupal.settings. |
---|
86 | // @todo check for all possible highchart options object callbacks. |
---|
87 | $formatter = $options->tooltip->formatter; |
---|
88 | if ($formatter) { |
---|
89 | drupal_add_js("Drupal.settings.highcharts.{$chart_id}.tooltip.formatter = $formatter", 'inline', 'header', 'FALSE', 'FALSE', FALSE); |
---|
90 | } |
---|
91 | } |
---|