Skip to content

Commit

Permalink
chart: Add labels support
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Nov 20, 2023
1 parent 5f29cc7 commit 06e96be
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
Binary file modified gallery/barchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions gallery/barchart.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.0": canvas, chart
#import "@preview/cetz:0.2.0": canvas, chart, draw

#set page(width: auto, height: auto, margin: .5cm)

Expand All @@ -12,11 +12,14 @@
)

#canvas({
draw.set-style(legend: (fill: white))
chart.barchart(mode: "clustered",
size: (9, auto),
label-key: 0,
value-key: (..range(1, 5)),
bar-width: .8,
x-tick-step: 2.5,
data2)
data2,
labels: ([Low], [Medium], [High], [Very high]),
legend: "legend.inner-north-east",)
})
6 changes: 4 additions & 2 deletions src/lib/chart/barchart.typ
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
/// - x-unit (content,auto): Tick suffix added to each tick label
/// - y-label (content,none): Y axis label
/// - x-label (content,none): x axis label
/// - labels (none,content): Legend labels per x value group
/// - ..plot-args (any): Arguments to pass to `plot.plot`
#let barchart(data,
label-key: 0,
Expand All @@ -56,6 +57,7 @@
x-label: none,
x-unit: auto,
y-label: none,
labels: none,
..plot-args
) = {
assert(type(label-key) in (int, str))
Expand Down Expand Up @@ -94,7 +96,6 @@
root: "barchart", base: barchart-default-style)
draw.set-style(..style)


let y-inset = calc.max(style.y-inset, style.bar-width / 2)
plot.plot(size: size,
axis-style: "scientific-auto",
Expand All @@ -110,7 +111,8 @@
{
plot.add-bar(data,
mode: mode,
bar-width: style.bar-width,
labels: labels,
bar-width: -style.bar-width,
axes: ("y", "x"))
})
})
Expand Down
3 changes: 3 additions & 0 deletions src/lib/chart/columnchart.typ
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
/// - y-unit (content,auto): Tick suffix added to each tick label
/// - y-label (content,none): Y axis label
/// - x-label (content,none): x axis label
/// - labels (none,content): Legend labels per y value group
/// - ..plot-args (any): Arguments to pass to `plot.plot`
#let columnchart(data,
label-key: 0,
Expand All @@ -57,6 +58,7 @@
x-label: none,
y-unit: auto,
y-label: none,
labels: none,
..plot-args
) = {
assert(type(label-key) in (int, str))
Expand Down Expand Up @@ -110,6 +112,7 @@
{
plot.add-bar(data,
mode: mode,
labels: labels,
bar-width: style.bar-width,
axes: ("x", "y"))
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plot/bar.typ
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
message: "Mode must be basic, clustered, stacked or stacked100, but is " + mode)
assert(bar-position in ("start", "center", "end"),
message: "Invalid bar-position '" + bar-position + "'. Allowed values are: start, center, end")
assert(bar-width > 0,
assert(bar-width != 0,
message: "Option bar-width must be > 0, but is " + str(bar-width))

let n = util.max(..data.map(d => d.len() - 1))
Expand Down
Binary file modified tests/chart/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06e96be

Please sign in to comment.