From 5fe3903979743dd2de0ed14d4cbcc7bf3a9e8b9f Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sun, 16 Jul 2023 21:21:33 +0200 Subject: [PATCH] refactor: split the TemperaturePanel.vue and remove the big getters Signed-off-by: Stefan Dej --- src/components/charts/TempChart.vue | 245 ++++++-------- .../Temperature/TemperaturePanelListItem.vue | 247 +++++++++----- ...mperaturePanelListItemAdditionalSensor.vue | 34 ++ ...turePanelListItemAdditionalSensorValue.vue | 70 ++++ .../TemperaturePanelListItemEdit.vue | 93 ++++++ ...aturePanelListItemEditAdditionalSensor.vue | 40 +++ ...TemperaturePanelListItemEditChartSerie.vue | 42 +++ .../Temperature/TemperaturePanelSettings.vue | 2 - src/components/panels/TemperaturePanel.vue | 157 +-------- src/store/gui/actions.ts | 37 ++ src/store/gui/getters.ts | 7 +- src/store/gui/mutations.ts | 39 +++ src/store/printer/getters.ts | 316 +----------------- src/store/printer/tempHistory/actions.ts | 252 +++++++------- src/store/printer/tempHistory/getters.ts | 66 ++-- 15 files changed, 801 insertions(+), 846 deletions(-) create mode 100644 src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensor.vue create mode 100644 src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensorValue.vue create mode 100644 src/components/panels/Temperature/TemperaturePanelListItemEdit.vue create mode 100644 src/components/panels/Temperature/TemperaturePanelListItemEditAdditionalSensor.vue create mode 100644 src/components/panels/Temperature/TemperaturePanelListItemEditChartSerie.vue diff --git a/src/components/charts/TempChart.vue b/src/components/charts/TempChart.vue index bb4c994a5..ea69062c9 100644 --- a/src/components/charts/TempChart.vue +++ b/src/components/charts/TempChart.vue @@ -14,11 +14,12 @@ import { convertName } from '@/plugins/helpers' import Component from 'vue-class-component' import { Mixins, Watch } from 'vue-property-decorator' import BaseMixin from '../mixins/base' -import { PrinterTempHistoryStateSerie, PrinterTempHistoryStateSourceEntry } from '@/store/printer/tempHistory/types' +import { PrinterTempHistoryStateSourceEntry } from '@/store/printer/tempHistory/types' import type { ECharts } from 'echarts/core' import type { ECBasicOption } from 'echarts/types/dist/shared.d' import { mdiClock } from '@mdi/js' +import { datasetTypesInPercents } from '@/store/variables' interface echartsTooltipObj { [key: string]: any @@ -33,11 +34,51 @@ export default class TempChart extends Mixins(BaseMixin) { } private isVisible = true - public chartOptions: ECBasicOption = { - darkMode: true, - renderer: 'svg', - animation: false, - tooltip: { + get chartOptions(): ECBasicOption { + return { + darkMode: true, + renderer: 'svg', + animation: false, + tooltip: this.tooltip, + grid: { + top: 35, + right: this.gridRight, + bottom: 30, + left: 25, + }, + legend: { + animation: false, + show: false, + selected: this.selectedLegends, + }, + /*dataZoom: [{ + type: 'inside', + }],*/ + xAxis: { + type: 'time', + splitNumber: 5, + minInterval: 60 * 1000, + splitLine: { + show: true, + lineStyle: { + color: 'rgba(255, 255, 255, 0.06)', + }, + }, + axisLabel: { + color: 'rgba(255, 255, 255, 0.24)', + margin: 10, + formatter: this.timeFormat, + }, + }, + yAxis: this.yAxis, + media: this.media, + dataset: { source: this.source }, + series: this.series, + } + } + + get tooltip() { + return { animation: false, trigger: 'axis', backgroundColor: 'rgba(0,0,0,0.9)', @@ -57,38 +98,11 @@ export default class TempChart extends Mixins(BaseMixin) { obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 5 return obj }, - }, - grid: { - top: 35, - right: 15, - bottom: 30, - left: 25, - }, - legend: { - animation: false, - show: false, - selected: {}, - }, - /*dataZoom: [{ - type: 'inside', - }],*/ - xAxis: { - type: 'time', - splitNumber: 5, - minInterval: 60 * 1000, - splitLine: { - show: true, - lineStyle: { - color: 'rgba(255, 255, 255, 0.06)', - }, - }, - axisLabel: { - color: 'rgba(255, 255, 255, 0.24)', - margin: 10, - formatter: this.timeFormat, - }, - }, - yAxis: [ + } + } + + get yAxis() { + return [ { name: this.$t('Panels.TemperaturePanel.TemperaturesInChart'), type: 'value', @@ -158,15 +172,18 @@ export default class TempChart extends Mixins(BaseMixin) { }, }, }, - ], - media: [ + ] + } + + get media() { + return [ { query: { minWidth: 500, }, option: { grid: { - right: 15, + right: this.optionGridRight, left: 40, }, yAxis: [ @@ -188,11 +205,7 @@ export default class TempChart extends Mixins(BaseMixin) { ], }, }, - ], - dataset: { - source: [], - }, - series: [], + ] } get chart(): ECharts | null { @@ -241,85 +254,34 @@ export default class TempChart extends Mixins(BaseMixin) { } } - mounted() { - this.initChart() - this.chartOptions.xAxis.axisLabel.formatter = this.timeFormat - } - - beforeDestroy() { - if (typeof window === 'undefined') return - if (this.chart) this.chart.dispose() - } + get gridRight() { + if (this.boolDisplayPwmAxis) return 25 - initChart() { - this.chartOptions.series = this.series - this.chartOptions.legend.selected = this.selectedLegends - this.updateChartPwmAxis() + return 15 } - updateChart() { - if (this.chart && this.isVisible) { - //const t0 = performance.now() - const limitDate = new Date(Date.now() - this.maxHistory * 1000) - let newSource = [...this.source].filter((entry: PrinterTempHistoryStateSourceEntry) => { - return entry.date >= limitDate - }) + get optionGridRight() { + if (this.boolDisplayPwmAxis) return 35 - this.chart?.setOption( - { - dataset: { - source: newSource, - }, - }, - false, - true - ) - - //const t1 = performance.now() - //window.console.debug('calc chart', (t1-t0).toFixed(), newSource.length, this.source.length) - - // reset tempHistory if working sources are smaller than 80% - if (this.source.length > 0 && newSource.length < this.maxHistory * 0.8) { - this.$socket.emit('server.temperature_store', {}, { action: 'printer/tempHistory/init' }) - } - } + return 15 } - updateChartPwmAxis() { - if (this.boolDisplayPwmAxis) { - this.chartOptions.yAxis[1].show = true - this.chartOptions.grid.right = 25 - this.chartOptions.media[0].option.grid.right = 35 - } else { - this.chartOptions.yAxis[1].show = false - this.chartOptions.grid.right = 15 - this.chartOptions.media[0].option.grid.right = 15 - } + beforeDestroy() { + if (typeof window === 'undefined') return + if (this.chart) this.chart.dispose() } visibilityChanged(isVisible: boolean) { this.isVisible = isVisible - - if (isVisible) { - this.initChart() - } } tooltipFormatter(datasets: any) { let output = '' - const mainDatasets = datasets.filter((dataset: any) => { - if (dataset.seriesName === 'date') return false - - const lastIndex = dataset.seriesName.lastIndexOf('-') - if (lastIndex === -1) return true - - const suffix = dataset.seriesName.slice(lastIndex + 1) - return !['target', 'power', 'speed'].includes(suffix) - }) + const mainDatasets = datasets.filter((dataset: any) => dataset.seriesName.endsWith('-temperature')) if (datasets.length) { let outputTime = datasets[0]['axisValueLabel'] - outputTime = outputTime.substr(outputTime.indexOf(' ')) + outputTime = outputTime.substring(outputTime.indexOf(' ')) output += '
' + @@ -338,24 +300,35 @@ export default class TempChart extends Mixins(BaseMixin) { } mainDatasets.forEach((dataset: any) => { + const baseSeriesName = dataset.seriesName.substring(0, dataset.seriesName.lastIndexOf('-')) + let displayName = baseSeriesName + if (displayName.indexOf(' ') !== -1) { + displayName = displayName.substring(displayName.indexOf(' ') + 1) + } + output += '
' output += '
' output += dataset.marker - output += "" + convertName(dataset.seriesName) + ':' + output += "" + convertName(displayName) + ':' output += '
' output += '
' - if (dataset.seriesName in dataset.value) output += dataset.value[dataset.seriesName].toFixed(1) - if (dataset.seriesName + '-target' in dataset.value) - output += ' / ' + dataset.value[dataset.seriesName + '-target'].toFixed(1) + const seriesNameTemperature = `${baseSeriesName}-temperature` + const seriesNameTarget = `${baseSeriesName}-target` + + if (seriesNameTemperature in dataset.value) output += dataset.value[seriesNameTemperature].toFixed(1) + if (seriesNameTarget in dataset.value) output += ' / ' + dataset.value[seriesNameTarget].toFixed(1) output += '°C' - if (dataset.seriesName + '-power' in dataset.value) - output += ' [ ' + (dataset.value[dataset.seriesName + '-power'] * 100).toFixed(0) + '% ]' - if (dataset.seriesName + '-speed' in dataset.value) - output += ' [ ' + (dataset.value[dataset.seriesName + '-speed'] * 100).toFixed(0) + '% ]' + datasetTypesInPercents.forEach((attrKey) => { + const seriesName = `${baseSeriesName}-${attrKey}` + if (!(seriesName in dataset.value)) return + + const value = (dataset.value[seriesName] * 100).toFixed(0) + output += ` [ ${value}% ]` + }) output += '
' output += '
' @@ -364,26 +337,6 @@ export default class TempChart extends Mixins(BaseMixin) { return output } - @Watch('series', { deep: true }) - seriesChanged(newVal: PrinterTempHistoryStateSerie[]) { - if (this.chart && this.chart?.isDisposed() !== true) { - this.chart.setOption( - { - series: newVal, - }, - false, - true - ) - } - } - - @Watch('source') - sourceChanged() { - if (this.chart?.isDisposed() !== true) { - this.updateChart() - } - } - @Watch('selectedLegends') selectedLegendsChanged(newVal: any, oldVal: any) { if (this.chart?.isDisposed() !== true) { @@ -396,14 +349,20 @@ export default class TempChart extends Mixins(BaseMixin) { } } - @Watch('boolDisplayPwmAxis') - boolDisplayPwmAxisChanged() { - this.updateChartPwmAxis() - } + @Watch('source') + sourceChanged(newVal: PrinterTempHistoryStateSourceEntry[]) { + // break if chart isn't initialized or not visible + if (!this.chart || !this.isVisible) return + + const limitDate = new Date(Date.now() - this.maxHistory * 1000) + let newSource = newVal.filter((entry: PrinterTempHistoryStateSourceEntry) => { + return entry.date >= limitDate + }) - @Watch('hours12Format') - hours12FormatChanged() { - this.chartOptions.xAxis.axisLabel.formatter = this.timeFormat + // reset tempHistory if working sources are smaller than 80% + if (newVal.length > 0 && newSource.length < this.maxHistory * 0.8) { + this.$socket.emit('server.temperature_store', {}, { action: 'printer/tempHistory/init' }) + } } } diff --git a/src/components/panels/Temperature/TemperaturePanelListItem.vue b/src/components/panels/Temperature/TemperaturePanelListItem.vue index 70d756bd2..ca7c16542 100644 --- a/src/components/panels/Temperature/TemperaturePanelListItem.vue +++ b/src/components/panels/Temperature/TemperaturePanelListItem.vue @@ -1,15 +1,17 @@ diff --git a/src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensor.vue b/src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensor.vue new file mode 100644 index 000000000..40f1afd7e --- /dev/null +++ b/src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensor.vue @@ -0,0 +1,34 @@ + + + diff --git a/src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensorValue.vue b/src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensorValue.vue new file mode 100644 index 000000000..aadc033b1 --- /dev/null +++ b/src/components/panels/Temperature/TemperaturePanelListItemAdditionalSensorValue.vue @@ -0,0 +1,70 @@ + + + diff --git a/src/components/panels/Temperature/TemperaturePanelListItemEdit.vue b/src/components/panels/Temperature/TemperaturePanelListItemEdit.vue new file mode 100644 index 000000000..ed96dfe05 --- /dev/null +++ b/src/components/panels/Temperature/TemperaturePanelListItemEdit.vue @@ -0,0 +1,93 @@ + + + diff --git a/src/components/panels/Temperature/TemperaturePanelListItemEditAdditionalSensor.vue b/src/components/panels/Temperature/TemperaturePanelListItemEditAdditionalSensor.vue new file mode 100644 index 000000000..1c9d110bd --- /dev/null +++ b/src/components/panels/Temperature/TemperaturePanelListItemEditAdditionalSensor.vue @@ -0,0 +1,40 @@ + + + diff --git a/src/components/panels/Temperature/TemperaturePanelListItemEditChartSerie.vue b/src/components/panels/Temperature/TemperaturePanelListItemEditChartSerie.vue new file mode 100644 index 000000000..ddfbea12f --- /dev/null +++ b/src/components/panels/Temperature/TemperaturePanelListItemEditChartSerie.vue @@ -0,0 +1,42 @@ + + + diff --git a/src/components/panels/Temperature/TemperaturePanelSettings.vue b/src/components/panels/Temperature/TemperaturePanelSettings.vue index df1a73be7..ad9cfc8a8 100644 --- a/src/components/panels/Temperature/TemperaturePanelSettings.vue +++ b/src/components/panels/Temperature/TemperaturePanelSettings.vue @@ -51,5 +51,3 @@ export default class TemperaturePanelSettings extends Mixins(BaseMixin) { } } - - diff --git a/src/components/panels/TemperaturePanel.vue b/src/components/panels/TemperaturePanel.vue index ae6b247b5..4acf4e9e8 100644 --- a/src/components/panels/TemperaturePanel.vue +++ b/src/components/panels/TemperaturePanel.vue @@ -1,87 +1,28 @@