|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Graphs |
|
4
|
|
|
* |
|
5
|
|
|
* This class handles building pretty report graphs |
|
6
|
|
|
* |
|
7
|
|
|
* @package Give |
|
8
|
|
|
* @subpackage Admin/Reports |
|
9
|
|
|
* @copyright Copyright (c) 2012, WordImpress |
|
10
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
11
|
|
|
* @since 1.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
// Exit if accessed directly. |
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
16
|
|
|
exit; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Give_Graph Class |
|
21
|
|
|
* |
|
22
|
|
|
* @since 1.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Give_Graph { |
|
25
|
|
|
|
|
26
|
|
|
/* |
|
27
|
|
|
|
|
28
|
|
|
Simple example: |
|
29
|
|
|
|
|
30
|
|
|
data format for each point: array( location on x, location on y ) |
|
31
|
|
|
|
|
32
|
|
|
$data = array( |
|
33
|
|
|
|
|
34
|
|
|
'Label' => array( |
|
35
|
|
|
array( 1, 5 ), |
|
36
|
|
|
array( 3, 8 ), |
|
37
|
|
|
array( 10, 2 ) |
|
38
|
|
|
), |
|
39
|
|
|
|
|
40
|
|
|
'Second Label' => array( |
|
41
|
|
|
array( 1, 7 ), |
|
42
|
|
|
array( 4, 5 ), |
|
43
|
|
|
array( 12, 8 ) |
|
44
|
|
|
) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$graph = new Give_Graph( $data ); |
|
48
|
|
|
$graph->display(); |
|
49
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Data to graph |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
* @since 1.0 |
|
57
|
|
|
*/ |
|
58
|
|
|
private $data; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Unique ID for the graph |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
* @since 1.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
private $id = ''; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Graph options |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
* @since 1.0 |
|
73
|
|
|
*/ |
|
74
|
|
|
private $options = array(); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get things started |
|
78
|
|
|
* |
|
79
|
|
|
* @since 1.0 |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $_data |
|
82
|
|
|
* @param array $options |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __construct( $_data, $options = array() ) { |
|
85
|
|
|
|
|
86
|
|
|
$this->data = $_data; |
|
87
|
|
|
|
|
88
|
|
|
// Generate unique ID |
|
89
|
|
|
$this->id = md5( rand() ); |
|
90
|
|
|
|
|
91
|
|
|
// Setup default options; |
|
92
|
|
|
$this->options = apply_filters( 'give_graph_args', array( |
|
93
|
|
|
'y_mode' => null, |
|
94
|
|
|
'x_mode' => null, |
|
95
|
|
|
'y_decimals' => 0, |
|
96
|
|
|
'x_decimals' => 0, |
|
97
|
|
|
'y_position' => 'right', |
|
98
|
|
|
'time_format' => '%d/%b', |
|
99
|
|
|
'ticksize_unit' => 'day', |
|
100
|
|
|
'ticksize_num' => 1, |
|
101
|
|
|
'multiple_y_axes' => false, |
|
102
|
|
|
'bgcolor' => '#f9f9f9', |
|
103
|
|
|
'bordercolor' => '#eee', |
|
104
|
|
|
'color' => '#bbb', |
|
105
|
|
|
'borderwidth' => 1, |
|
106
|
|
|
'bars' => true, |
|
107
|
|
|
'lines' => false, |
|
108
|
|
|
'points' => true, |
|
109
|
|
|
'dataType' => array() |
|
110
|
|
|
) ); |
|
111
|
|
|
|
|
112
|
|
|
$this->options = wp_parse_args( $options, $this->options ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set an option |
|
117
|
|
|
* |
|
118
|
|
|
* @param $key The option key to set |
|
119
|
|
|
* @param $value The value to assign to the key |
|
120
|
|
|
* |
|
121
|
|
|
* @since 1.0 |
|
122
|
|
|
*/ |
|
123
|
|
|
public function set( $key, $value ) { |
|
124
|
|
|
$this->options[ $key ] = $value; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get an option |
|
129
|
|
|
* |
|
130
|
|
|
* @param $key The option key to get |
|
131
|
|
|
* |
|
132
|
|
|
* @since 1.0 |
|
133
|
|
|
*/ |
|
134
|
|
|
public function get( $key ) { |
|
135
|
|
|
return isset( $this->options[ $key ] ) ? $this->options[ $key ] : false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get graph data |
|
140
|
|
|
* |
|
141
|
|
|
* @since 1.0 |
|
142
|
|
|
*/ |
|
143
|
|
|
public function get_data() { |
|
144
|
|
|
return apply_filters( 'give_get_graph_data', $this->data, $this ); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Load the graphing library script |
|
149
|
|
|
* |
|
150
|
|
|
* @since 1.0 |
|
151
|
|
|
*/ |
|
152
|
|
|
public function load_scripts() { |
|
153
|
|
|
// Use minified libraries if SCRIPT_DEBUG is turned off |
|
154
|
|
|
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
|
155
|
|
|
|
|
156
|
|
|
wp_register_script( 'jquery-flot-orderbars', GIVE_PLUGIN_URL . 'assets/js/plugins/jquery.flot.orderBars' . $suffix . '.js', array('jquery-flot'), GIVE_VERSION ); |
|
|
|
|
|
|
157
|
|
|
wp_enqueue_script( 'jquery-flot-orderbars' ); |
|
158
|
|
|
|
|
159
|
|
|
wp_register_script( 'jquery-flot-time', GIVE_PLUGIN_URL . 'assets/js/plugins/jquery.flot.time' . $suffix . '.js', array('jquery-flot'), GIVE_VERSION ); |
|
|
|
|
|
|
160
|
|
|
wp_enqueue_script( 'jquery-flot-time' ); |
|
161
|
|
|
|
|
162
|
|
|
wp_register_script( 'jquery-flot-resize', GIVE_PLUGIN_URL . 'assets/js/plugins/jquery.flot.resize' . $suffix . '.js', array('jquery-flot'), GIVE_VERSION ); |
|
|
|
|
|
|
163
|
|
|
wp_enqueue_script( 'jquery-flot-resize' ); |
|
164
|
|
|
|
|
165
|
|
|
wp_register_script( 'jquery-flot', GIVE_PLUGIN_URL . 'assets/js/plugins/jquery.flot' . $suffix . '.js', false, GIVE_VERSION ); |
|
166
|
|
|
wp_enqueue_script( 'jquery-flot' ); |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Build the graph and return it as a string |
|
172
|
|
|
* |
|
173
|
|
|
* @var array |
|
174
|
|
|
* @since 1.0 |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function build_graph() { |
|
178
|
|
|
|
|
179
|
|
|
$yaxis_count = 1; |
|
180
|
|
|
|
|
181
|
|
|
$this->load_scripts(); |
|
182
|
|
|
|
|
183
|
|
|
ob_start(); |
|
184
|
|
|
?> |
|
185
|
|
|
<script type="text/javascript"> |
|
186
|
|
|
|
|
187
|
|
|
jQuery( document ).ready( function ( $ ) { |
|
188
|
|
|
$.plot( |
|
189
|
|
|
$( "#give-graph-<?php echo $this->id; ?>" ), |
|
|
|
|
|
|
190
|
|
|
[ |
|
191
|
|
|
<?php |
|
192
|
|
|
$order = 0; |
|
193
|
|
|
foreach( $this->get_data() as $label => $data ) : |
|
|
|
|
|
|
194
|
|
|
?> |
|
195
|
|
|
{ |
|
196
|
|
|
label : "<?php echo esc_attr( $label ); ?>", |
|
197
|
|
|
id : "<?php echo sanitize_key( $label ); ?>", |
|
198
|
|
|
dataType : '<?php echo ( ! empty( $this->options['dataType'][$order] ) ? $this->options['dataType'][$order] : 'count' ); ?>', |
|
|
|
|
|
|
199
|
|
|
// data format is: [ point on x, value on y ] |
|
200
|
|
|
data : [<?php foreach( $data as $point ) { echo '[' . implode( ',', $point ) . '],'; } ?>], |
|
|
|
|
|
|
201
|
|
|
points: { |
|
202
|
|
|
show: <?php echo $this->options['points'] ? 'true' : 'false'; ?>, |
|
|
|
|
|
|
203
|
|
|
}, |
|
204
|
|
|
bars : { |
|
205
|
|
|
show : <?php echo $this->options['bars'] ? 'true' : 'false'; ?>, |
|
|
|
|
|
|
206
|
|
|
barWidth: 100, |
|
207
|
|
|
order: <?php echo $order++; ?>, |
|
|
|
|
|
|
208
|
|
|
align : 'center' |
|
209
|
|
|
}, |
|
210
|
|
|
lines : { |
|
211
|
|
|
show : <?php echo $this->options['lines'] ? 'true' : 'false'; ?>, |
|
|
|
|
|
|
212
|
|
|
fill : true, |
|
213
|
|
|
fillColor: {colors: [{opacity: 0.4}, {opacity: 0.1}]} |
|
214
|
|
|
}, |
|
215
|
|
|
<?php if( $this->options[ 'multiple_y_axes' ] ) : ?> |
|
|
|
|
|
|
216
|
|
|
yaxis : <?php echo $yaxis_count; ?> |
|
|
|
|
|
|
217
|
|
|
<?php endif; ?> |
|
218
|
|
|
|
|
219
|
|
|
}, |
|
220
|
|
|
|
|
221
|
|
|
<?php $yaxis_count++; endforeach; ?> |
|
222
|
|
|
|
|
223
|
|
|
], |
|
224
|
|
|
{ |
|
225
|
|
|
// Options |
|
226
|
|
|
grid: { |
|
227
|
|
|
show : true, |
|
228
|
|
|
aboveData : false, |
|
229
|
|
|
color : "<?php echo $this->options[ 'color' ]; ?>", |
|
|
|
|
|
|
230
|
|
|
backgroundColor: "<?php echo $this->options[ 'bgcolor' ]; ?>", |
|
|
|
|
|
|
231
|
|
|
borderColor : "<?php echo $this->options[ 'bordercolor' ]; ?>", |
|
|
|
|
|
|
232
|
|
|
borderWidth : <?php echo absint( $this->options[ 'borderwidth' ] ); ?>, |
|
|
|
|
|
|
233
|
|
|
clickable : false, |
|
234
|
|
|
hoverable : true |
|
235
|
|
|
}, |
|
236
|
|
|
|
|
237
|
|
|
colors: ["#66bb6a", "#546e7a"], //Give Colors |
|
238
|
|
|
|
|
239
|
|
|
xaxis: { |
|
240
|
|
|
mode : "<?php echo $this->options['x_mode']; ?>", |
|
|
|
|
|
|
241
|
|
|
timeFormat : "<?php echo $this->options['x_mode'] == 'time' ? $this->options['time_format'] : ''; ?>", |
|
|
|
|
|
|
242
|
|
|
tickSize : "<?php echo $this->options['x_mode'] == 'time' ? '' : 1; ?>", |
|
|
|
|
|
|
243
|
|
|
<?php if( $this->options['x_mode'] != 'time' ) : ?> |
|
|
|
|
|
|
244
|
|
|
tickDecimals: <?php echo $this->options['x_decimals']; ?> |
|
|
|
|
|
|
245
|
|
|
<?php endif; ?> |
|
246
|
|
|
}, |
|
247
|
|
|
yaxis: { |
|
248
|
|
|
position : 'right', |
|
249
|
|
|
min : 0, |
|
250
|
|
|
mode : "<?php echo $this->options['y_mode']; ?>", |
|
|
|
|
|
|
251
|
|
|
timeFormat : "<?php echo $this->options['y_mode'] == 'time' ? $this->options['time_format'] : ''; ?>", |
|
|
|
|
|
|
252
|
|
|
<?php if( $this->options['y_mode'] != 'time' ) : ?> |
|
|
|
|
|
|
253
|
|
|
tickDecimals: <?php echo $this->options['y_decimals']; ?> |
|
|
|
|
|
|
254
|
|
|
<?php endif; ?> |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
); |
|
258
|
|
|
|
|
259
|
|
|
function give_flot_tooltip( x, y, contents ) { |
|
260
|
|
|
$( '<div id="give-flot-tooltip">' + contents + '</div>' ).css( { |
|
261
|
|
|
position : 'absolute', |
|
262
|
|
|
display : 'none', |
|
263
|
|
|
top : y + 5, |
|
264
|
|
|
left : x + 5, |
|
265
|
|
|
border : '1px solid #fdd', |
|
266
|
|
|
padding : '2px', |
|
267
|
|
|
'background-color': '#fee', |
|
268
|
|
|
opacity : 0.80 |
|
269
|
|
|
} ).appendTo( "body" ).fadeIn( 200 ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
var previousPoint = null; |
|
273
|
|
|
$( "#give-graph-<?php echo $this->id; ?>" ).bind( "plothover", function ( event, pos, item ) { |
|
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
$( "#x" ).text( pos.x.toFixed( 2 ) ); |
|
276
|
|
|
$( "#y" ).text( pos.y.toFixed( 2 ) ); |
|
277
|
|
|
if ( item ) { |
|
278
|
|
|
if ( previousPoint !== item.dataIndex ) { |
|
279
|
|
|
previousPoint = item.dataIndex; |
|
280
|
|
|
$( "#give-flot-tooltip" ).remove(); |
|
281
|
|
|
var x = item.datapoint[0].toFixed( 2 ), |
|
282
|
|
|
y = accounting.formatMoney( item.datapoint[1].toFixed( give_vars.currency_decimals ), '', give_vars.currency_decimals, give_vars.thousands_separator, give_vars.decimal_separator ); |
|
283
|
|
|
|
|
284
|
|
|
if ( item.series.dataType.length && item.series.dataType === 'amount' ) { |
|
285
|
|
|
|
|
286
|
|
|
if ( give_vars.currency_pos === 'before' ) { |
|
287
|
|
|
|
|
288
|
|
|
give_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + give_vars.currency_sign + y ); |
|
289
|
|
|
} else { |
|
290
|
|
|
give_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y + give_vars.currency_sign ); |
|
291
|
|
|
} |
|
292
|
|
|
} else { |
|
293
|
|
|
give_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + parseInt( y ) ); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} else { |
|
297
|
|
|
$( "#give-flot-tooltip" ).remove(); |
|
298
|
|
|
previousPoint = null; |
|
299
|
|
|
} |
|
300
|
|
|
} ); |
|
301
|
|
|
|
|
302
|
|
|
} ); |
|
303
|
|
|
|
|
304
|
|
|
</script> |
|
305
|
|
|
<div id="give-graph-<?php echo $this->id; ?>" class="give-graph" style="height: 300px;"></div> |
|
|
|
|
|
|
306
|
|
|
<?php |
|
307
|
|
|
return ob_get_clean(); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Output the final graph |
|
312
|
|
|
* |
|
313
|
|
|
* @since 1.0 |
|
314
|
|
|
*/ |
|
315
|
|
|
public function display() { |
|
316
|
|
|
/** |
|
317
|
|
|
* Fires before displaying the final graph. |
|
318
|
|
|
* |
|
319
|
|
|
* @since 1.0 |
|
320
|
|
|
* |
|
321
|
|
|
* @param Give_Graph $this Graph object. |
|
322
|
|
|
*/ |
|
323
|
|
|
do_action( 'give_before_graph', $this ); |
|
324
|
|
|
|
|
325
|
|
|
// Build the graph. |
|
326
|
|
|
echo $this->build_graph(); |
|
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Fires after displaying the final graph. |
|
330
|
|
|
* |
|
331
|
|
|
* @since 1.0 |
|
332
|
|
|
* |
|
333
|
|
|
* @param Give_Graph $this Graph object. |
|
334
|
|
|
*/ |
|
335
|
|
|
do_action( 'give_after_graph', $this ); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
} |
|
339
|
|
|
|