1: <?php
2: /**
3: * Reports
4: *
5: * This class handles building report graphs
6: *
7: * @package EPL
8: * @subpackage Admin/Classes/ReportsGraph
9: * @copyright Copyright (c) 2020, Merv Barrett
10: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11: * @since 3.0
12: */
13:
14: // Exit if accessed directly.
15: if ( ! defined( 'ABSPATH' ) ) {
16: exit;
17: }
18:
19: /**
20: * EPL_Graph Class
21: *
22: * @since 3.0
23: */
24: class EPL_Graph {
25:
26: /**
27: * Data to graph
28: *
29: * @var array
30: * @since 3.0
31: */
32: private $data;
33:
34: /**
35: * Unique ID for the graph
36: *
37: * @var string
38: * @since 3.0
39: */
40: private $id = '';
41:
42: /**
43: * Graph options
44: *
45: * @var array
46: * @since 3.0
47: */
48: private $options = array();
49:
50: /**
51: * Get things started
52: *
53: * @since 3.0
54: * @param string $_data the data.
55: */
56: public function __construct( $_data ) {
57:
58: $this->data = $_data;
59:
60: // Generate unique ID.
61: $this->id = 'a' . md5( wp_rand() );
62:
63: // Setup default options.
64: $this->options = array(
65: 'y_mode' => null,
66: 'x_mode' => null,
67: 'y_decimals' => 0,
68: 'x_decimals' => 0,
69: 'y_position' => 'right',
70: 'time_format' => '%d/%b',
71: 'ticksize_unit' => 'day',
72: 'ticksize_num' => 1,
73: 'multiple_y_axes' => false,
74: 'bgcolor' => '#f9f9f9',
75: 'bordercolor' => '#ccc',
76: 'color' => '#bbb',
77: 'borderwidth' => 2,
78: 'bars' => false,
79: 'lines' => true,
80: 'points' => true,
81: );
82: }
83:
84: /**
85: * Set an option
86: *
87: * @since 3.0.0
88: * @param string $key The option key to set.
89: * @param string $value The value to assign to the key.
90: */
91: public function set( $key, $value ) {
92: $this->options[ $key ] = $value;
93: }
94:
95: /**
96: * Get an option
97: *
98: * @param string $key The option key to get.
99: *
100: * @return bool|mixed
101: * @since 3.0
102: */
103: public function get( $key ) {
104: return isset( $this->options[ $key ] ) ? $this->options[ $key ] : false;
105: }
106:
107: /**
108: * Get graph data
109: *
110: * @since 3.0
111: */
112: public function get_data() {
113: return apply_filters( 'epl_get_graph_data', $this->data, $this );
114: }
115:
116: /**
117: * Load the graphing library script
118: *
119: * @since 3.0
120: */
121: public function load_scripts() {
122: // Use minified libraries if SCRIPT_DEBUG is turned off.
123: $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
124: wp_enqueue_script( 'epl-jquery-flot', EPL_PLUGIN_URL . 'lib/assets/js/jquery-flot' . $suffix . '.js', array(), EPL_PROPERTY_VER, true );
125:
126: do_action( 'epl_graph_load_scripts' );
127: }
128:
129: /**
130: * Build the graph and return it as a string
131: *
132: * @since 3.0
133: * @return string
134: */
135: public function build_graph() {
136:
137: $yaxis_count = 1;
138:
139: $this->load_scripts();
140:
141: ob_start();
142: ?>
143: <script type="text/javascript">
144: jQuery( document ).ready( function($) {
145: $.plot(
146: $("#epl-graph-<?php echo esc_attr( $this->id ); ?>"),
147: [
148: <?php foreach ( $this->get_data() as $label => $data ) : ?>
149: {
150: label: "<?php echo esc_attr( $data['label'] ); ?>",
151: id: "<?php echo esc_attr( sanitize_key( $data['id'] ) ); ?>",
152:
153: <?php if ( ! is_null( $data['color'] ) ) : ?>
154:
155: color: "<?php echo esc_attr( $data['color'] ); ?>",
156:
157: <?php endif; ?>
158: // data format is: [ point on x, value on y ]
159: data: [
160: <?php
161: foreach ( $data['data'] as $point ) {
162: echo esc_attr( '[' . implode( ',', $point ) . '],' );
163: }
164: ?>
165: ],
166: points: {
167: show: <?php echo $this->options['points'] ? 'true' : 'false'; ?>,
168: },
169: bars: {
170: show: <?php echo $this->options['bars'] ? 'true' : 'false'; ?>,
171: barWidth: 12,
172: aling: 'center',
173: },
174: lines: {
175: show: <?php echo $this->options['lines'] ? 'true' : 'false'; ?>
176: },
177: <?php if ( $this->options['multiple_y_axes'] ) : ?>
178: yaxis: <?php echo esc_attr( $yaxis_count ); ?>
179: <?php endif; ?>
180: },
181: <?php
182: ++$yaxis_count;
183: endforeach;
184: ?>
185: ],
186: {
187: // Options
188: grid: {
189: show: true,
190: aboveData: false,
191: color: "<?php echo esc_attr( $this->options['color'] ); ?>",
192: backgroundColor: "<?php echo esc_attr( $this->options['bgcolor'] ); ?>",
193: borderColor: "<?php echo esc_attr( $this->options['bordercolor'] ); ?>",
194: borderWidth: <?php echo esc_attr( absint( $this->options['borderwidth'] ) ); ?>,
195: clickable: false,
196: hoverable: true
197: },
198: xaxis: {
199: mode: "<?php echo esc_attr( $this->options['x_mode'] ); ?>",
200: timeFormat: "<?php echo 'time' === $this->options['x_mode'] ? esc_attr( $this->options['time_format'] ) : ''; ?>",
201: tickSize: "<?php echo 'time' === $this->options['x_mode'] ? '' : esc_attr( $this->options['ticksize_num'] ); ?>",
202: <?php if ( 'time' !== $this->options['x_mode'] ) : ?>
203: tickDecimals: <?php echo esc_attr( $this->options['x_decimals'] ); ?>
204: <?php endif; ?>
205: },
206: yaxis: {
207: position: 'right',
208: axisLabelColor: '#ff0000',
209: min: 0,
210: mode: "<?php echo esc_attr( $this->options['y_mode'] ); ?>",
211: timeFormat: "<?php echo 'time' === $this->options['y_mode'] ? esc_attr( $this->options['time_format'] ) : ''; ?>",
212: <?php if ( 'time' !== $this->options['y_mode'] ) : ?>
213: tickDecimals: <?php echo esc_attr( $this->options['y_decimals'] ); ?>
214: <?php endif; ?>
215: }
216: }
217:
218: );
219:
220: function epl_flot_tooltip(x, y, contents) {
221: $('<div id="epl-flot-tooltip">' + contents + '</div>').css( {
222: position: 'absolute',
223: display: 'none',
224: top: y + 5,
225: left: x + 5,
226: border: '1px solid #fdd',
227: padding: '2px',
228: 'background-color': '#fee',
229: opacity: 0.80
230: }).appendTo("body").fadeIn(200);
231: }
232:
233: var previousPoint = null;
234: $("#epl-graph-<?php echo esc_attr( $this->id ); ?>").bind("plothover", function (event, pos, item) {
235: $("#x").text(pos.x.toFixed(2));
236: $("#y").text(pos.y.toFixed(2));
237: if (item) {
238: if (previousPoint != item.dataIndex) {
239: previousPoint = item.dataIndex;
240: $("#epl-flot-tooltip").remove();
241: var x = item.datapoint[0].toFixed(2),
242: y = item.datapoint[1].toFixed(2);
243: if ( item.series.id == 'earnings' ) {
244: if ( epl_vars.currency_pos == 'before' ) {
245: epl_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + epl_vars.currency_sign + y );
246: } else {
247: epl_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y + epl_vars.currency_sign );
248: }
249: } else {
250: epl_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y.replace( '.00', '' ) );
251: }
252: }
253: } else {
254: $("#epl-flot-tooltip").remove();
255: previousPoint = null;
256: }
257: });
258:
259: });
260:
261: </script>
262: <div id="epl-graph-<?php echo esc_attr( $this->id ); ?>" class="epl-graph" style="height: 300px;"></div>
263: <?php
264: return ob_get_clean();
265: }
266:
267: /**
268: * Output the final graph
269: *
270: * @since 3.0
271: */
272: public function display() {
273: do_action( 'epl_before_graph', $this );
274: echo $this->build_graph(); //phpcs:ignore WordPress.Security.EscapeOutput
275: do_action( 'epl_after_graph', $this );
276: }
277: }
278: