From 3d6d662a64545172a8b033724d630970e4ce4aa2 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Tue, 31 Oct 2023 10:01:49 +0100 Subject: [PATCH] group chart by month only if viewing more than 1y of data & use that logic for preloading data too --- assets/src/js/components/chart.js | 8 ++++---- src/class-dashboard.php | 14 +++++++------- src/views/dashboard-page.php | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/assets/src/js/components/chart.js b/assets/src/js/components/chart.js index 50ce1053..974046d1 100644 --- a/assets/src/js/components/chart.js +++ b/assets/src/js/components/chart.js @@ -1,6 +1,6 @@ import { request } from '../util/api.js' -import { magnitude, formatLargeNumber } from '../util/numbers' -import { isLastDayOfMonth, toISO8601, format } from '../util/dates.js' +import { magnitude, formatLargeNumber } from '../util/numbers.js' +import { toISO8601, format } from '../util/dates.js' import { eventListenersModule, attributesModule, init, h } from "snabbdom" const {i18n} = window.koko_analytics; const patch = init([eventListenersModule, attributesModule]) @@ -60,7 +60,7 @@ function hideTooltip() { */ export default function(root, data, height) { if (!height) { - height = Math.max(240, Math.min(window.innerHeight / 3, window.innerWidth / 2, 360)); + height = Math.max(240, Math.min(window.innerHeight / 3, 360)); } root.parentElement.style.minHeight = `${height+4}px` let dateFormatOptions = {month: 'short', year: 'numeric'} @@ -102,7 +102,7 @@ export default function(root, data, height) { * @param {Date} endDate */ function update(startDate, endDate) { - const groupByMonth = (startDate.getDate() === 1 && isLastDayOfMonth(endDate) && (endDate - startDate) > 86400000 * 92) || (endDate - startDate) > 86400000 * 365 + const groupByMonth = (endDate - startDate) >= 86400000 * 364 dateFormatOptions = groupByMonth ? {month: 'short', year: 'numeric'} : undefined request('/stats', { diff --git a/src/class-dashboard.php b/src/class-dashboard.php index f90c600f..d622f6c0 100644 --- a/src/class-dashboard.php +++ b/src/class-dashboard.php @@ -45,15 +45,15 @@ public function show(): void require __DIR__ . '/views/dashboard-page.php'; } - private function get_script_data(): array + private function get_script_data(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd): array { - // TODO: Determine group parameter for pre-loading chart data - - $settings = get_settings(); $stats = new Stats(); - $dates = new Dates(); - [$dateStart, $dateEnd] = $dates->get_range($settings['default_view']); $items_per_page = (int) apply_filters('koko_analytics_items_per_page', 20); + $groupChartBy = 'day'; + + if ($dateEnd->getTimestamp() - $dateStart->getTimestamp() >= 86400 * 364) { + $groupChartBy = 'month'; + } return apply_filters('koko_analytics_dashboard_script_data', array( 'root' => rest_url(), @@ -66,7 +66,7 @@ private function get_script_data(): array 'Pageviews' => __('Pageviews', 'koko-analytics'), ), 'data' => array( - 'chart' => $stats->get_stats($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), 'day'), + 'chart' => $stats->get_stats($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), $groupChartBy), 'posts' => $stats->get_posts($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), 0, $items_per_page), 'referrers' => $stats->get_referrers($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), 0, $items_per_page), ) diff --git a/src/views/dashboard-page.php b/src/views/dashboard-page.php index a1068d42..c7659488 100644 --- a/src/views/dashboard-page.php +++ b/src/views/dashboard-page.php @@ -141,5 +141,5 @@