Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Fix main graph intervals (#4024)

RobertJoonasf49bdef7dda7
diff --git a/assets/js/dashboard/stats/graph/interval-picker.js b/assets/js/dashboard/stats/graph/interval-picker.js
index d01f3610f6..96feb70e56 100644
--- a/assets/js/dashboard/stats/graph/interval-picker.js
+++ b/assets/js/dashboard/stats/graph/interval-picker.js
@@ -4,7 +4,6 @@ import React, { Fragment, useCallback, useEffect } from 'react';
import classNames from 'classnames'
import * as storage from '../../util/storage'
import { isKeyPressed } from '../../keybinding.js'
-import { monthsBetweenDates } from '../../util/date.js'
const INTERVAL_LABELS = {
'minute': 'Minutes',
@@ -15,13 +14,47 @@ const INTERVAL_LABELS = {
}
function validIntervals(site, query) {
- if (query.period === "custom" && monthsBetweenDates(query.from, query.to) > 12) {
- return ["week", "month"]
+ if (query.period === 'custom') {
+ if (query.to.diff(query.from, 'days') < 7) {
+ return ['date']
+ } else if (query.to.diff(query.from, 'months') < 1) {
+ return ['date', 'week']
+ } else if (query.to.diff(query.from, 'months') < 12) {
+ return ['date', 'week', 'month']
+ } else {
+ return ['week', 'month']
+ }
} else {
return site.validIntervalsByPeriod[query.period]
}
}
+function getDefaultInterval(query, validIntervals) {
+ const defaultByPeriod = {
+ 'day': 'hour',
+ '7d': 'date',
+ '6mo': 'month',
+ '12mo': 'month',
+ 'year': 'month'
+ }
+
+ if (query.period === 'custom') {
+ return defaultForCustomPeriod(query.from, query.to)
+ } else {
+ return defaultByPeriod[query.period] || validIntervals[0]
+ }
+}
+
+function defaultForCustomPeriod(from, to) {
+ if (to.diff(from, 'days') < 30) {
+ return 'date'
+ } else if (to.diff(from, 'months') < 6) {
+ return 'week'
+ } else {
+ return 'month'
+ }
+}
+
function getStoredInterval(period, domain) {
return storage.getItem(`interval__${period}__${domain}`)
}
@@ -45,7 +78,7 @@ export const getCurrentInterval = function(site, query) {
const options = validIntervals(site, query)
const storedInterval = getStoredInterval(query.period, site.domain)
- const defaultInterval = [...options].pop()
+ const defaultInterval = getDefaultInterval(query, options)
if (storedInterval && options.includes(storedInterval)) {
return storedInterval
diff --git a/assets/js/dashboard/util/date.js b/assets/js/dashboard/util/date.js
index 405198501d..eddec1a380 100644
--- a/assets/js/dashboard/util/date.js
+++ b/assets/js/dashboard/util/date.js
@@ -123,9 +123,4 @@ export function isAfter(date1, date2, period) {
return false;
}
return date1.date() > date2.date()
-}
-
-export function monthsBetweenDates(date1, date2) {
- var diffMonths = (date2.year() - date1.year()) * 12 + date2.month() - date1.month();
- return Math.abs(diffMonths);
-}
+}
\ No newline at end of file