Skip to content

Commit 81b52c0

Browse files
committed
Refactoring Strip Chart
1 parent b685581 commit 81b52c0

File tree

1 file changed

+107
-88
lines changed

1 file changed

+107
-88
lines changed

src/ui/widgets/StripChart/stripChart.tsx

Lines changed: 107 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,19 @@ export const StripChartComponent = (
7676
} = props;
7777

7878
// If we're passed an empty array fill in defaults
79-
if (traces.length < 1) traces.push(new Trace());
80-
if (axes.length < 1) axes.push(new Axis({ xAxis: false }));
79+
const localAxes = useMemo(
80+
() => (axes.length > 0 ? [...axes] : [new Axis({ xAxis: false })]),
81+
[axes]
82+
);
8183

8284
// Convert start time into milliseconds period
8385
const timePeriod = useMemo(() => convertStringTimePeriod(start), [start]);
84-
const [minX, setMinX] = useState<Date>(
85-
new Date(new Date().getTime() - timePeriod)
86-
);
87-
const [maxX, setMaxX] = useState<Date>(new Date());
86+
const [dateRange, setDateRange] = useState<{ minX: Date; maxX: Date }>({
87+
minX: new Date(new Date().getTime() - timePeriod),
88+
maxX: new Date()
89+
});
8890
const [data, setData] = useState<TimeSeriesPoint[]>([]);
8991

90-
useEffect(() => {
91-
setData(currentData => {
92-
// Remove outdated data points
93-
let i = 0;
94-
while (i < currentData.length && currentData[i].dateTime < minX) {
95-
i++;
96-
}
97-
98-
return i - 1 > 0 ? currentData.slice(i - 1) : currentData;
99-
});
100-
}, [minX]);
101-
10292
useEffect(() => {
10393
const updateDataMap = (timeSeries: TimeSeriesPoint[]) => {
10494
if (pvData) {
@@ -115,6 +105,19 @@ export const StripChartComponent = (
115105
allDates[0]
116106
);
117107

108+
// Remove outdated data points
109+
let i = 0;
110+
while (
111+
i < timeSeries.length &&
112+
timeSeries[i].dateTime <
113+
new Date(mostRecentDate.getTime() - timePeriod)
114+
) {
115+
i++;
116+
}
117+
const truncatedTimeSeries =
118+
i - 1 > 0 ? timeSeries.slice(i - 1) : timeSeries;
119+
120+
// create new data point
118121
let newTimeseriesPoint: TimeSeriesPoint = { dateTime: mostRecentDate };
119122

120123
pvData.forEach(pvItem => {
@@ -125,88 +128,104 @@ export const StripChartComponent = (
125128
};
126129
});
127130

128-
return [...timeSeries, newTimeseriesPoint];
131+
return [...truncatedTimeSeries, newTimeseriesPoint];
129132
}
130133

131134
return timeSeries;
132135
};
133136

134-
setMinX(new Date(new Date().getTime() - timePeriod));
135-
setMaxX(new Date());
136-
137+
setDateRange({
138+
minX: new Date(new Date().getTime() - timePeriod),
139+
maxX: new Date()
140+
});
137141
setData(currentData => updateDataMap(currentData));
138142
}, [timePeriod, pvData]);
139143

140-
// For some reason the below styling doesn't change axis line and tick
141-
// colour so we set it using sx in the Line Chart below by passing this in
142-
const yAxesStyle: any = {};
144+
const { yAxes, yAxesStyle } = useMemo(() => {
145+
// For some reason the below styling doesn't change axis line and tick
146+
// colour so we set it using sx in the Line Chart below by passing this in
147+
const yAxesStyle: any = {};
143148

144-
const yAxes: ReadonlyArray<YAxis<any>> = axes.map((item, idx) => {
145-
const axis = {
146-
width: 45,
147-
id: idx,
148-
label: item.title,
149-
color: item.color?.toString(),
150-
labelStyle: {
151-
font: item.titleFont.css(),
152-
fill: item.color.toString()
153-
},
154-
tickLabelStyle: {
155-
font: item.scaleFont.css(),
156-
fill: item.color.toString()
157-
},
158-
scaleType: item.logScale ? "symlog" : "linear",
159-
position: "left",
160-
min: item.autoscale ? undefined : item.minimum,
161-
max: item.autoscale ? undefined : item.maximum
162-
};
163-
yAxesStyle[`.MuiChartsAxisId${idx}`] = {
164-
".MuiChartsAxis-line": {
165-
stroke: item.color.toString()
166-
},
167-
".MuiChartsAxis-tick": {
168-
stroke: item.color.toString()
169-
}
170-
};
171-
return axis;
172-
});
149+
localAxes.forEach((item, idx) => {
150+
yAxesStyle[`.MuiChartsAxisId${idx}`] = {
151+
".MuiChartsAxis-line": {
152+
stroke: item.color.toString()
153+
},
154+
".MuiChartsAxis-tick": {
155+
stroke: item.color.toString()
156+
}
157+
};
158+
});
159+
160+
const yAxes: ReadonlyArray<YAxis<any>> = localAxes.map(
161+
(item, idx): YAxis<any> => ({
162+
width: 45,
163+
id: idx,
164+
label: item.title,
165+
color: item.color?.toString(),
166+
labelStyle: {
167+
font: item.titleFont.css(),
168+
fill: item.color.toString()
169+
},
170+
tickLabelStyle: {
171+
font: item.scaleFont.css(),
172+
fill: item.color.toString()
173+
},
174+
scaleType: item.logScale ? "symlog" : "linear",
175+
position: "left",
176+
min: item.autoscale ? undefined : item.minimum,
177+
max: item.autoscale ? undefined : item.maximum
178+
})
179+
);
173180

174-
const xAxis: ReadonlyArray<XAxis<any>> = [
175-
{
176-
color: foregroundColor.toString(),
177-
dataKey: "dateTime",
178-
min: minX,
179-
max: maxX,
180-
scaleType: "time"
181-
}
182-
];
181+
return { yAxes, yAxesStyle };
182+
}, [localAxes]);
183183

184-
const series = traces
185-
?.map((item, index) => {
186-
const pvName = item?.yPv;
187-
const effectivePvName = pvData
188-
?.map(pvItem => pvItem.effectivePvName)
189-
?.find(effectivePvName => pvName && effectivePvName?.endsWith(pvName));
190-
if (!effectivePvName) {
191-
return null;
184+
const xAxis: ReadonlyArray<XAxis<any>> = useMemo(
185+
() => [
186+
{
187+
color: foregroundColor.toString(),
188+
dataKey: "dateTime",
189+
min: dateRange.minX,
190+
max: dateRange.maxX,
191+
scaleType: "time"
192192
}
193+
],
194+
[dateRange, foregroundColor]
195+
);
193196

194-
return {
195-
id: index, // item.axis <= axes.length - 1 ? item.axis : 0,
196-
dataKey: effectivePvName,
197-
label: item.name,
198-
color: visible ? item.color.toString() : "transparent",
199-
showMark: item.pointType === 0 ? false : true,
200-
shape: MARKER_STYLES[item.pointType],
201-
line: {
202-
strokeWidth: item.lineWidth
203-
},
204-
area: item.traceType === 5 ? true : false,
205-
connectNulls: false,
206-
curve: item.traceType === 2 ? ("stepAfter" as CurveType) : "linear"
207-
};
208-
})
209-
.filter(x => !!x);
197+
const series = useMemo(
198+
() =>
199+
(traces?.length > 1 ? traces : [new Trace()])
200+
?.map((item, index) => {
201+
const pvName = item?.yPv;
202+
const effectivePvName = pvData
203+
?.map(pvItem => pvItem.effectivePvName)
204+
?.find(
205+
effectivePvName => pvName && effectivePvName?.endsWith(pvName)
206+
);
207+
if (!effectivePvName) {
208+
return null;
209+
}
210+
211+
return {
212+
id: index,
213+
dataKey: effectivePvName,
214+
label: item.name,
215+
color: visible ? item.color.toString() : "transparent",
216+
showMark: item.pointType === 0 ? false : true,
217+
shape: MARKER_STYLES[item.pointType],
218+
line: {
219+
strokeWidth: item.lineWidth
220+
},
221+
area: item.traceType === 5 ? true : false,
222+
connectNulls: false,
223+
curve: item.traceType === 2 ? ("stepAfter" as CurveType) : "linear"
224+
};
225+
})
226+
.filter(x => !!x),
227+
[traces, pvData, visible]
228+
);
210229

211230
// TO DO
212231
// Add error bars option
@@ -229,7 +248,7 @@ export const StripChartComponent = (
229248
</Typography>
230249
<LineChart
231250
hideLegend={showLegend}
232-
grid={{ vertical: axes[0].showGrid, horizontal: showGrid }}
251+
grid={{ vertical: localAxes[0].showGrid, horizontal: showGrid }}
233252
dataset={data}
234253
sx={{
235254
width: "100%",

0 commit comments

Comments
 (0)