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