diff --git a/src/axes.typ b/src/axes.typ index e1dc27b..1d172c9 100644 --- a/src/axes.typ +++ b/src/axes.typ @@ -86,6 +86,7 @@ padding: 0, )) +// Default Schoolbook Style #let default-style-schoolbook = util.merge-dictionary(default-style, ( x: (stroke: auto, fill: none, mark: (start: none, end: "straight"), tick: (label: (anchor: "north"))), @@ -223,14 +224,13 @@ // - ticks (dictionary): Tick settings: // - step (number): Major tic step // - minor-step (number): Minor tic step -// - unit (content): Tick label suffix // - decimals (int): Tick float decimal length // - label (content): Axis label // - mode (string): Axis scaling function. Takes `lin` or `log` // - base (number): Base for tick labels when logarithmically scaled. #let axis(min: -1, max: 1, label: none, ticks: (step: auto, minor-step: none, - unit: none, decimals: 2, grid: false, + decimals: 2, grid: false, format: "float" ), mode: auto, base: auto) = ( @@ -260,9 +260,6 @@ value = str(value) } - if tic-options.at("unit", default: none) != none { - value += tic-options.unit - } return value } diff --git a/src/chart/barchart.typ b/src/chart/barchart.typ index ec7df40..441eeec 100644 --- a/src/chart/barchart.typ +++ b/src/chart/barchart.typ @@ -54,7 +54,6 @@ /// width can be set to `auto`. /// - bar-style (style,function): Style or function (idx => style) to use for /// each bar, accepts a palette function. -/// - 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 @@ -67,7 +66,7 @@ size: (auto, 1), bar-style: palette.red, x-label: none, - x-unit: auto, + x-format: auto, y-label: none, labels: none, ..plot-args @@ -100,9 +99,9 @@ (data.len() - i - 1, t.at(label-key)) }) - let x-unit = x-unit - if x-unit == auto { - x-unit = if mode == "stacked100" {[%]} else [] + let x-format = x-format + if x-format == auto { + x-format = if mode == "stacked100" {plot.formats.decimal.with(suffix: [%])} else {auto} } data = data.enumerate().map(((i, d)) => { @@ -119,6 +118,7 @@ axis-style: "scientific-auto", x-label: x-label, x-grid: true, + x-format: x-format, y-label: y-label, y-min: -y-inset, y-max: data.len() + y-inset - 1, diff --git a/src/chart/columnchart.typ b/src/chart/columnchart.typ index 5a4fcb3..0ab65ad 100644 --- a/src/chart/columnchart.typ +++ b/src/chart/columnchart.typ @@ -54,7 +54,6 @@ /// width can be set to `auto`. /// - bar-style (style,function): Style or function (idx => style) to use for /// each bar, accepts a palette function. -/// - 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 @@ -67,7 +66,7 @@ size: (auto, 1), bar-style: palette.red, x-label: none, - y-unit: auto, + y-format: auto, y-label: none, labels: none, ..plot-args @@ -98,9 +97,9 @@ (i, t.at(label-key)) }) - let y-unit = y-unit - if y-unit == auto { - y-unit = if mode == "stacked100" {[%]} else [] + let y-format = y-format + if y-format == auto { + y-format = if mode == "stacked100" {plot.formats.decimal.with(suffix: [%])} else {auto} } data = data.enumerate().map(((i, d)) => { @@ -117,6 +116,7 @@ axis-style: "scientific-auto", y-grid: true, y-label: y-label, + y-format: y-format, x-min: -x-inset, x-max: data.len() + x-inset - 1, x-tick-step: none, diff --git a/src/plot.typ b/src/plot.typ index 9992fa4..acf36aa 100644 --- a/src/plot.typ +++ b/src/plot.typ @@ -115,9 +115,6 @@ /// Number of decimals digits to display for tick labels, if the format is set /// to `"float"`. /// ]) -/// #show-parameter-block("unit", ("none", "content"), default: "none", [ -/// Suffix to append to all tick labels. -/// ]) /// #show-parameter-block("mode", ("none", "string"), default: "none", [ /// The scaling function of the axis. Takes `lin` (default) for linear scaling, /// and `log` for logarithmic scaling.]) diff --git a/src/plot/formats.typ b/src/plot/formats.typ index f4e8d20..60a12f7 100644 --- a/src/plot/formats.typ +++ b/src/plot/formats.typ @@ -83,27 +83,29 @@ /// could be found, a real number with `digits` digits is used. /// - digits (int): Number of digits to use for rounding /// - eps (number): Epsilon used for comparison +/// - prefix (content): Content to prefix +/// - suffix (content): Content to append /// -> Content if a matching fraction could be found or none -#let multiple-of(value, factor: calc.pi, symbol: $pi$, fraction: true, digits: 2, eps: 1e-6) = { +#let multiple-of(value, factor: calc.pi, symbol: $pi$, fraction: true, digits: 2, eps: 1e-6, prefix: [], suffix: []) = { if _compare(value, 0, eps: eps) { return _block-eq($0$) } let a = value / factor if _compare(a, 1, eps: eps) { - return _block-eq(symbol) + return _block-eq(prefix + symbol + suffix) } else if _compare(a, -1, eps: eps) { - return _block-eq($-$ + symbol) + return _block-eq(prefix + $-$ + symbol + suffix) } if fraction != none { let frac = _find-fraction(a, denom: if fraction == true { auto } else { fraction }) if frac != none { - return _block-eq(frac + symbol) + return _block-eq(prefix + frac + symbol + suffix) } } - return _block-eq($#calc.round(a, digits: digits)$ + symbol) + return _block-eq(prefix + $#calc.round(a, digits: digits)$ + symbol + suffix) } /// Scientific notation tick formatter @@ -119,8 +121,10 @@ /// /// - value (number): Value to format /// - digits (int): Number of digits for rounding the factor +/// - prefix (content): Content to prefix +/// - suffix (content): Content to append /// -> Content -#let sci(value, digits: 2) = { +#let sci(value, digits: 2, prefix: [], suffix: []) = { let exponent = if value != 0 { calc.floor(calc.log(calc.abs(value), base: 10)) } else { @@ -136,10 +140,10 @@ value = calc.round(value, digits: digits) if exponent <= -1 or exponent >= 1 { - return _block-eq($#value times 10^#exponent$) + return _block-eq(prefix + $#value times 10^#exponent$ + suffix) } - return _block-eq($#value$) + return _block-eq(prefix + $#value$ + suffix) } /// Rounded decimal number formatter @@ -155,7 +159,9 @@ /// /// - value (number): Value to format /// - digits (int): Number of digits to round to +/// - prefix (content): Content to prefix +/// - suffix (content): Content to append /// -> Content -#let decimal(value, digits: 2) = { - _block-eq($#calc.round(value, digits: digits)$) +#let decimal(value, digits: 2, prefix: [], suffix: []) = { + _block-eq(prefix + $#calc.round(value, digits: digits)$ + suffix) } diff --git a/tests/axes/ref/1.png b/tests/axes/ref/1.png index 17ba71d..816f0ad 100644 Binary files a/tests/axes/ref/1.png and b/tests/axes/ref/1.png differ diff --git a/tests/axes/test.typ b/tests/axes/test.typ index 21b2e7e..a4b6153 100644 --- a/tests/axes/test.typ +++ b/tests/axes/test.typ @@ -36,7 +36,7 @@ draw-unset: false, top: none, bottom: axes.axis(min: -1, max: 1, ticks: (step: 1, minor-step: auto, - grid: "both", unit: [ units])), + grid: "both", format: plot.formats.decimal.with(prefix: $<-$, suffix: $->$))), left: axes.axis(min: -1, max: 1, ticks: (step: .5, minor-step: auto, grid: false)), right: axes.axis(min: -10, max: 10, ticks: (step: auto, minor-step: auto, diff --git a/tests/chart/ref/1.png b/tests/chart/ref/1.png index 4dcad6c..b7e9f6d 100644 Binary files a/tests/chart/ref/1.png and b/tests/chart/ref/1.png differ diff --git a/tests/plot/violin/test.typ b/tests/plot/violin/test.typ index 3822e2f..93afadb 100644 --- a/tests/plot/violin/test.typ +++ b/tests/plot/violin/test.typ @@ -22,7 +22,6 @@ let default-colors = (palette.blue-colors.at(3), palette.pink-colors.at(3)) plot.plot(size: (9, 6), - y-label: [Age], y-min: -10, y-max: 20, y-tick-step: 10, y-minor-tick-step: 5,