Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/charts/bar/clustered.typ
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@
let series-data = ()

for (series-index, y-key) in y-keys.enumerate() {

series-data.push(
(
label: if label-key != none {labels.at(series-index)},
label: if label-key != none {
labels.at(series-index, default: none)
},
data: for (observation-index, observation) in data.enumerate() {
let x = observation-index - cluster-width/2 + series-index * (bar-width + bar-spacing) + bar-width/2
let x = observation-index - cluster-width / 2 + series-index * (bar-width + bar-spacing) + bar-width/2
let y = observation.at(y-key, default: 0)

((
Expand Down
2 changes: 2 additions & 0 deletions src/charts/bar/plotter.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
..plot-args,
) = {
draw.group(ctx => {
/*
// Setup styles
let style = styles.resolve(
ctx.style,
Expand Down Expand Up @@ -64,5 +65,6 @@
)
}
)
*/
})
}
108 changes: 49 additions & 59 deletions src/charts/bar/stacked.typ
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#import "/src/cetz.typ": canvas, palette
#import "plotter.typ": plotter
#import "/src/cetz.typ" as cetz: canvas, palette
#import "/src/plot.typ"
#import "/src/charts/environment.typ": chart

//#import "plotter.typ": plotter

/// Render a stacked bar chart
/// ```example
/// cetz-plot.chart.bar.stacked(
/// size: (4,4),
/// (
/// ([One], 1, 1, 2, 3),
/// ([Two], 3, 1, 1 ,1),
/// ([Three], 3, 2, 1, 3),
/// ),
/// size: (4, 4),
/// (([One], 1, 1, 2, 3),
/// ([Two], 3, 1, 1 ,1),
/// ([Three], 3, 2, 1, 3)),
/// label-key: 0,
/// y-keys: (1,2,3,4),
/// y-keys: (1, 2, 3, 4),
/// labels: (
/// $0 -> 24$,
/// $25 -> 49$,
/// $50 -> 74$,
/// $75 -> 100$
/// ),
/// $75 -> 100$),
/// )
/// ```
/// - data (array): An array of clusers to plot. Each entry can include a label
Expand Down Expand Up @@ -53,64 +53,52 @@
axes: ("x", "y"),
..plot-args
) = {
let cluster-count = data.len()
let cluster-width = cluster-count * bar-width
let offsets = (0,)*cluster-count
let num-cols = data.len()
let plot-args = plot-args.named()

let series-data = ()
if "ticks" not in plot-args {
plot-args.x-ticks = data.enumerate().map(((x, col)) => {
(x, [#x])
})
}

for (series-index, y-key) in y-keys.enumerate() {
chart(x-tick-step: none, ..plot-args, style => {
import "/src/plot/add.typ"

series-data.push(
(
label: if label-key != none {labels.at(series-index)},
data: for (observation-index, observation) in data.enumerate() {
let x = observation-index
let y = observation.at(y-key, default: 0)
let y-offset = offsets.at(observation-index)
offsets.at(observation-index) += y
((
x: x,
y: y,
y-offset: y-offset,
),)
}
)
)
}
let y-offset = (0,) * num-cols
for (y, y-key) in y-keys.enumerate() {
let row-data = data.enumerate().map(((x, col)) => {
(x, col.at(y-key, default: 0), y-offset.at(x, default: 0))
})

plotter(
data,
series-data,
x-key: "x",
y-key: "y",
y-offset-key: "y-offset",
y-error-key: none,
label-key: label-key,
bar-width: bar-width,
bar-style: bar-style,
axes: axes,
..plot-args,
)
y-offset = y-offset.enumerate().map(((x, offset)) => {
offset + row-data.at(x).at(1)
})

add.bar(row-data,
x-key: 0,
y-key: 1,
y-offset-key: 2,
axes: axes,
style: bar-style)
}
})
}

/// Render a stacked bar chart
/// ```example
/// cetz-plot.chart.bar.stacked100(
/// size: (4,4),
/// (
/// ([One], 1, 1, 2, 3),
/// ([Two], 3, 1, 1 ,1),
/// ([Three], 3, 2, 1, 3),
/// ),
/// size: (4, 4),
/// (([One], 1, 1, 2, 3),
/// ([Two], 3, 1, 1 ,1),
/// ([Three], 3, 2, 1, 3)),
/// label-key: 0,
/// y-keys: (1,2,3,4),
/// y-keys: (1, 2, 3, 4),
/// labels: (
/// $0 -> 24$,
/// $25 -> 49$,
/// $50 -> 74$,
/// $75 -> 100$
/// ),
/// $75 -> 100$),
/// )
/// ```
/// - data (array): An array of clusers to plot. Each entry can include a label
Expand Down Expand Up @@ -147,11 +135,13 @@
..plot-args
) = stacked(
data.map(d=>{
let sum = y-keys.map(k=>d.at(k, default: 0)).sum()
let sum = y-keys.map(d.at.with(default: 0)).sum()
assert(sum != 0,
message: "Y value sum must be != 0!")
for key in y-keys {
d.at(key) /= sum
}
d
return d
}),
labels: labels,
label-key: label-key,
Expand All @@ -161,6 +151,6 @@
bar-style: bar-style,
axes: axes,
y-tick-step: 0.2,
y-format: (it)=>{$#{it*100}%$},
y-format: (it) => { $#{it * 100}%$ },
..plot-args
)
)
13 changes: 5 additions & 8 deletions src/plot/elements/bar.typ
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,19 @@
style: (:),
axes: ("x", "y")
) = {

let x-domain = (
calc.min(..data.map(it=>{it.at(x-key)-bar-width})),
calc.max(..data.map(it=>{it.at(x-key)+bar-width})),
calc.min(..data.map(it => {it.at(x-key) - bar-width / 2})),
calc.max(..data.map(it => {it.at(x-key) + bar-width / 2})),
)

let y-domain = if y-offset-key != none {
(
calc.min(
..data.map(it=>{it.at(y-key)+it.at(y-offset-key, default: 0)}),
..data.map(it=>{it.at(y-key) + it.at(y-offset-key, default: 0)}),
..data.map(it=>{it.at(y-offset-key, default: 0)})
),
calc.max(
..data.map(it=>{it.at(y-key)+it.at(y-offset-key, default: 0)}),
..data.map(it=>{it.at(y-key) + it.at(y-offset-key, default: 0)}),
..data.map(it=>{it.at(y-offset-key, default: 0)})
)
)
Expand Down Expand Up @@ -158,6 +157,4 @@
plot-fill: _fill,
plot-legend-preview: _legend-preview
),)


}
}