1 | API |
---|
2 | === |
---|
3 | |
---|
4 | There are two primary functions, theme_highcharts_chart() (returns HTML for a highcharts container), and highcharts_render() (renders a highcharts options object). |
---|
5 | |
---|
6 | Modules may create highchart options objects (see http://www.highcharts.com/ref/), leveraging anything available in the highcharts API. |
---|
7 | |
---|
8 | Example options object: |
---|
9 | |
---|
10 | ```php |
---|
11 | /** |
---|
12 | * Pie-basic highcharts options object. |
---|
13 | * |
---|
14 | * @see highcharts_render() |
---|
15 | */ |
---|
16 | function custom_pie_basic_options() { |
---|
17 | $options = new stdClass(); |
---|
18 | |
---|
19 | // Chart. |
---|
20 | $options->chart = (object)array( |
---|
21 | 'renderTo' => 'container', |
---|
22 | 'plotBackgroundColor' => NULL, |
---|
23 | 'plotBorderWidth' => NULL, |
---|
24 | 'plotShadow' => FALSE, |
---|
25 | ); |
---|
26 | |
---|
27 | // Title. |
---|
28 | $options->title->text = t('Pies'); |
---|
29 | |
---|
30 | // Tooltip. |
---|
31 | // Normally formatter is a function callback. For now we'll make it a string. |
---|
32 | // @todo whenever this is user defined (views config, etc) be sure to sanitize |
---|
33 | // this string before passing to highcharts_render(). |
---|
34 | $options->tooltip->formatter = "function() {return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';}"; |
---|
35 | |
---|
36 | // Plot options. |
---|
37 | $options->plotOptions->pie = (object)array( |
---|
38 | 'allowPointSelect' => TRUE, |
---|
39 | 'cursor' => 'pointer', |
---|
40 | 'dataLabels' => array( |
---|
41 | 'enabled' => FALSE, |
---|
42 | ), |
---|
43 | 'showInLegend' => TRUE, |
---|
44 | ); |
---|
45 | |
---|
46 | // Series. |
---|
47 | $options->series = array(); |
---|
48 | $series = new StdClass(); |
---|
49 | $series->type = 'pie'; |
---|
50 | $series->name = 'Slices'; |
---|
51 | $series->data = array(); |
---|
52 | $series->data[] = array('Banana creme', 45.0); |
---|
53 | $series->data[] = array('Pumpkin', 26.8); |
---|
54 | |
---|
55 | // Selected item is an object. |
---|
56 | $selected = new stdClass(); |
---|
57 | $selected->name = 'Apple'; |
---|
58 | $selected->y = 12.8; |
---|
59 | $selected->sliced = TRUE; |
---|
60 | $selected->selected = TRUE; |
---|
61 | $series->data[] = $selected; |
---|
62 | |
---|
63 | $series->data[] = array('Lemon merengue', 8.5); |
---|
64 | $series->data[] = array('Mincemeat', 6.2); |
---|
65 | $series->data[] = array('Others', 0.7); |
---|
66 | |
---|
67 | $options->series[] = $series; |
---|
68 | |
---|
69 | // Diable credits. |
---|
70 | $options->credits->enabled = FALSE; |
---|
71 | |
---|
72 | return $options; |
---|
73 | } |
---|
74 | ``` |
---|
75 | |
---|
76 | Example implementation of new Drupal Highcharts API functions: |
---|
77 | |
---|
78 | ```php |
---|
79 | /** |
---|
80 | * Pie-basic highcharts block. |
---|
81 | * |
---|
82 | * @return hook_block() view op definition. |
---|
83 | */ |
---|
84 | function _custom_block_view_pie_basic() { |
---|
85 | $options = custom_pie_basic_options(); |
---|
86 | if (is_object($options)) { |
---|
87 | // Render the chart options object, and retrieve the chart id. |
---|
88 | highcharts_render($options); |
---|
89 | // Define required and optional chart container attributes. |
---|
90 | $attributes = array( |
---|
91 | // Required chart id passed by reference. |
---|
92 | 'id' => $options->chart->renderTo, |
---|
93 | // Optionally add styles or any other valid attribute. |
---|
94 | 'style' => 'height: 400px;' |
---|
95 | ); |
---|
96 | |
---|
97 | // Return block definition. |
---|
98 | return array( |
---|
99 | 'subject' => check_plain($options->title->text), |
---|
100 | 'content' => theme('highcharts_chart', $attributes), |
---|
101 | ); |
---|
102 | } |
---|
103 | } |
---|
104 | ``` |
---|