Skip to content

Commit

Permalink
group chart by month only if viewing more than 1y of data & use that …
Browse files Browse the repository at this point in the history
…logic for preloading data too
  • Loading branch information
dannyvankooten committed Oct 31, 2023
1 parent a541554 commit 3d6d662
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions assets/src/js/components/chart.js
Original file line number Diff line number Diff line change
@@ -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])
Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -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', {
Expand Down
14 changes: 7 additions & 7 deletions src/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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),
)
Expand Down
2 changes: 1 addition & 1 deletion src/views/dashboard-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,5 @@
</div>

<script>
var koko_analytics = <?php echo json_encode($this->get_script_data()); ?>;
var koko_analytics = <?php echo json_encode($this->get_script_data($dateStart, $dateEnd)); ?>;
</script>

0 comments on commit 3d6d662

Please sign in to comment.