Skip to content

Commit 7ccc4ba

Browse files
authored
tgui: fixes chart in power monitor (#980)
* initial commit * does nothing
1 parent 04b205d commit 7ccc4ba

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

tgui/packages/tgui/components/Chart.jsx renamed to tgui/packages/tgui/components/Chart.tsx

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,57 @@
55
*/
66

77
import { map, zipWith } from 'common/collections';
8-
import { Component, createRef } from 'react';
9-
import { Box } from './Box';
8+
import { Component, createRef, RefObject } from 'react';
109

11-
const normalizeData = (data, scale, rangeX, rangeY) => {
10+
import { Box, BoxProps } from './Box';
11+
12+
type Props = {
13+
data: number[][];
14+
} & Partial<{
15+
fillColor: string;
16+
rangeX: [number, number];
17+
rangeY: [number, number];
18+
strokeColor: string;
19+
strokeWidth: number;
20+
}> &
21+
BoxProps;
22+
23+
type State = {
24+
viewBox: [number, number];
25+
};
26+
27+
type Point = number[];
28+
type Range = [number, number];
29+
30+
const normalizeData = (
31+
data: Point[],
32+
scale: number[],
33+
rangeX?: Range,
34+
rangeY?: Range,
35+
) => {
1236
if (data.length === 0) {
1337
return [];
1438
}
39+
1540
const min = zipWith(Math.min)(...data);
1641
const max = zipWith(Math.max)(...data);
42+
1743
if (rangeX !== undefined) {
1844
min[0] = rangeX[0];
1945
max[0] = rangeX[1];
2046
}
47+
2148
if (rangeY !== undefined) {
2249
min[1] = rangeY[0];
2350
max[1] = rangeY[1];
2451
}
25-
const normalized = map((point) => {
26-
return zipWith((value, min, max, scale) => {
52+
53+
const normalized = map((point: Point) => {
54+
return zipWith((value: number, min: number, max: number, scale: number) => {
2755
return ((value - min) / (max - min)) * scale;
2856
})(point, min, max, scale);
2957
})(data);
58+
3059
return normalized;
3160
};
3261

@@ -39,21 +68,18 @@ const dataToPolylinePoints = (data) => {
3968
return points;
4069
};
4170

42-
class LineChart extends Component {
43-
constructor(props) {
71+
class LineChart extends Component<Props> {
72+
ref: RefObject<HTMLDivElement>;
73+
state: State;
74+
75+
constructor(props: Props) {
4476
super(props);
4577
this.ref = createRef();
4678
this.state = {
4779
// Initial guess
4880
viewBox: [600, 200],
4981
};
50-
this.handleResize = () => {
51-
const element = this.ref.current;
52-
if (!element) return;
53-
this.setState({
54-
viewBox: [element.offsetWidth, element.offsetHeight],
55-
});
56-
};
82+
this.handleResize = this.handleResize.bind(this);
5783
}
5884

5985
componentDidMount() {
@@ -65,6 +91,16 @@ class LineChart extends Component {
6591
window.removeEventListener('resize', this.handleResize);
6692
}
6793

94+
handleResize = () => {
95+
const element = this.ref.current;
96+
if (!element) {
97+
return;
98+
}
99+
this.setState({
100+
viewBox: [element.offsetWidth, element.offsetHeight],
101+
});
102+
};
103+
68104
render() {
69105
const {
70106
data = [],
@@ -87,32 +123,32 @@ class LineChart extends Component {
87123
normalized.push([-strokeWidth, first[1]]);
88124
}
89125
const points = dataToPolylinePoints(normalized);
126+
const divProps = { ...rest, className: '', ref: this.ref };
127+
90128
return (
91129
<Box position="relative" {...rest}>
92-
{(props) => (
93-
<div ref={this.ref} {...props}>
94-
<svg
95-
viewBox={`0 0 ${viewBox[0]} ${viewBox[1]}`}
96-
preserveAspectRatio="none"
97-
style={{
98-
position: 'absolute',
99-
top: 0,
100-
left: 0,
101-
right: 0,
102-
bottom: 0,
103-
overflow: 'hidden',
104-
}}
105-
>
106-
<polyline
107-
transform={`scale(1, -1) translate(0, -${viewBox[1]})`}
108-
fill={fillColor}
109-
stroke={strokeColor}
110-
strokeWidth={strokeWidth}
111-
points={points}
112-
/>
113-
</svg>
114-
</div>
115-
)}
130+
<Box {...divProps}>
131+
<svg
132+
viewBox={`0 0 ${viewBox[0]} ${viewBox[1]}`}
133+
preserveAspectRatio="none"
134+
style={{
135+
position: 'absolute',
136+
top: 0,
137+
left: 0,
138+
right: 0,
139+
bottom: 0,
140+
overflow: 'hidden',
141+
}}
142+
>
143+
<polyline
144+
transform={`scale(1, -1) translate(0, -${viewBox[1]})`}
145+
fill={fillColor}
146+
stroke={strokeColor}
147+
strokeWidth={strokeWidth}
148+
points={points}
149+
/>
150+
</svg>
151+
</Box>
116152
</Box>
117153
);
118154
}

tgui/packages/tgui/interfaces/PowerMonitor.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const PowerMonitor = () => {
3535

3636
export const PowerMonitorContent = (props) => {
3737
const { data } = useBackend();
38-
const { history } = data;
38+
const { history = { supply: [], demand: [] } } = data;
3939
const [sortByField, setSortByField] = useLocalState('sortByField', null);
4040
const supply = history.supply[history.supply.length - 1] || 0;
4141
const demand = history.demand[history.demand.length - 1] || 0;
@@ -87,7 +87,7 @@ export const PowerMonitorContent = (props) => {
8787
</Section>
8888
</Flex.Item>
8989
<Flex.Item mx={0.5} grow={1}>
90-
<Section position="relative" height="100%">
90+
<Section position="relative" fill>
9191
<Chart.Line
9292
fillPositionedParent
9393
data={supplyData}

0 commit comments

Comments
 (0)