Skip to content

Commit 30bc5c5

Browse files
authored
refactor: change line chart to scatter chart and update related data handling (#520)
1 parent 8781d74 commit 30bc5c5

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

app/components/device-detail/graph.tsx

+21-21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'chartjs-adapter-date-fns'
1414
// import { de, enGB } from "date-fns/locale";
1515
import { Download, RefreshCcw, X } from 'lucide-react'
1616
import { useMemo, useRef, useState, useEffect, useContext } from 'react'
17-
import { Line } from 'react-chartjs-2'
17+
import { Scatter } from 'react-chartjs-2'
1818
import { isBrowser, isTablet } from 'react-device-detect'
1919
import Draggable, { type DraggableData } from 'react-draggable'
2020
import { useNavigate, useNavigation, useSearchParams } from 'react-router'
@@ -59,11 +59,11 @@ const GraphWithZoom = (props: any) => {
5959
}, [])
6060

6161
return (
62-
<Line
63-
data={props.lineData}
62+
<Scatter
63+
data={props.chartData}
6464
options={props.options}
6565
ref={props.chartRef}
66-
></Line>
66+
></Scatter>
6767
)
6868
}
6969

@@ -98,7 +98,7 @@ export default function Graph({
9898
const isAggregated = aggregation !== 'raw'
9999

100100
const nodeRef = useRef(null)
101-
const chartRef = useRef<ChartJS<'line'>>(null)
101+
const chartRef = useRef<ChartJS<'scatter'>>(null)
102102

103103
useEffect(() => {
104104
if (chartRef.current) {
@@ -120,7 +120,7 @@ export default function Graph({
120120
// get theme from tailwind
121121
const [theme] = 'light' //useTheme();
122122

123-
const [lineData, setLineData] = useState(() => {
123+
const [chartData, setChartData] = useState(() => {
124124
const includeDeviceName =
125125
sensors.length === 2 && sensors[0].device_name !== sensors[1].device_name
126126

@@ -145,7 +145,7 @@ export default function Graph({
145145
y: measurement.value,
146146
locationId: measurement.locationId,
147147
})),
148-
pointRadius: 0,
148+
pointRadius: 3,
149149
borderColor: sensor.color,
150150
backgroundColor: sensor.color,
151151
yAxisID: index === 0 ? 'y' : 'y1',
@@ -194,7 +194,7 @@ export default function Graph({
194194
const includeDeviceName =
195195
sensors.length === 2 && sensors[0].device_name !== sensors[1].device_name
196196

197-
setLineData({
197+
setChartData({
198198
datasets: sensors
199199
.map(
200200
(
@@ -215,7 +215,7 @@ export default function Graph({
215215
y: measurement.value,
216216
locationId: measurement.locationId,
217217
})),
218-
pointRadius: 0,
218+
pointRadius: 1,
219219
borderColor: sensor.color,
220220
backgroundColor: sensor.color,
221221
yAxisID: index === 0 ? 'y' : 'y1',
@@ -260,7 +260,7 @@ export default function Graph({
260260
})
261261
}, [sensors, isAggregated])
262262

263-
const options: ChartOptions<'line'> = useMemo(() => {
263+
const options: ChartOptions<'scatter'> = useMemo(() => {
264264
return {
265265
maintainAspectRatio: false,
266266
responsive: true,
@@ -324,7 +324,7 @@ export default function Graph({
324324
display: true,
325325
text: sensors[0].title + ' in ' + sensors[0].unit,
326326
},
327-
type: 'linear',
327+
// type: 'linear',
328328
display: true,
329329
position: 'left',
330330
grid: {
@@ -339,7 +339,7 @@ export default function Graph({
339339
display: true,
340340
text: sensors[1] ? sensors[1].title + ' in ' + sensors[1].unit : '', //data.sensors[1].unit
341341
},
342-
type: 'linear',
342+
// type: 'linear',
343343
display: 'auto',
344344
position: 'right',
345345
grid: {
@@ -356,7 +356,7 @@ export default function Graph({
356356
label: (context: any) => {
357357
const dataIndex = context.dataIndex
358358
const datasetIndex = context.datasetIndex
359-
const point = lineData.datasets[datasetIndex].data[dataIndex]
359+
const point = chartData.datasets[datasetIndex].data[dataIndex]
360360
const locationId = point.locationId
361361
setHoveredPoint(locationId)
362362
return `${context.dataset.label}: ${context.raw.y}`
@@ -405,7 +405,7 @@ export default function Graph({
405405
setColorPickerState({
406406
open: !colorPickerState.open,
407407
index,
408-
color: lineData.datasets[index].borderColor as string,
408+
color: chartData.datasets[index].borderColor as string,
409409
})
410410
},
411411
labels: {
@@ -421,18 +421,18 @@ export default function Graph({
421421
currentZoom?.xMax,
422422
theme,
423423
sensors,
424-
lineData.datasets,
424+
chartData.datasets,
425425
setHoveredPoint,
426426
colorPickerState.open,
427427
])
428428

429429
function handleColorChange(newColor: string) {
430-
const updatedDatasets = [...lineData.datasets]
430+
const updatedDatasets = [...chartData.datasets]
431431
updatedDatasets[colorPickerState.index].borderColor = newColor
432432
updatedDatasets[colorPickerState.index].backgroundColor = newColor
433433

434-
// Update the lineData state with the new dataset colors
435-
setLineData((prevData) => ({
434+
// Update the chartData state with the new dataset colors
435+
setChartData((prevData) => ({
436436
...prevData,
437437
datasets: updatedDatasets,
438438
}))
@@ -459,14 +459,14 @@ export default function Graph({
459459
}
460460

461461
function handleCsvDownloadClick() {
462-
const labels = lineData.datasets[0].data.map((point: any) => point.x)
462+
const labels = chartData.datasets[0].data.map((point: any) => point.x)
463463

464464
let csvContent = 'timestamp,deviceId,sensorId,value,unit,phenomena\n'
465465

466466
// Loop through each timestamp and sensor data
467467
labels.forEach((timestamp: any, index: string | number) => {
468468
sensors.forEach((sensor: any) => {
469-
const dataset = lineData.datasets.find(
469+
const dataset = chartData.datasets.find(
470470
(ds: { label: string | any[] }) => ds.label.includes(sensor.title),
471471
)
472472
if (dataset) {
@@ -598,7 +598,7 @@ export default function Graph({
598598
<ClientOnly fallback={<Spinner />}>
599599
{() => (
600600
<GraphWithZoom
601-
lineData={lineData}
601+
chartData={chartData}
602602
options={options}
603603
chartRef={chartRef} // Pass chartRef as a prop
604604
/>

0 commit comments

Comments
 (0)