2013-06-27 17:39:55 +00:00
|
|
|
<?php
|
2017-11-07 07:12:40 +00:00
|
|
|
/**
|
|
|
|
* Sales by category report functionality
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Admin/Reporting
|
|
|
|
*/
|
2016-01-05 11:18:58 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2017-11-07 07:12:40 +00:00
|
|
|
exit; // Exit if accessed directly.
|
2016-01-05 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WC_Report_Sales_By_Category
|
2014-02-14 13:02:37 +00:00
|
|
|
*
|
2014-08-31 07:19:13 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin/Reports
|
2014-02-14 13:02:37 +00:00
|
|
|
* @version 2.1.0
|
2013-06-27 17:39:55 +00:00
|
|
|
*/
|
|
|
|
class WC_Report_Sales_By_Category extends WC_Admin_Report {
|
|
|
|
|
2016-01-05 11:18:58 +00:00
|
|
|
/**
|
2016-12-19 11:28:09 +00:00
|
|
|
* Chart colors.
|
2016-01-05 11:18:58 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-20 11:07:31 +00:00
|
|
|
public $chart_colours = array();
|
2016-01-05 11:18:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Categories ids.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-01-20 09:53:33 +00:00
|
|
|
public $show_categories = array();
|
2016-01-05 11:18:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Item sales.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-01-20 09:53:33 +00:00
|
|
|
private $item_sales = array();
|
2016-01-05 11:18:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Item sales and times.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-01-20 09:53:33 +00:00
|
|
|
private $item_sales_and_times = array();
|
2013-06-27 17:39:55 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Constructor.
|
2013-06-27 17:39:55 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2015-01-20 09:53:33 +00:00
|
|
|
if ( isset( $_GET['show_categories'] ) ) {
|
|
|
|
$this->show_categories = is_array( $_GET['show_categories'] ) ? array_map( 'absint', $_GET['show_categories'] ) : array( absint( $_GET['show_categories'] ) );
|
2014-01-21 22:14:26 +00:00
|
|
|
}
|
2013-06-27 17:39:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 09:43:26 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get all product ids in a category (and its children).
|
2014-08-31 07:19:13 +00:00
|
|
|
*
|
2017-11-07 07:12:40 +00:00
|
|
|
* @param int $category_id Category ID.
|
2014-04-25 09:43:26 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_products_in_category( $category_id ) {
|
|
|
|
$term_ids = get_term_children( $category_id, 'product_cat' );
|
|
|
|
$term_ids[] = $category_id;
|
|
|
|
$product_ids = get_objects_in_term( $term_ids, 'product_cat' );
|
|
|
|
|
|
|
|
return array_unique( apply_filters( 'woocommerce_report_sales_by_category_get_products_in_category', $product_ids, $category_id ) );
|
|
|
|
}
|
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get the legend for the main chart sidebar.
|
2014-08-31 07:19:13 +00:00
|
|
|
*
|
2013-06-27 17:39:55 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_chart_legend() {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( empty( $this->show_categories ) ) {
|
2013-06-27 17:39:55 +00:00
|
|
|
return array();
|
2014-01-21 22:14:26 +00:00
|
|
|
}
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2014-04-25 09:43:26 +00:00
|
|
|
$legend = array();
|
|
|
|
$index = 0;
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2014-01-21 22:14:26 +00:00
|
|
|
foreach ( $this->show_categories as $category ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2014-04-25 09:43:26 +00:00
|
|
|
$category = get_term( $category, 'product_cat' );
|
|
|
|
$total = 0;
|
|
|
|
$product_ids = $this->get_products_in_category( $category->term_id );
|
2013-06-27 17:39:55 +00:00
|
|
|
|
|
|
|
foreach ( $product_ids as $id ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
if ( isset( $this->item_sales[ $id ] ) ) {
|
2013-06-27 17:39:55 +00:00
|
|
|
$total += $this->item_sales[ $id ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
$legend[] = array(
|
2016-10-29 20:03:28 +00:00
|
|
|
/* translators: 1: total items sold 2: category name */
|
2016-09-01 20:50:14 +00:00
|
|
|
'title' => sprintf( __( '%1$s sales in %2$s', 'woocommerce' ), '<strong>' . wc_price( $total ) . '</strong>', $category->name ),
|
2016-12-20 11:07:31 +00:00
|
|
|
'color' => isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[0],
|
2016-08-27 01:46:45 +00:00
|
|
|
'highlight_series' => $index,
|
2013-06-27 20:34:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$index++;
|
|
|
|
}
|
2013-06-27 17:39:55 +00:00
|
|
|
|
|
|
|
return $legend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Output the report.
|
2013-06-27 17:39:55 +00:00
|
|
|
*/
|
|
|
|
public function output_report() {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
$ranges = array(
|
|
|
|
'year' => __( 'Year', 'woocommerce' ),
|
2016-10-12 10:16:30 +00:00
|
|
|
'last_month' => __( 'Last month', 'woocommerce' ),
|
|
|
|
'month' => __( 'This month', 'woocommerce' ),
|
|
|
|
'7day' => __( 'Last 7 days', 'woocommerce' ),
|
2013-06-27 17:39:55 +00:00
|
|
|
);
|
|
|
|
|
2016-12-20 11:07:31 +00:00
|
|
|
$this->chart_colours = array( '#3498db', '#34495e', '#1abc9c', '#2ecc71', '#f1c40f', '#e67e22', '#e74c3c', '#2980b9', '#8e44ad', '#2c3e50', '#16a085', '#27ae60', '#f39c12', '#d35400', '#c0392b' );
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2017-11-07 07:12:40 +00:00
|
|
|
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : '7day';
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2014-01-21 22:14:26 +00:00
|
|
|
if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
|
2013-07-30 10:12:42 +00:00
|
|
|
$current_range = '7day';
|
2014-01-21 22:14:26 +00:00
|
|
|
}
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2017-04-18 23:33:25 +00:00
|
|
|
$this->check_current_range_nonce( $current_range );
|
2013-07-30 10:12:42 +00:00
|
|
|
$this->calculate_current_range( $current_range );
|
2013-06-27 20:34:18 +00:00
|
|
|
|
2017-11-07 07:12:40 +00:00
|
|
|
// Get item sales data.
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( ! empty( $this->show_categories ) ) {
|
2013-06-27 17:39:55 +00:00
|
|
|
$order_items = $this->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'_product_id' => array(
|
|
|
|
'type' => 'order_item_meta',
|
|
|
|
'order_item_type' => 'line_item',
|
|
|
|
'function' => '',
|
2016-08-27 01:46:45 +00:00
|
|
|
'name' => 'product_id',
|
2013-06-27 17:39:55 +00:00
|
|
|
),
|
|
|
|
'_line_total' => array(
|
|
|
|
'type' => 'order_item_meta',
|
|
|
|
'order_item_type' => 'line_item',
|
2014-10-24 10:58:25 +00:00
|
|
|
'function' => 'SUM',
|
2016-08-27 01:46:45 +00:00
|
|
|
'name' => 'order_item_amount',
|
2013-06-27 20:34:18 +00:00
|
|
|
),
|
|
|
|
'post_date' => array(
|
|
|
|
'type' => 'post_data',
|
|
|
|
'function' => '',
|
2016-08-27 01:46:45 +00:00
|
|
|
'name' => 'post_date',
|
2013-06-27 20:34:18 +00:00
|
|
|
),
|
2013-06-27 17:39:55 +00:00
|
|
|
),
|
2014-10-24 10:58:25 +00:00
|
|
|
'group_by' => 'ID, product_id, post_date',
|
2013-06-27 17:39:55 +00:00
|
|
|
'query_type' => 'get_results',
|
2016-08-27 01:46:45 +00:00
|
|
|
'filter_range' => true,
|
2013-06-27 17:39:55 +00:00
|
|
|
) );
|
|
|
|
|
2014-08-31 07:19:13 +00:00
|
|
|
$this->item_sales = array();
|
2013-06-27 20:34:18 +00:00
|
|
|
$this->item_sales_and_times = array();
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2015-01-20 09:53:33 +00:00
|
|
|
if ( is_array( $order_items ) ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
foreach ( $order_items as $order_item ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
switch ( $this->chart_groupby ) {
|
|
|
|
case 'day' :
|
|
|
|
$time = strtotime( date( 'Ymd', strtotime( $order_item->post_date ) ) ) * 1000;
|
|
|
|
break;
|
|
|
|
case 'month' :
|
2015-01-20 09:53:33 +00:00
|
|
|
default :
|
2013-06-27 20:34:18 +00:00
|
|
|
$time = strtotime( date( 'Ym', strtotime( $order_item->post_date ) ) . '01' ) * 1000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->item_sales_and_times[ $time ][ $order_item->product_id ] = isset( $this->item_sales_and_times[ $time ][ $order_item->product_id ] ) ? $this->item_sales_and_times[ $time ][ $order_item->product_id ] + $order_item->order_item_amount : $order_item->order_item_amount;
|
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
$this->item_sales[ $order_item->product_id ] = isset( $this->item_sales[ $order_item->product_id ] ) ? $this->item_sales[ $order_item->product_id ] + $order_item->order_item_amount : $order_item->order_item_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 22:14:26 +00:00
|
|
|
include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
|
2013-06-27 17:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-05 11:18:58 +00:00
|
|
|
* Get chart widgets.
|
2014-08-31 07:19:13 +00:00
|
|
|
*
|
2013-06-27 17:39:55 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_chart_widgets() {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'title' => __( 'Categories', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'callback' => array( $this, 'category_widget' ),
|
2016-08-27 02:08:49 +00:00
|
|
|
),
|
2013-06-27 17:39:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-05 11:18:58 +00:00
|
|
|
* Output category widget.
|
2013-06-27 17:39:55 +00:00
|
|
|
*/
|
|
|
|
public function category_widget() {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
$categories = get_terms( 'product_cat', array( 'orderby' => 'name' ) );
|
|
|
|
?>
|
|
|
|
<form method="GET">
|
|
|
|
<div>
|
2015-08-05 18:08:15 +00:00
|
|
|
<select multiple="multiple" data-placeholder="<?php esc_attr_e( 'Select categories…', 'woocommerce' ); ?>" class="wc-enhanced-select" id="show_categories" name="show_categories[]" style="width: 205px;">
|
2013-06-27 17:39:55 +00:00
|
|
|
<?php
|
|
|
|
$r = array();
|
|
|
|
$r['pad_counts'] = 1;
|
|
|
|
$r['hierarchical'] = 1;
|
|
|
|
$r['hide_empty'] = 1;
|
|
|
|
$r['value'] = 'id';
|
|
|
|
$r['selected'] = $this->show_categories;
|
|
|
|
|
|
|
|
include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-dropdown-walker.php' );
|
|
|
|
|
2017-11-07 07:12:40 +00:00
|
|
|
echo wc_walk_category_dropdown_tree( $categories, 0, $r ); // @codingStandardsIgnoreLine
|
2013-06-27 17:39:55 +00:00
|
|
|
?>
|
|
|
|
</select>
|
2017-11-07 07:12:40 +00:00
|
|
|
<?php // @codingStandardsIgnoreStart ?>
|
|
|
|
<a href="#" class="select_none"><?php esc_html_e( 'None', 'woocommerce' ); ?></a>
|
|
|
|
<a href="#" class="select_all"><?php esc_html_e( 'All', 'woocommerce' ); ?></a>
|
2017-11-07 07:13:47 +00:00
|
|
|
<button type="submit" class="submit button" value="<?php esc_attr_e( 'Show', 'woocommerce' ); ?>"><?php esc_html_e( 'Show', 'woocommerce' ); ?></button>
|
2017-11-07 07:12:40 +00:00
|
|
|
<input type="hidden" name="range" value="<?php echo ( ! empty( $_GET['range'] ) ) ? esc_attr( wp_unslash( $_GET['range'] ) ) : ''; ?>" />
|
|
|
|
<input type="hidden" name="start_date" value="<?php echo ( ! empty( $_GET['start_date'] ) ) ? esc_attr( wp_unslash( $_GET['start_date'] ) ) : ''; ?>" />
|
|
|
|
<input type="hidden" name="end_date" value="<?php echo ( ! empty( $_GET['end_date'] ) ) ? esc_attr( wp_unslash( $_GET['end_date'] ) ) : ''; ?>" />
|
|
|
|
<input type="hidden" name="page" value="<?php echo ( ! empty( $_GET['page'] ) ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : ''; ?>" />
|
|
|
|
<input type="hidden" name="tab" value="<?php echo ( ! empty( $_GET['tab'] ) ) ? esc_attr( wp_unslash( $_GET['tab'] ) ) : ''; ?>" />
|
|
|
|
<input type="hidden" name="report" value="<?php echo ( ! empty( $_GET['report'] ) ) ? esc_attr( wp_unslash( $_GET['report'] ) ) : ''; ?>" />
|
|
|
|
<?php // @codingStandardsIgnoreEnd ?>
|
2013-06-27 17:39:55 +00:00
|
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
jQuery(function(){
|
2016-10-12 10:16:30 +00:00
|
|
|
// Select all/None
|
2014-01-24 18:34:48 +00:00
|
|
|
jQuery( '.chart-widget' ).on( 'click', '.select_all', function() {
|
2015-11-03 12:28:01 +00:00
|
|
|
jQuery(this).closest( 'div' ).find( 'select option' ).attr( 'selected', 'selected' );
|
2015-01-09 16:43:35 +00:00
|
|
|
jQuery(this).closest( 'div' ).find('select').change();
|
2013-06-27 17:39:55 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2014-01-24 18:34:48 +00:00
|
|
|
jQuery( '.chart-widget').on( 'click', '.select_none', function() {
|
2015-11-03 12:28:01 +00:00
|
|
|
jQuery(this).closest( 'div' ).find( 'select option' ).removeAttr( 'selected' );
|
2015-01-09 16:43:35 +00:00
|
|
|
jQuery(this).closest( 'div' ).find('select').change();
|
2013-06-27 17:39:55 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</form>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:56:12 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Output an export link.
|
2013-07-18 11:56:12 +00:00
|
|
|
*/
|
|
|
|
public function get_export_button() {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2017-11-07 07:12:40 +00:00
|
|
|
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : '7day';
|
2013-07-18 11:56:12 +00:00
|
|
|
?>
|
|
|
|
<a
|
|
|
|
href="#"
|
2017-11-07 07:12:40 +00:00
|
|
|
download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo esc_attr( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ); ?>.csv"
|
2013-07-18 11:56:12 +00:00
|
|
|
class="export_csv"
|
|
|
|
data-export="chart"
|
2015-08-05 18:08:15 +00:00
|
|
|
data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>"
|
2017-11-07 07:12:40 +00:00
|
|
|
data-groupby="<?php echo esc_attr( $this->chart_groupby ); ?>"
|
2013-07-18 11:56:12 +00:00
|
|
|
>
|
2017-11-07 07:12:40 +00:00
|
|
|
<?php esc_html_e( 'Export CSV', 'woocommerce' ); ?>
|
2013-07-18 11:56:12 +00:00
|
|
|
</a>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Get the main chart.
|
2013-06-27 17:39:55 +00:00
|
|
|
*/
|
|
|
|
public function get_main_chart() {
|
|
|
|
global $wp_locale;
|
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( empty( $this->show_categories ) ) {
|
2013-06-27 17:39:55 +00:00
|
|
|
?>
|
|
|
|
<div class="chart-container">
|
2017-11-07 07:12:40 +00:00
|
|
|
<p class="chart-prompt"><?php esc_html_e( 'Choose a category to view stats', 'woocommerce' ); ?></p>
|
2013-06-27 17:39:55 +00:00
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
} else {
|
2015-02-03 14:12:04 +00:00
|
|
|
$chart_data = array();
|
|
|
|
$index = 0;
|
2013-06-27 17:39:55 +00:00
|
|
|
|
|
|
|
foreach ( $this->show_categories as $category ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2014-04-25 09:43:26 +00:00
|
|
|
$category = get_term( $category, 'product_cat' );
|
|
|
|
$product_ids = $this->get_products_in_category( $category->term_id );
|
2013-06-27 20:34:18 +00:00
|
|
|
$category_chart_data = array();
|
|
|
|
|
|
|
|
for ( $i = 0; $i <= $this->chart_interval; $i ++ ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
$interval_total = 0;
|
|
|
|
|
|
|
|
switch ( $this->chart_groupby ) {
|
|
|
|
case 'day' :
|
|
|
|
$time = strtotime( date( 'Ymd', strtotime( "+{$i} DAY", $this->start_date ) ) ) * 1000;
|
|
|
|
break;
|
|
|
|
case 'month' :
|
2015-01-20 09:53:33 +00:00
|
|
|
default :
|
2013-06-27 20:34:18 +00:00
|
|
|
$time = strtotime( date( 'Ym', strtotime( "+{$i} MONTH", $this->start_date ) ) . '01' ) * 1000;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
foreach ( $product_ids as $id ) {
|
2014-08-31 07:19:13 +00:00
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
if ( isset( $this->item_sales_and_times[ $time ][ $id ] ) ) {
|
|
|
|
$interval_total += $this->item_sales_and_times[ $time ][ $id ];
|
|
|
|
}
|
2013-06-27 17:39:55 +00:00
|
|
|
}
|
2013-06-27 20:34:18 +00:00
|
|
|
|
2015-04-14 09:51:20 +00:00
|
|
|
$category_chart_data[] = array( $time, (float) wc_format_decimal( $interval_total, wc_get_price_decimals() ) );
|
2013-06-27 17:39:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:34:18 +00:00
|
|
|
$chart_data[ $category->term_id ]['category'] = $category->name;
|
|
|
|
$chart_data[ $category->term_id ]['data'] = $category_chart_data;
|
2013-06-27 17:39:55 +00:00
|
|
|
|
2016-03-04 10:53:48 +00:00
|
|
|
$index++;
|
2013-06-27 17:39:55 +00:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="chart-container">
|
|
|
|
<div class="chart-placeholder main"></div>
|
|
|
|
</div>
|
2017-11-07 07:12:40 +00:00
|
|
|
<?php // @codingStandardsIgnoreStart ?>
|
2013-06-27 17:39:55 +00:00
|
|
|
<script type="text/javascript">
|
2013-07-18 11:56:12 +00:00
|
|
|
var main_chart;
|
|
|
|
|
2013-06-27 17:39:55 +00:00
|
|
|
jQuery(function(){
|
2013-07-18 08:20:49 +00:00
|
|
|
var drawGraph = function( highlight ) {
|
|
|
|
var series = [
|
2013-06-27 20:34:18 +00:00
|
|
|
<?php
|
|
|
|
$index = 0;
|
|
|
|
foreach ( $chart_data as $data ) {
|
2016-12-20 11:07:31 +00:00
|
|
|
$color = isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[0];
|
2013-06-27 20:34:18 +00:00
|
|
|
$width = $this->barwidth / sizeof( $chart_data );
|
|
|
|
$offset = ( $width * $index );
|
|
|
|
$series = $data['data'];
|
2015-04-14 09:51:20 +00:00
|
|
|
foreach ( $series as $key => $series_data ) {
|
2013-06-27 20:34:18 +00:00
|
|
|
$series[ $key ][0] = $series_data[0] + $offset;
|
2015-04-14 09:51:20 +00:00
|
|
|
}
|
2013-06-27 20:34:18 +00:00
|
|
|
echo '{
|
|
|
|
label: "' . esc_js( $data['category'] ) . '",
|
|
|
|
data: jQuery.parseJSON( "' . json_encode( $series ) . '" ),
|
|
|
|
color: "' . $color . '",
|
2014-10-24 10:58:25 +00:00
|
|
|
bars: {
|
|
|
|
fillColor: "' . $color . '",
|
|
|
|
fill: true,
|
|
|
|
show: true,
|
|
|
|
lineWidth: 1,
|
|
|
|
align: "center",
|
|
|
|
barWidth: ' . $width * 0.75 . ',
|
|
|
|
stack: false
|
|
|
|
},
|
|
|
|
' . $this->get_currency_tooltip() . ',
|
2013-06-27 20:34:18 +00:00
|
|
|
enable_tooltip: true,
|
|
|
|
prepend_label: true
|
|
|
|
},';
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
?>
|
2013-07-18 08:20:49 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if ( highlight !== 'undefined' && series[ highlight ] ) {
|
|
|
|
highlight_series = series[ highlight ];
|
|
|
|
|
|
|
|
highlight_series.color = '#9c5d90';
|
|
|
|
|
2014-08-31 07:19:13 +00:00
|
|
|
if ( highlight_series.bars ) {
|
2013-07-18 08:20:49 +00:00
|
|
|
highlight_series.bars.fillColor = '#9c5d90';
|
2014-08-31 07:19:13 +00:00
|
|
|
}
|
2013-07-18 08:20:49 +00:00
|
|
|
|
|
|
|
if ( highlight_series.lines ) {
|
|
|
|
highlight_series.lines.lineWidth = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:56:12 +00:00
|
|
|
main_chart = jQuery.plot(
|
2013-07-18 08:20:49 +00:00
|
|
|
jQuery('.chart-placeholder.main'),
|
|
|
|
series,
|
|
|
|
{
|
|
|
|
legend: {
|
|
|
|
show: false
|
|
|
|
},
|
2014-01-21 22:11:05 +00:00
|
|
|
grid: {
|
|
|
|
color: '#aaa',
|
|
|
|
borderColor: 'transparent',
|
|
|
|
borderWidth: 0,
|
|
|
|
hoverable: true
|
|
|
|
},
|
|
|
|
xaxes: [ {
|
|
|
|
color: '#aaa',
|
|
|
|
reserveSpace: true,
|
|
|
|
position: "bottom",
|
|
|
|
tickColor: 'transparent',
|
2013-07-18 08:20:49 +00:00
|
|
|
mode: "time",
|
2016-09-09 00:14:28 +00:00
|
|
|
timeformat: "<?php echo ( 'day' === $this->chart_groupby ) ? '%d %b' : '%b'; ?>",
|
2014-01-21 22:14:26 +00:00
|
|
|
monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ); ?>,
|
2013-07-18 08:20:49 +00:00
|
|
|
tickLength: 1,
|
|
|
|
minTickSize: [1, "<?php echo $this->chart_groupby; ?>"],
|
|
|
|
tickSize: [1, "<?php echo $this->chart_groupby; ?>"],
|
|
|
|
font: {
|
2014-01-21 22:11:05 +00:00
|
|
|
color: "#aaa"
|
|
|
|
}
|
2013-07-18 08:20:49 +00:00
|
|
|
} ],
|
2014-01-21 22:11:05 +00:00
|
|
|
yaxes: [
|
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
tickDecimals: 2,
|
|
|
|
color: 'transparent',
|
|
|
|
font: { color: "#aaa" }
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
jQuery('.chart-placeholder').resize();
|
|
|
|
|
|
|
|
}
|
2013-07-18 08:20:49 +00:00
|
|
|
|
|
|
|
drawGraph();
|
|
|
|
|
|
|
|
jQuery('.highlight_series').hover(
|
|
|
|
function() {
|
|
|
|
drawGraph( jQuery(this).data('series') );
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
drawGraph();
|
|
|
|
}
|
|
|
|
);
|
2013-06-27 17:39:55 +00:00
|
|
|
});
|
|
|
|
</script>
|
2017-11-07 07:12:40 +00:00
|
|
|
<?php // @codingStandardsIgnoreEnd ?>
|
2013-06-27 17:39:55 +00:00
|
|
|
<?php
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 22:10:40 +00:00
|
|
|
}
|