Skip to content

Commit

Permalink
Merge pull request #99 from x-team/2.0.0-refactor
Browse files Browse the repository at this point in the history
Pull chart code from meta-boxes.php
  • Loading branch information
frankiejarrett committed Jun 9, 2014
2 parents 53fb412 + d0e3147 commit 8e80f87
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 171 deletions.
179 changes: 179 additions & 0 deletions includes/charts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

class WP_Stream_Reports_Charts {

public function __construct() {
// 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 );
}

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;
}

/**
* 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 ) ) );
}

}
174 changes: 3 additions & 171 deletions includes/meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions stream-reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8e80f87

Please sign in to comment.