From 2039e6a8fca66142c201dd02898a93a5178b0679 Mon Sep 17 00:00:00 2001 From: Michael Geers Date: Sat, 5 Aug 2023 12:20:29 +0200 Subject: [PATCH] UI: Support negative prices and smart cost limit (#9233) * UI: negative smart cost limit * use en dash --- assets/js/components/GridSettingsModal.vue | 25 ++++++++++++++----- .../js/components/TargetChargePlan.story.vue | 24 +++++++++++++++--- assets/js/components/TariffChart.vue | 15 +++++++---- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/assets/js/components/GridSettingsModal.vue b/assets/js/components/GridSettingsModal.vue index 3303f9378..0b6bb3397 100644 --- a/assets/js/components/GridSettingsModal.vue +++ b/assets/js/components/GridSettingsModal.vue @@ -48,7 +48,7 @@ - + {{ $t("gridSettings.costLimitDescription") }} @@ -133,7 +133,10 @@ export default { const values = []; const stepSize = this.optionStepSize; for (let i = 1; i <= 100; i++) { - values.push(stepSize * i); + const value = this.optionStartValue + stepSize * i; + if (value != 0) { + values.push(value); + } } // add special entry if currently selected value is not in the scale const selected = this.selectedSmartCostLimit; @@ -150,13 +153,22 @@ export default { return { value, name }; }); }, + optionStartValue() { + if (!this.tariff) { + return 0; + } + const { min } = this.costRange(this.totalSlots); + const minValue = Math.min(0, min); + const stepSize = this.optionStepSize; + return Math.ceil(minValue / stepSize) * stepSize; + }, optionStepSize() { if (!this.tariff) { return 1; } - const { max } = this.costRange(this.totalSlots); + const { min, max } = this.costRange(this.totalSlots); for (const scale of [0.1, 1, 10, 50, 100, 200, 500, 1000, 2000, 5000, 10000]) { - if (max < scale) { + if (max - Math.min(0, min) < scale) { return scale / 100; } } @@ -181,7 +193,8 @@ export default { const day = this.weekdayShort(start); // TODO: handle multiple matching time slots const price = this.findSlotInRange(start, end, rates)?.price; - const charging = price < this.selectedSmartCostLimit; + const charging = + price < this.selectedSmartCostLimit && this.selectedSmartCostLimit !== 0; const selectable = price !== undefined; result.push({ day, price, startHour, endHour, charging, selectable }); } @@ -292,7 +305,7 @@ export default { const fmtMax = this.isCo2 ? this.fmtCo2Short(max) : this.fmtPricePerKWh(max, this.currency, true); - return `${fmtMin} - ${fmtMax}`; + return `${fmtMin} – ${fmtMax}`; }, slotHovered(index) { this.activeIndex = index; diff --git a/assets/js/components/TargetChargePlan.story.vue b/assets/js/components/TargetChargePlan.story.vue index 7697e46e4..a7fe05294 100644 --- a/assets/js/components/TargetChargePlan.story.vue +++ b/assets/js/components/TargetChargePlan.story.vue @@ -31,7 +31,7 @@ const co2 = { ].map((price, i) => createRate(price, i)), duration: 8695, plan: [createRate(213, 4), createRate(336, 11), createRate(336, 12)], - unit: "gCO2eq", + smartCostType: "co2", targetTime: createDate(14), }; @@ -39,7 +39,8 @@ const fixed = { rates: [createRate(0.442, 0, 50)], duration: 8695, plan: [createRate(0.442, 12, 3)], - unit: "EUR", + smartCostType: "price", + currency: "EUR", targetTime: createDate(14), }; @@ -53,7 +54,8 @@ const zoned = { ], duration: 8695, plan: [createRate(2.39, 13, 3)], - unit: "DKK", + smartCostType: "price", + currency: "DKK", targetTime: createDate(17), }; @@ -61,9 +63,20 @@ const unknown = { rates: co2.rates.slice(0, 16), duration: 8695, plan: [createRate(213, 4), createRate(336, 11), createRate(336, 12)], - unit: "gCO2eq", + smartCostType: "co2", targetTime: createDate(14), }; + +const dynamic = { + rates: [ + 0.12, 0.15, 0, -0.05, -0.11, -0.24, -0.08, 0.12, 0.25, 0.29, 0.22, 0.31, 0.31, 0.33, + ].map((price, i) => createRate(price, i)), + duration: 8695, + plan: [createRate(0.23, 2, 5)], + smartCostType: "price", + currency: "EUR", + targetTime: createDate(13), +}; diff --git a/assets/js/components/TariffChart.vue b/assets/js/components/TariffChart.vue index 13633981a..2c09dc5d0 100644 --- a/assets/js/components/TariffChart.vue +++ b/assets/js/components/TariffChart.vue @@ -44,15 +44,17 @@ export default { return { activeIndex: null, startTime: new Date() }; }, computed: { - maxPrice() { - let result = 0; + priceInfo() { + let max = Number.MIN_VALUE; + let min = 0; this.slots .map((s) => s.price) .filter((price) => price !== undefined) .forEach((price) => { - result = Math.max(result, price); + max = Math.max(max, price); + min = Math.min(min, price); }); - return result; + return { min, range: max - min }; }, avgPrice() { let sum = 0; @@ -89,7 +91,10 @@ export default { }, priceStyle(price) { const value = price === undefined ? this.avgPrice : price; - const height = value !== undefined ? `${5 + (95 / this.maxPrice) * value}%` : "100%"; + const height = + value !== undefined + ? `${10 + (90 / this.priceInfo.range) * (value - this.priceInfo.min)}%` + : "100%"; return { height }; }, },