Skip to content

Commit

Permalink
Charts WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrswift committed Aug 5, 2024
1 parent cb8505b commit 91c3419
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/chart.typ
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#import "charts/bar/bar.typ" as bar: stacked, clustered, simple
#import "charts/bar/bar.typ" as bar: stacked, clustered, simple
#import "charts/area/impl.typ" as area: stacked, stacked100
1 change: 1 addition & 0 deletions src/charts/area/impl.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "stacked.typ": stacked, stacked100
53 changes: 53 additions & 0 deletions src/charts/area/plotter.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#import "/src/cetz.typ": draw, styles, palette
#import "/src/plot.typ": plot
#import "/src/plot/add.typ" as add: series, xy, fill-between
#import "style.typ": areachart-default-style

#let plotter(
series-data,
x-list: (),
area-style: palette.red,
axes: ("x", "y"),
stack: false,
..plot-args,
) = draw.group(ctx => {

// Setup styles
let style = styles.resolve(
ctx.style,
merge: (:),
root: "areachart",
base: areachart-default-style
)
draw.set-style(..style)

plot(
y-grid: true,

plot-style: area-style,
..plot-args,

{
let y-offsets = (0,) * x-list.len()
for (label, data) in series-data {
add.series(
label: label,
{
add.fill-between(
data.enumerate().map(((k,v))=>(x-list.at(k), v + y-offsets.at(k))),
data.enumerate().map(((k,v))=>(x-list.at(k), y-offsets.at(k))),
)

if stack == true {
for (key, value) in data.enumerate() {
y-offsets.at(key) += value
}
}

}
)
}
}
)

})
56 changes: 56 additions & 0 deletions src/charts/area/stacked.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#import "/src/cetz.typ": canvas, palette
#import "plotter.typ": plotter

#let stacked(
data,
x-key: 0,
y-keys: (1,),
area-style: palette.red,
axes: ("x", "y"),
..plot-args
) = {

let series-data = ()
for (series-index, data) in data.enumerate(){
series-data.push(
(
label: if label-key != none {data.at(label-key)},
data: y-keys.map(k=>data.at(k, default: 0))
)
)
}

plotter(
series-data,
number-points: y-keys.len(),
x-list: x-list,
area-style: area-style,
axes: axes,
stack: true,
..plot-args
)
}

#let stacked100(
data,
label-key: 0,
x-key: 1,
y-keys: (2,),
area-style: palette.red,
axes: ("x", "y"),
..plot-args
) = stacked(
data.map(d=>{
let sum = y-keys.map(k=>d.at(k, default: 0)).sum()
for key in y-keys {
d.at(key) /= sum
}
d
}),
label-key: label-key,
x-key: x-key,
y-keys: y-keys,
area-style: area-style,
axes: axes,
..plot-args
)
4 changes: 4 additions & 0 deletions src/charts/area/style.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#let areachart-default-style = (
axes: (tick: (length: 0), grid: (stroke: (dash: "dotted"))),
y-inset: 1,
)
22 changes: 22 additions & 0 deletions tests/charts/area/stacked/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#set page(width: auto, height: auto, margin: 1cm)
#import "/tests/helper.typ": *

#let data = (
([15-24], 18.0, 20.1, 23.0, 17.0),
([25-29], 16.3, 17.6, 19.4, 15.3),
([30-34], 14.0, 15.3, 13.9, 18.7),
([35-44], 35.5, 26.5, 29.4, 25.8),
([45-54], 25.0, 20.6, 22.4, 22.0),
([55+], 19.9, 18.2, 19.2, 16.4),
)

#let x-list = (0, 3.3, 6.6, 9.9)

#test-case(cetz-plot.chart.area.stacked100(
size: (10,9),
label-key: 0,
y-keys: (1,2,3,4),
x-list: x-list,
labels: ([Low], [Medium], [High], [Very high]),
data,
))
22 changes: 22 additions & 0 deletions tests/charts/area/stacked100/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#set page(width: auto, height: auto, margin: 1cm)
#import "/tests/helper.typ": *

#let data = (
(0, 18.0, 20.1, 23.0, 17.0),
(2.5, 16.3, 17.6, 19.4, 15.3),
(5, 14.0, 15.3, 13.9, 18.7),
(7.5, 35.5, 26.5, 29.4, 25.8),
(10, 25.0, 20.6, 22.4, 22.0),
(12.5, 19.9, 18.2, 19.2, 16.4),
)

#let x-list = (0, 3.3, 6.6, 9.9)

#test-case(cetz-plot.chart.area.stacked(
size: (10,9),
x-key: 0,
y-keys: (1,2,3,4),
x-list: x-list,
labels: ([Low], [Medium], [High], [Very high]),
data,
))

0 comments on commit 91c3419

Please sign in to comment.