-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflot.module
221 lines (195 loc) · 5.84 KB
/
flot.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
<?php
/**
* Implements hook_views_api().
*/
function flot_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'flot') . '/views',
);
}
/**
* Implements hook_theme().
*/
function flot_theme() {
return array(
'flot_graph' => array(
'variables' => array('element' => array(), 'data' => array(), 'options' => array(), 'loader' => FALSE),
),
);
}
/**
* Main flot graphing function
*
* @param $element
* An associative array to define a placeholder element. If an 'id' is
* omitted one will be generated, If no 'style' is specified and width and
* height style will be added. In short you can just pass an empty array and
* everything will still work. This argument is essentially optional and has
* been kept as the first argument to remain consistant with flots own api.
* @param $data
* The data series for the graph. Optional. See flot's API.txt for more
* details. This module defines the flotData class which can be used or
* extended to make generating data objects simpler.
* @param $options
* Options to pass to flot. Optional. See flot's API.txt for more details.
* @param $loader
* Allows alterative loading of flot data. If 'false' data will passed
* directly to an invocation of $.plot(). Otherwise the contents of $loader
* should be js.
*
* @return
* The placeholder element
*/
function theme_flot_graph($variables) {
$element = $variables['element'];
$data = $variables['data'];
$options = $variables['options'];
$loader = $variables['loader'];
static $n;
if (!isset($element['id'])) {
$n++;
$element['id'] = 'flot-auto-identifier-' . $n;
}
if (!isset($element['style'])) {
$element['style'] = "width:100%;height:200px";
}
flot_add_js();
if (count($data)) {
$extra = '';
$id = str_replace('-', '_', $element['id']);
if ($loader) {
$json_data = drupal_json_encode($data);
$extra = "Drupal.flot.{$id}_data = {$json_data}; {$loader}";
$data = array();
}
$json_data = drupal_json_encode($data);
$json_options = drupal_json_encode($options);
$data = "(function ($) {
$(document).ready(function() {
Drupal.flot.{$id} = $.plot($('#{$element['id']}'), {$json_data}, {$json_options});
{$extra}
});
})(jQuery);";
drupal_add_js($data, array('type' => 'inline', 'scope' => JS_DEFAULT));
}
return '<div ' . drupal_attributes($element) . '> </div>';
}
/**
* Helper to add flot's js
*/
function flot_add_js() {
static $added;
if ($added !== true) {
if (module_exists('libraries') && libraries_get_path('flot')) {
$path = libraries_get_path('flot');
}
if (!isset($path)) {
$path = drupal_get_path('module', 'flot') . '/flot';
}
// Different versions of flot have used different packing methods. Attempt to support both.
$excanvas = file_exists("{$path}/excanvas.min.js") ? "{$path}/excanvas.min.js" : "{$path}/excanvas.pack.js";
drupal_add_html_head('<!--[if IE]><script language="javascript" type="text/javascript" src="' . base_path() . $excanvas . '"></script><![endif]-->', $key = NULL /* TODO Set this variable. */);
drupal_add_js($path . '/jquery.flot.js');
drupal_add_js('(function ($) { Drupal.flot = Drupal.flot || {}; })(jQuery);', array('type' => 'inline', 'scope' => JS_DEFAULT));
$added = true;
}
}
/**
* Data class for the flot API.
*
* Make some nested objects to keep things simple when creating a data series.
*/
class flotData {
public $data;
public $lines;
public $bars;
public $points;
function __construct($data) {
$this->data = $data;
$this->lines = new stdClass();
$this->bars = new stdClass();
$this->points = new stdClass();
$this->grid = new StdClass();
}
}
/**
* Style class for the flot API.
*
* Provides some sensible defaults and helper methods for managing axes.
*/
class flotStyle {
public $colors;
public $grid;
public $lines;
public $bars;
public $points;
function __construct() {
$this->lines = new StdClass();
$this->bars = new StdClass();
$this->points = new StdClass();
$this->lines->show = FALSE;
$this->bars->show = FALSE;
$this->points->show = FALSE;
$this->colors = array('#666', '#999', '#ccc');
$this->shadowSize = 0;
$this->grid = new StdClass();
$this->grid->labelMargin = 0;
$this->grid->tickColor = '#eee';
$this->grid->backgroundColor = '#f8f8f8';
$this->grid->borderWidth = 0;
$this->grid->hoverable = true;
$this->grid->autoHighlight = true;
$this->grid->clickable = false;
}
function axis_ticks($axis = 'yaxis', $ticks = array()) {
if (count($ticks)) {
$this->{$axis} = new StdClass();
$this->{$axis}->ticks = $ticks;
}
}
function axis_range($axis = 'yaxis', $range = array(), $granularity = 0) {
if (count($range)) {
$this->{$axis} = new StdClass();
$this->{$axis}->min = min($range);
$this->{$axis}->max = max($range);
if (is_numeric($granularity) && $granularity != 0) {
$tickSize = ($this->{$axis}->max - $this->{$axis}->min) / $granularity;
$this->{$axis}->tickSize = floor($tickSize);
}
}
}
}
/**
* Basic line style class for the flot.
*/
class flotStyleLine extends flotStyle {
function __construct() {
parent::__construct();
$this->lines->show = TRUE;
$this->lines->lineWidth = 1;
$this->lines->fill = .1;
}
}
/**
* Basic bar style class for the flot.
*/
class flotStyleBar extends flotStyle {
function __construct() {
parent::__construct();
$this->bars->show = TRUE;
$this->bars->lineWidth = 0;
$this->bars->fill = .5;
}
}
/**
* Points style class for the flot.
*/
class flotStylePoint extends flotStyle {
function __construct() {
parent::__construct();
$this->points->show = TRUE;
$this->points->lineWidth = 1;
$this->points->fill = .1;
}
}