diff --git a/components/Charts/TimeOfDay/index.vue b/components/Charts/TimeOfDay/index.vue index 80691a2d..e496b53b 100644 --- a/components/Charts/TimeOfDay/index.vue +++ b/components/Charts/TimeOfDay/index.vue @@ -91,7 +91,7 @@ import { CHART_CURVE_SMOOTH, CHART_CURVE_STEP } from '@/constants/chart-options. import DateDisplay from '@/services/DateDisplay.js' import DayLines from './DayLines.vue' import DaySparkLines from './DaySparkLines.vue' -import { getDataBucket, getTimeLabel } from '@/data/transform/time-of-day.js' +import { getDataBucket, getTimeLabel, getDay } from '@/data/transform/time-of-day.js' import AverageStackedArea from './AverageStackedArea.vue' export default { @@ -136,7 +136,6 @@ export default { filterPeriod: 'filterPeriod', hiddenFuelTechs: 'hiddenFuelTechs', - todayKey: 'timeOfDay/todayKey', selectedToDs: 'timeOfDay/selectedToDs', chartPowerCurrentUnit: 'chartOptionsPowerEnergy/chartPowerCurrentUnit', @@ -164,6 +163,11 @@ export default { return 1 }, + todayKey() { + const lastDate = this.currentDataset[this.currentDataset.length - 1].date + return getDay(lastDate) + }, + allDomains() { const price = [] if (!this.currentDomainPowerEnergy) return [] @@ -226,7 +230,7 @@ export default { id: key, label: getLabel(key) } - }) + }) return datasetKeys }, diff --git a/data/transform/time-of-day.js b/data/transform/time-of-day.js index fdf76815..cce3efb9 100644 --- a/data/transform/time-of-day.js +++ b/data/transform/time-of-day.js @@ -6,10 +6,9 @@ export function getDay(d) { return utcFormat(`%e %b`)(d) } -function getDayKeys(range) { +export function getDayKeys(range, jsDate) { const keys = [] - let utcCurrent = new Date() - utcCurrent.setUTCDate(utcCurrent.getDate()); + let utcCurrent = new Date(jsDate.getTime()) utcCurrent.setUTCHours(0, 0, 0, 0) for (let i = 0; i < range; i++) { @@ -32,9 +31,8 @@ export function getTimeLabel(d) { return `${hour}${min}${ampm}` } -function getTimebucket(interval) { - let utcCurrent = new Date() - utcCurrent.setUTCDate(utcCurrent.getDate()); +function getTimebucket(interval, jsDate) { + let utcCurrent = new Date(jsDate.getTime()) utcCurrent.setUTCHours(0, 0, 0, 0) const b = [] @@ -80,16 +78,17 @@ export function getDataBucket({ data, domain, demandDomain, isPrice, category, p } }) - // TODO: maybe create dayKeys using dataset start/last time instead - const dayKeys = getDayKeys(range) - const timeBucket = getTimebucket(interval) + const lastPoint = dataset[dataset.length - 1] + const dayKeys = getDayKeys(range, lastPoint.date) + const timeBucket = getTimebucket(interval, lastPoint.date) dataset.forEach(d => { const date = d.date const day = getDay(date) + const findDay = dayKeys.find(k => k === day) const x = getTimeLabel(date) const find = timeBucket.find(b => b.x === x) - if (find) { + if (find && findDay) { find[day] = d.value } }) @@ -105,13 +104,14 @@ export function getDataBucket({ data, domain, demandDomain, isPrice, category, p b._average = total / keyCount }) - // console.log('domain', domain, data) // console.log('dataCountWithValues', dataCountWithValues) // console.log('dataValueSum', dataValueSum) // console.log('average', dataValueSum / dataCountWithValues) // console.log('========') + // console.log('average', getAverage({ data, domain, isPrice, demandDomain, category })) + return { data: timeBucket, average: getAverage({ data, domain, isPrice, demandDomain, category }) diff --git a/store/timeOfDay.js b/store/timeOfDay.js index cbbc55fa..a9c3b8d5 100644 --- a/store/timeOfDay.js +++ b/store/timeOfDay.js @@ -1,17 +1,8 @@ -import { getDay } from '@/data/transform/time-of-day.js' - export const state = () => ({ - todayKey: function() { - const utcCurrent = new Date() - utcCurrent.setUTCDate(utcCurrent.getDate()); - utcCurrent.setUTCHours(0, 0, 0, 0) - return getDay(utcCurrent) - }(), selectedToDs: [] }) export const getters = { - todayKey: (state) => state.todayKey, selectedToDs: (state) => state.selectedToDs }