Skip to content

Commit 6861899

Browse files
committed
Add random option to barchart example
1 parent f188ac9 commit 6861899

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

docs/examples/barchart/index.qmd

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,63 @@ This Svelte component accepts a few props:
6262

6363
We can hook any of those values up to OJS code using `myBarChart.propName`.
6464

65-
For example, let's make the data user-configurable:
65+
Let's give the user the choice between a randomly changing bar chart and user-configurable data and colours:
6666

6767
```{ojs}
6868
//| code-fold: true
69+
viewof changeType = Inputs.radio(["Random", "Custom"], {value: "Random"})
70+
```
71+
72+
Here's how we generate data and colours that change every two seconds
73+
74+
```{ojs}
75+
//| code-fold: true
76+
randomData = {
77+
while(true) {
78+
yield Promises.delay(2000, [
79+
Math.floor(Math.random() * 100).toString(),
80+
Math.floor(Math.random() * 100).toString(),
81+
Math.floor(Math.random() * 100).toString(),
82+
Math.floor(Math.random() * 100).toString(),
83+
Math.floor(Math.random() * 100).toString()
84+
]);
85+
}
86+
}
6987
70-
viewof userData = Inputs.form([
71-
Inputs.text({type: "number", value: 25, width: 20}),
72-
Inputs.text({type: "number", value: 35, width: 20}),
73-
Inputs.text({type: "number", value: 65, width: 20}),
74-
Inputs.text({type: "number", value: 5, width: 20}),
75-
Inputs.text({type: "number", value: 50, width: 20})
76-
]);
77-
myBarChart.data = [...userData];
88+
randomColor = {
89+
while(true) {
90+
yield Promises.delay(2000,
91+
`#${Math.floor(Math.random() * 256).toString(16)}${Math.floor(Math.random() * 256).toString(16)}${Math.floor(Math.random() * 256).toString(16)}`);
92+
}
93+
}
94+
```
95+
96+
And here are inputs that allow the user to change the data and colour:
97+
98+
```{ojs}
99+
//| code-fold: true
100+
viewof userData = Inputs.form(
101+
[
102+
Inputs.text({type: "number", value: 25, width: 20}),
103+
Inputs.text({type: "number", value: 35, width: 20}),
104+
Inputs.text({type: "number", value: 65, width: 20}),
105+
Inputs.text({type: "number", value: 5, width: 20}),
106+
Inputs.text({type: "number", value: 50, width: 20})
107+
], {
108+
disabled: changeType == "Random"
109+
});
110+
111+
viewof userColor = Inputs.color({
112+
value: "#36a7e9",
113+
disabled: changeType == "Random"});
78114
```
79115

80-
Or the colour:
116+
Finally, we update the visual whenever either the choice type changes or the random data does:
81117

82118
```{ojs}
83119
//| code-fold: true
84-
viewof userColor = Inputs.color({value: "#36a7e9"});
85-
myBarChart.barColor = userColor;
120+
myBarChart.data = changeType == "Random" ? randomData : [...userData];
121+
myBarChart.barColor = changeType == "Random" ? randomColor : userColor;
86122
```
87123
::::
88124

0 commit comments

Comments
 (0)