From b756ef71c34dbfb5caa8a908ff890afc168fda3d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 25 May 2023 16:29:10 -0700 Subject: Custom attributes in chart --- modern/src/reports/ChartReportPage.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'modern/src') diff --git a/modern/src/reports/ChartReportPage.js b/modern/src/reports/ChartReportPage.js index 3a4aeb4f..2c33f0f5 100644 --- a/modern/src/reports/ChartReportPage.js +++ b/modern/src/reports/ChartReportPage.js @@ -31,6 +31,7 @@ const ChartReportPage = () => { const hours12 = usePreference('twelveHourFormat'); const [items, setItems] = useState([]); + const [types, setTypes] = useState(['speed']); const [type, setType] = useState('speed'); const values = items.map((it) => it[type]); @@ -45,13 +46,16 @@ const ChartReportPage = () => { }); if (response.ok) { const positions = await response.json(); + const keySet = new Set(); + const keyList = []; const formattedPositions = positions.map((position) => { const data = { ...position, ...position.attributes }; const formatted = {}; formatted.fixTime = formatTime(position.fixTime, 'time', hours12); - Object.keys(data).forEach((key) => { + Object.keys(data).filter((key) => !['id', 'deviceId'].includes(key)).forEach((key) => { const value = data[key]; if (typeof value === 'number') { + keySet.add(key); const definition = positionAttributes[key] || {}; switch (definition.dataType) { case 'speed': @@ -77,6 +81,13 @@ const ChartReportPage = () => { }); return formatted; }); + Object.keys(positionAttributes).forEach((key) => { + if (keySet.has(key)) { + keyList.push(key); + keySet.delete(key); + } + }); + setTypes([...keyList, ...keySet]); setItems(formattedPositions); } else { throw Error(await response.text()); @@ -89,9 +100,14 @@ const ChartReportPage = () => {
{t('reportChartType')} - setType(e.target.value)} + disabled={!items.length} + > + {types.map((key) => ( + {positionAttributes[key]?.name || key} ))} @@ -109,7 +125,7 @@ const ChartReportPage = () => { value.toFixed(2)} domain={[minValue - valueRange / 5, maxValue + valueRange / 5]} /> - [value, positionAttributes[key].name]} /> + [value, positionAttributes[key]?.name || key]} /> -- cgit v1.2.3