diff --git a/src/components/desktop/TrendsChart.vue b/src/components/desktop/TrendsChart.vue index 6528a6fe..e8c603d7 100644 --- a/src/components/desktop/TrendsChart.vue +++ b/src/components/desktop/TrendsChart.vue @@ -20,13 +20,11 @@ import { import { getYearMonthFirstUnixTime, getYearMonthLastUnixTime, - getAllYearsStartAndEndUnixTimes, - getAllQuartersStartAndEndUnixTimes, - getAllMonthsStartAndEndUnixTimes, getDateTypeByDateRange } from '@/lib/datetime.js'; import { - sortStatisticsItems + sortStatisticsItems, + getAllDateRanges } from '@/lib/statistics.js'; export default { @@ -87,44 +85,7 @@ export default { return map; }, allDateRanges: function () { - let startYearMonth = this.startYearMonth; - let endYearMonth = this.endYearMonth; - - if ((!this.startYearMonth || !this.endYearMonth) && this.items && this.items.length) { - let minYear = Number.MAX_SAFE_INTEGER, minMonth = Number.MAX_SAFE_INTEGER, maxYear = 0, maxMonth = 0; - - for (let i = 0; i < this.items.length; i++) { - const item = this.items[i]; - - for (let j = 0; j < item.items.length; j++) { - const dataItem = item.items[j]; - - if (dataItem.year < minYear || (dataItem.year === minYear && dataItem.month < minMonth)) { - minYear = dataItem.year; - minMonth = dataItem.month; - } - - if (dataItem.year > maxYear || (dataItem.year === maxYear && dataItem.month > maxMonth)) { - maxYear = dataItem.year; - maxMonth = dataItem.month; - } - } - } - - startYearMonth = `${minYear}-${minMonth}`; - endYearMonth = `${maxYear}-${maxMonth}`; - } - - if (!startYearMonth || !endYearMonth) { - return []; - } - if (this.dateAggregationType === statisticsConstants.allDateAggregationTypes.Year.type) { - return getAllYearsStartAndEndUnixTimes(startYearMonth, endYearMonth); - } else if (this.dateAggregationType === statisticsConstants.allDateAggregationTypes.Quarter.type) { - return getAllQuartersStartAndEndUnixTimes(startYearMonth, endYearMonth); - } else { // if (this.dateAggregationType === statisticsConstants.allDateAggregationTypes.Month.type) { - return getAllMonthsStartAndEndUnixTimes(startYearMonth, endYearMonth); - } + return getAllDateRanges(this.items, this.startYearMonth, this.endYearMonth, this.dateAggregationType); }, allDisplayDateRanges: function () { const allDisplayDateRanges = []; @@ -301,14 +262,14 @@ export default { displayItems.push({ name: name, color: color, - displayOrders:displayOrders, + displayOrders: displayOrders, totalAmount: amount }); totalAmount += amount; } - sortStatisticsItems(displayItems, self.sortingType) + sortStatisticsItems(displayItems, self.sortingType); for (let i = 0; i < displayItems.length; i++) { const item = displayItems[i]; diff --git a/src/lib/statistics.js b/src/lib/statistics.js index 8d372fcc..15c55c41 100644 --- a/src/lib/statistics.js +++ b/src/lib/statistics.js @@ -1,5 +1,11 @@ import statisticsConstants from '@/consts/statistics.js'; +import { + getAllMonthsStartAndEndUnixTimes, + getAllQuartersStartAndEndUnixTimes, + getAllYearsStartAndEndUnixTimes +} from '@/lib/datetime.js'; + export function isChartDataTypeAvailableForAnalysisType(chartDataType, analysisType) { for (const dataTypeField in statisticsConstants.allChartDataTypes) { if (!Object.prototype.hasOwnProperty.call(statisticsConstants.allChartDataTypes, dataTypeField)) { @@ -52,3 +58,41 @@ export function sortStatisticsItems(items, sortingType) { }); } } + +export function getAllDateRanges(items, startYearMonth, endYearMonth, dateAggregationType) { + if ((!startYearMonth || !endYearMonth) && items && items.length) { + let minYear = Number.MAX_SAFE_INTEGER, minMonth = Number.MAX_SAFE_INTEGER, maxYear = 0, maxMonth = 0; + + for (let i = 0; i < items.length; i++) { + const item = items[i]; + + for (let j = 0; j < item.items.length; j++) { + const dataItem = item.items[j]; + + if (dataItem.year < minYear || (dataItem.year === minYear && dataItem.month < minMonth)) { + minYear = dataItem.year; + minMonth = dataItem.month; + } + + if (dataItem.year > maxYear || (dataItem.year === maxYear && dataItem.month > maxMonth)) { + maxYear = dataItem.year; + maxMonth = dataItem.month; + } + } + } + + startYearMonth = `${minYear}-${minMonth}`; + endYearMonth = `${maxYear}-${maxMonth}`; + } + + if (!startYearMonth || !endYearMonth) { + return []; + } + if (dateAggregationType === statisticsConstants.allDateAggregationTypes.Year.type) { + return getAllYearsStartAndEndUnixTimes(startYearMonth, endYearMonth); + } else if (dateAggregationType === statisticsConstants.allDateAggregationTypes.Quarter.type) { + return getAllQuartersStartAndEndUnixTimes(startYearMonth, endYearMonth); + } else { // if (dateAggregationType === statisticsConstants.allDateAggregationTypes.Month.type) { + return getAllMonthsStartAndEndUnixTimes(startYearMonth, endYearMonth); + } +} diff --git a/src/views/desktop/statistics/TransactionPage.vue b/src/views/desktop/statistics/TransactionPage.vue index 9d2a23ef..81d8bb4f 100644 --- a/src/views/desktop/statistics/TransactionPage.vue +++ b/src/views/desktop/statistics/TransactionPage.vue @@ -378,7 +378,7 @@ export default { alwaysShowNav: mdAndUp.value, showNav: mdAndUp.value, analysisType: statisticsConstants.allAnalysisTypes.CategoricalAnalysis, - trendDateAggregationType: statisticsConstants.allDateAggregationTypes.Month.type, + trendDateAggregationType: statisticsConstants.defaultDateAggregationType, showCustomDateRangeDialog: false, showCustomMonthRangeDialog: false, showFilterAccountDialog: false,