diff --git a/includes/charts.php b/includes/charts.php new file mode 100644 index 00000000..6f3a4ebe --- /dev/null +++ b/includes/charts.php @@ -0,0 +1,179 @@ + $items ) { + $sorted[ $type ] = $this->count_by_field( 'created', $items, array( $this, 'collapse_dates' ) ); + } + + $sorted = $this->pad_fields( $sorted ); + + foreach ( $sorted as $type => &$items ) { + ksort( $items ); + } + + $coordinates = array(); + + foreach ( $sorted as $line_name => $points ) { + $line_data = array( + 'key' => $line_name, + 'values' => array(), + ); + + foreach ( $points as $x => $y ) { + $line_data['values'][] = array( + 'x' => $x, + 'y' => $y, + ); + } + + $coordinates[] = $line_data; + } + + return $coordinates; + } + + public function pie_chart_coordinates( $records, $args ) { + if ( 'pie' !== $args['chart_type'] ) { + return $records; + } + + $counts = array(); + + foreach ( $records as $type => $items ) { + $counts[] = array( + 'key' => $type, + 'value' => count( $items ), + ); + } + + return $counts; + } + + public function bar_chart_coordinates( $records, $args ) { + if ( 'multibar' !== $args['chart_type'] ) { + return $records; + } + + $sorted = array(); + + // Get date count for each sort + foreach ( $records as $type => $items ) { + $sorted[ $type ] = $this->count_by_field( 'created', $items, array( $this, 'collapse_dates' ) ); + } + + $sorted = $this->pad_fields( $sorted ); + + foreach ( $sorted as $type => &$items ) { + ksort( $items ); + } + + $coordinates = array(); + + foreach ( $sorted as $line_name => $points ) { + $line_data = array( + 'key' => $line_name, + 'values' => array(), + ); + + foreach ( $points as $x => $y ) { + $line_data['values'][] = array( + 'x' => $x, + 'y' => $y, + ); + } + + $coordinates[] = $line_data; + } + + return $coordinates; + } + + /** + * Counts the number of objects with similar field properties in an array + * @return array + */ + public function count_by_field( $field, $records, $callback = '' ) { + $sorted = $this->group_by_field( $field, $records, $callback ); + $counts = array(); + + foreach ( array_keys( $sorted ) as $key ) { + $counts[ $key ] = count( $sorted[ $key ] ); + } + + return $counts; + } + + /** + * Groups objects with similar field properties into arrays + * @return array + */ + public function group_by_field( $field, $records, $callback = '' ) { + $sorted = array(); + + foreach ( $records as $record ) { + $key = $record->$field; + + if ( is_callable( $callback ) ) { + $key = call_user_func( $callback, $key ); + } + + if ( array_key_exists( $key, $sorted ) && is_array( $sorted[ $key ] ) ) { + $sorted[ $key ][] = $record; + } else { + $sorted[ $key ] = array( $record ); + } + } + + return $sorted; + } + + /** + * Adds blank fields for all keys present in any array + * @return array + */ + public function pad_fields( $records ) { + $keys = array(); + + foreach ( $records as $dataset ) { + $keys = array_unique( array_merge( $keys, array_keys( $dataset ) ) ); + } + + $new_records = array(); + + foreach ( $keys as $key ) { + foreach ( $records as $data_key => $dataset ) { + if ( ! array_key_exists( $data_key, $new_records ) ) { + $new_records[ $data_key ] = array(); + } + + $new_records[ $data_key ][ $key ] = isset( $records[ $data_key ][ $key ] ) ? $records[ $data_key ][ $key ] : 0; + } + } + + return $new_records; + } + + /** + * Used to group data points by day + */ + protected function collapse_dates( $date ) { + return strtotime( date( 'Y-m-d', strtotime( $date ) ) ); + } + +} diff --git a/includes/meta-boxes.php b/includes/meta-boxes.php index 45758b79..c2da3f2d 100644 --- a/includes/meta-boxes.php +++ b/includes/meta-boxes.php @@ -32,16 +32,12 @@ class WP_Stream_Reports_Metaboxes { public function __construct() { // Get all sections from the database self::$sections = WP_Stream_Reports_Settings::get_user_options( 'sections' ); + $this->charts = new WP_Stream_Reports_Charts(); // Load records add_filter( 'wp_stream_reports_load_records', array( $this, 'sort_coordinates_by_count' ), 10, 2 ); add_filter( 'wp_stream_reports_load_records', array( $this, 'limit_coordinates' ), 10, 2 ); - // Make charts - add_filter( 'wp_stream_reports_make_chart', array( $this, 'pie_chart_coordinates' ), 10, 2 ); - add_filter( 'wp_stream_reports_make_chart', array( $this, 'bar_chart_coordinates' ), 10, 2 ); - add_filter( 'wp_stream_reports_make_chart', array( $this, 'line_chart_coordinates' ), 10, 2 ); - // Finalize charts add_filter( 'wp_stream_reports_finalize_chart', array( $this, 'translate_labels' ), 10, 2 ); add_filter( 'wp_stream_reports_finalize_chart', array( $this, 'apply_chart_settings' ), 10, 2 ); @@ -287,99 +283,6 @@ public function get_chart_coordinates( $args ) { return apply_filters( 'wp_stream_reports_finalize_chart', $coordinates, $args ); } - public function line_chart_coordinates( $records, $args ) { - if ( 'line' !== $args['chart_type'] ) { - return $records; - } - - $sorted = array(); - - // Get date count for each sort - foreach ( $records as $type => $items ) { - $sorted[ $type ] = $this->count_by_field( 'created', $items, array( $this, 'collapse_dates' ) ); - } - - $sorted = $this->pad_fields( $sorted ); - - foreach ( $sorted as $type => &$items ) { - ksort( $items ); - } - - $coordinates = array(); - foreach ( $sorted as $line_name => $points ) { - $line_data = array( - 'key' => $line_name, - 'values' => array(), - ); - - foreach ( $points as $x => $y ) { - $line_data['values'][] = array( - 'x' => $x, - 'y' => $y, - ); - } - - $coordinates[] = $line_data; - } - - return $coordinates; - } - - public function pie_chart_coordinates( $records, $args ) { - if ( 'pie' !== $args['chart_type'] ) { - return $records; - } - $counts = array(); - - foreach ( $records as $type => $items ) { - $counts[] = array( - 'key' => $type, - 'value' => count( $items ), - ); - } - - return $counts; - } - - public function bar_chart_coordinates( $records, $args ) { - if ( 'multibar' !== $args['chart_type'] ) { - return $records; - } - - $sorted = array(); - - // Get date count for each sort - foreach ( $records as $type => $items ) { - $sorted[ $type ] = $this->count_by_field( 'created', $items, array( $this, 'collapse_dates' ) ); - } - - $sorted = $this->pad_fields( $sorted ); - - foreach ( $sorted as $type => &$items ) { - ksort( $items ); - } - - $coordinates = array(); - - foreach ( $sorted as $line_name => $points ) { - $line_data = array( - 'key' => $line_name, - 'values' => array(), - ); - - foreach ( $points as $x => $y ) { - $line_data['values'][] = array( - 'x' => $x, - 'y' => $y, - ); - } - - $coordinates[] = $line_data; - } - - return $coordinates; - } - public function apply_chart_settings( $coordinates, $args ) { foreach ( $coordinates as $key => $dataset ) { if ( in_array( $key, $args['disabled'] ) ) { @@ -610,7 +513,7 @@ public function load_metabox_records( $args, $date_interval ) { } } } - $sorted = $this->group_by_field( $grouping_field, $unsorted ); + $sorted = $this->charts->group_by_field( $grouping_field, $unsorted ); return $sorted; } @@ -678,78 +581,7 @@ public function limit_coordinates( $records, $args ) { $top_elements['report-others'] = $other_element; return $top_elements; - } - - /** - * Groups objects with similar field properties into arrays - * @return array - */ - protected function group_by_field( $field, $records, $callback = '' ) { - $sorted = array(); - foreach ( $records as $record ) { - $key = $record->$field; - - if ( is_callable( $callback ) ) { - $key = call_user_func( $callback, $key ); - } - - if ( array_key_exists( $key, $sorted ) && is_array( $sorted[ $key ] ) ) { - $sorted[ $key ][] = $record; - } else { - $sorted[ $key ] = array( $record ); - } - } - - return $sorted; - } - - /** - * Counts the number of objects with similar field properties in an array - * @return array - */ - protected function count_by_field( $field, $records, $callback = '' ) { - $sorted = $this->group_by_field( $field, $records, $callback ); - $counts = array(); - - foreach ( array_keys( $sorted ) as $key ) { - $counts[ $key ] = count( $sorted[ $key ] ); - } - - return $counts; - } - - /** - * Adds blank fields for all keys present in any array - * @return array - */ - protected function pad_fields( $records ) { - $keys = array(); - - foreach ( $records as $dataset ) { - $keys = array_unique( array_merge( $keys, array_keys( $dataset ) ) ); - } - - $new_records = array(); - - foreach ( $keys as $key ) { - foreach ( $records as $data_key => $dataset ) { - if ( ! array_key_exists( $data_key, $new_records ) ) { - $new_records[ $data_key ] = array(); - } - - $new_records[ $data_key ][ $key ] = isset( $records[ $data_key ][ $key ] ) ? $records[ $data_key ][ $key ] : 0; - } - } - - return $new_records; - } - - /** - * Used to group data points by day - */ - protected function collapse_dates( $date ) { - return strtotime( date( 'Y-m-d', strtotime( $date ) ) ); - } + } /** * Creates a title generated from the arguments for the chart diff --git a/stream-reports.php b/stream-reports.php index 02378606..791b93ec 100644 --- a/stream-reports.php +++ b/stream-reports.php @@ -128,6 +128,7 @@ public function load() { // Load sections here require_once WP_STREAM_REPORTS_INC_DIR . 'meta-boxes.php'; + require_once WP_STREAM_REPORTS_INC_DIR . 'charts.php'; add_action( 'init', array( 'WP_Stream_Reports_Metaboxes', 'get_instance' ), 12 ); // Load Interval/Date class.