Skip to content

Commit c9d86e7

Browse files
authored
[add] stacked area chart example (#10)
1 parent 152215e commit c9d86e7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Stacked area chart
3+
sidebar_custom_props:
4+
tags: [fill]
5+
---
6+
7+
This example shows how related series can be compounded into a stacked area chart using <Crossref target="fill-between" />.
8+
9+
```example
10+
#let stacked-area(x, ..y) = {
11+
y
12+
.pos()
13+
.fold(
14+
((0,) * x.len(),) , (ys-stacked, ys) => {
15+
let previous = ys-stacked.last()
16+
ys-stacked.push(lq.vec.add(previous, ys))
17+
ys-stacked
18+
}
19+
)
20+
.windows(2)
21+
.map(((y1, y2)) => lq.fill-between(x, y1, y2: y2))
22+
}
23+
24+
#let x = range(10)
25+
#let y1= (0, 1, 3, 9, 5, 4, 2, 2, 1, 0)
26+
#let y2 = (5, 3, 2, 0, 1, 2, 2, 2, 3, 2)
27+
#let y3 = (0, 0, 0, 0, 1, 2, 4, 5, 5, 9)
28+
29+
#lq.diagram(
30+
..stacked-area(x, y1, y2, y3)
31+
)
32+
```
33+
34+
Stacked area charts are useful for showing the relationship between categories of the same type in one chart,
35+
but beware that including too many series can harm readability.

0 commit comments

Comments
 (0)