Skip to content

Commit 7f5c433

Browse files
author
bottas
committed
Added functionality to Chart Legend
Now the last visible dataset can't be hidden.
1 parent 96d4a07 commit 7f5c433

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

package.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,5 @@
2929
"eslintConfig": {
3030
"extends": "react-app"
3131
},
32-
"browserslist": {
33-
"production": [
34-
">0.2%",
35-
"not dead",
36-
"not op_mini all"
37-
],
38-
"development": [
39-
"last 1 chrome version",
40-
"last 1 firefox version",
41-
"last 1 safari version"
42-
]
43-
}
32+
"browserslist": []
4433
}

src/components/SensorView.tsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const agregation = "day"
1212
const beginDate = timeInMs - weekInMs;
1313
const endDate = timeInMs;
1414

15-
16-
var visibility = true;
15+
var disabledLegend: number;
1716

1817
function SensorView(props: any) {
1918
type ExpectedResponse = {
@@ -34,7 +33,7 @@ function SensorView(props: any) {
3433
const [response, setResponse] = useState<Array<ExpectedResponse> | undefined>();
3534

3635
const [isVisible, setVisible] = useState([true, true, true, true]);
37-
36+
const [disabledLegend, setDisabled] = useState(-1)
3837

3938
let one: Array<Series> = [],
4039
two: Array<Series> = [],
@@ -48,14 +47,6 @@ function SensorView(props: any) {
4847
.then(setResponse)
4948
}, [])
5049

51-
52-
useEffect(() => {
53-
if (isVisible.filter(Boolean).length === 1){
54-
//Desativar onClick do único elemento true, é possivel fazer isto dando o valor 'undefined'
55-
isVisible.findIndex(Boolean)
56-
}
57-
});
58-
5950
if (response !== undefined) {
6051

6152
let total = 0;
@@ -137,29 +128,49 @@ function SensorView(props: any) {
137128
const dotStyle3 = { backgroundColor: data.datasets[2].backgroundColor, };
138129
const dotStyle4 = { backgroundColor: data.datasets[3].backgroundColor, };
139130

131+
var click1 = () => setVisible([!isVisible[0], isVisible[1], isVisible[2], isVisible[3]]);
132+
var click2 = () => setVisible([isVisible[0], !isVisible[1], isVisible[2], isVisible[3]]);
133+
var click3 = () => setVisible([isVisible[0], isVisible[1], !isVisible[2], isVisible[3]]);
134+
var click4 = () => setVisible([isVisible[0], isVisible[1], isVisible[2], !isVisible[3]]);
135+
140136
const legendStyle = (isVisible: boolean): React.CSSProperties => ({
141137
textDecoration: isVisible ? "none" : "line-through",
142138
});
143139

144-
var test = undefined
145-
//var test = () => setVisible([!isVisible[0], isVisible[1], isVisible[2], isVisible[3]])
146140

141+
if (isVisible.filter(Boolean).length === 1) {
142+
setDisabled(isVisible.findIndex(Boolean));
143+
switch (disabledLegend) {
144+
case 0:
145+
click1 = () => undefined;
146+
break;
147+
case 1:
148+
click2 = () => undefined;
149+
break;
150+
case 2:
151+
click3 = () => undefined;
152+
break;
153+
case 3:
154+
click4 = () => undefined;
155+
break;
156+
}
157+
}
147158

148159
return (
149160
<ul>
150-
<li key={data.datasets[0].label} onClick={test}>
161+
<li key={data.datasets[0].label} onClick={click1}>
151162
<span className="dot" style={dotStyle1}></span>
152163
<span className="legend-text" style={legendStyle(isVisible[0])}>{data.datasets[0].label}</span>
153164
</li>
154-
<li key={data.datasets[1].label} onClick={() => setVisible([isVisible[0], !isVisible[1], isVisible[2], isVisible[3]])}>
165+
<li key={data.datasets[1].label} onClick={click2}>
155166
<span className="dot" style={dotStyle2}></span>
156167
<span className="legend-text" style={legendStyle(isVisible[1])}>{data.datasets[1].label}</span>
157168
</li>
158-
<li key={data.datasets[2].label} onClick={() => setVisible([isVisible[0], isVisible[1], !isVisible[2], isVisible[3]])}>
169+
<li key={data.datasets[2].label} onClick={click3}>
159170
<span className="dot" style={dotStyle3}></span>
160171
<span className="legend-text" style={legendStyle(isVisible[2])}>{data.datasets[2].label}</span>
161172
</li>
162-
<li key={data.datasets[3].label} onClick={() => setVisible([isVisible[0], isVisible[1], isVisible[2], !isVisible[3]])}>
173+
<li key={data.datasets[3].label} onClick={click4}>
163174
<span className="dot" style={dotStyle4}></span>
164175
<span className="legend-text" style={legendStyle(isVisible[3])}>{data.datasets[3].label}</span>
165176
</li>

0 commit comments

Comments
 (0)