diff --git a/client/src/pages/dashboard/components/BoxDialContent.tsx b/client/src/pages/dashboard/components/BoxDialContent.tsx index 4393782..1be0c0b 100644 --- a/client/src/pages/dashboard/components/BoxDialContent.tsx +++ b/client/src/pages/dashboard/components/BoxDialContent.tsx @@ -53,7 +53,7 @@ export const BoxDialContent = ({ box, data, refreshRef }: Props) => { {value.data && ( <> {displayValue} - {` ${data.unit}`} + {data.unit && ` ${data.unit}`} > )} diff --git a/client/src/pages/dashboard/components/BoxGraphContent.tsx b/client/src/pages/dashboard/components/BoxGraphContent.tsx index b856143..1fe8432 100644 --- a/client/src/pages/dashboard/components/BoxGraphContent.tsx +++ b/client/src/pages/dashboard/components/BoxGraphContent.tsx @@ -1,4 +1,6 @@ import { getSensorValues } from '@/api/sensorValues' +import { max } from '@/utils/max' +import { min } from '@/utils/min' import { DashboardGraphData } from '@/utils/parseDashboard' import { RefObject } from 'preact' import { useEffect, useRef } from 'preact/hooks' @@ -33,42 +35,58 @@ export const BoxGraphContent = ({ box, data, refreshRef }: Props) => { } useEffect(() => { - // TODO: These should be probably returned by server, could be outdated - const from = filter.customFrom - const to = filter.customTo - const minValue = parseFloat(data.min ?? '') - const maxValue = parseFloat(data.max ?? '') - const customRange = !isNaN(minValue) && !isNaN(maxValue) - if (bodyRef.current && values.data) { + // TODO: These should be probably returned by server, could be outdated + const from = filter.customFrom + const to = filter.customTo + const minValue = parseFloat(data.min ?? '') + const maxValue = parseFloat(data.max ?? '') + const customRange = !isNaN(minValue) || !isNaN(maxValue) + const graphType = data.graphType ?? 'line' + const fill = data.fill ?? undefined + const colorMode = data.colorMode + const staticColor = data.staticColor + + const x = values.data.map((v) => new Date(v.timestamp * 1000)) + const y = values.data.map((v) => v.value) + window.Plotly.newPlot( bodyRef.current, [ { - ...(data.graphType === 'line' && { + ...(graphType === 'line' && { type: 'scatter', mode: 'lines', + fill, }), - ...(data.graphType === 'points' && { + ...(graphType === 'points' && { type: 'scatter', mode: 'markers', + fill, }), - ...(data.graphType === 'lineAndPoints' && { + ...(graphType === 'lineAndPoints' && { type: 'scatter', mode: 'lines+markers', + fill, }), - ...(data.graphType === 'bar' && { type: 'bar' }), - x: values.data.map((v) => new Date(v.timestamp * 1000)), - y: values.data.map((v) => v.value), + ...(graphType === 'bar' && { type: 'bar' }), + x, + y, line: { width: 1, + ...(colorMode === 'static' && { color: staticColor }), }, }, ], { xaxis: { range: [from, to], type: 'date' }, yaxis: { - ...(customRange && { range: [minValue, maxValue] }), + ...(customRange && { + range: [ + isNaN(minValue) ? min(y) : minValue, + isNaN(maxValue) ? max(y) : maxValue, + ], + }), ...(data.unit && { ticksuffix: ` ${data.unit}` }), }, margin: { diff --git a/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx index 8ec26fe..6546465 100644 --- a/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx +++ b/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx @@ -1,3 +1,4 @@ +import { omit } from '@/utils/omit' import { DashboardGraphData } from '@/utils/parseDashboard' import { useEffect, useState } from 'preact/hooks' @@ -8,10 +9,7 @@ type Props = { export const GraphSettings = ({ value, onChange }: Props) => { const [formState, setFormState] = useState(() => ({ - min: value?.min, - max: value?.max, - graphType: value?.graphType, - unit: value?.unit, + ...(value && omit(value, ['type'])), })) const handleChange = (e: Event) => { @@ -43,18 +41,67 @@ export const GraphSettings = ({ value, onChange }: Props) => { +