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