You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{< include /.sverto/examples/barchart/index.qmd >}}
9
9
:::
10
10
11
-
Let's start with a barchart.
11
+
Let's start with a simple barchart.
12
+
13
+
This isn't going to be one of those barcharts you've seen on TV, what with tick marks and hover effects and such. This one's just some bars, some labels and a baseline. It will, however, come with some useful features to help us reuse it.
14
+
15
+
::::: {.columns .column-page}
16
+
17
+
# Try it out
18
+
19
+
::::{.column width="40%"}
20
+
21
+
The following code initialises our bar chart.
12
22
13
23
```{ojs}
14
-
BarChart = import_svelte("BarChart.svelte")
24
+
//| code-fold: true
25
+
26
+
BarChart =
27
+
import_svelte("BarChart.svelte")
28
+
15
29
myBarChart = new BarChart.default({
16
-
target: document.querySelector("#mybarchart")
30
+
target: document.querySelector("#mybarchart"),
31
+
props: {
32
+
data: [5, 5, 5, 5, 5],
33
+
height: 200,
34
+
width: 200,
35
+
barWidth: 25,
36
+
barColor: "#36a7e9"
37
+
}
17
38
});
18
39
```
19
40
20
41
:::{#mybarchart}
21
42
:::
22
43
23
-
There it is!
44
+
[See the bar chart source at [BarChart.svelte](./BarChart.svelte)]{style="font-size: smaller; font-color: #999999"}
45
+
46
+
::::
47
+
48
+
<!-- TODO - .svelte file content here? -->
49
+
50
+
::::{.column width="5%"}
51
+
::::
52
+
53
+
54
+
::::{.column width="45%"}
55
+
This Svelte component accepts a few props:
56
+
57
+
*`data`: a simple array of (up to) 5 numbers
58
+
*`height`: the height of the chart in pixels
59
+
*`width`: the width of the chart in pixels
60
+
*`barWidth`: the width of each bar
61
+
*`barColor`: the colour of the bars and labels (note the US spelling here)
62
+
63
+
We can hook any of those values up to OJS code using `myBarChart.propName`.
64
+
65
+
For example, let's make the data user-configurable:
This Svelte component's pretty basic, though. What else is it missing?
94
+
95
+
* The height and width are configurable, and the bars resize in response to them, but their CSS transitions are slow to catch up. Ideally we'd have the bars apply the transition when they resize because of a change in data but _not_ in response to a change in chart height or width.
96
+
* We have no axes other than the baseline. That's fine for a lot of uses, but we might want to add those elements for other uses.
97
+
- We could add those elements manually, but the [`d3-axis`](https://github.com/d3/d3-axis) package has some helpers for creating axes quickly!
98
+
* The bars are all the same colour. We could write a function that converts each bar's data value to a colour, and use it for the `<rect>``fill` attribute, but the [`d3-scale-chromatic`](https://github.com/d3/d3-scale-chromatic) also has some helpers to do this quickly!
0 commit comments