PageRenderTime 43ms CodeModel.GetById 25ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/shared/classes/class.charting.php

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