Skip to content

Commit

Permalink
plot: Refine default style (#423)
Browse files Browse the repository at this point in the history
Allow setting different grid style for major and minor grid lines.

Fixes #278
  • Loading branch information
johannes-wolf authored Dec 24, 2023
1 parent 290cb31 commit cc8bbdf
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 14 deletions.
33 changes: 22 additions & 11 deletions src/lib/axes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@
fill: none,
stroke: black,
label: (
offset: .2cm,
anchor: auto,
offset: .2cm, // Axis label offset
anchor: auto, // Axis label anchor
),
tick: (
fill: none,
stroke: black,
length: .1cm,
minor-length: .08cm,
length: .1cm, // Tick length: Number
minor-length: 80%, // Minor tick length: Number, Ratio
label: (
offset: .2cm,
angle: 0deg,
anchor: auto,
offset: .2cm, // Tick label offset
angle: 0deg, // Tick label angle
anchor: auto, // Tick label anchor
)
),
grid: (
stroke: (paint: gray, dash: "dotted"),
fill: none
stroke: (paint: gray.lighten(50%), thickness: 1pt),
),
minor-grid: (
stroke: (paint: gray.lighten(50%), thickness: .5pt),
),
)

Expand All @@ -42,6 +44,9 @@
style.label.offset = n(style.label.offset)
style.tick.length = n(style.tick.length)
style.tick.minor-length = n(style.tick.minor-length)
if type(style.tick.minor-length) == ratio {
style.tick.minor-length = style.tick.minor-length * style.tick.length / 100%
}
style.tick.label.offset = n(style.tick.label.offset)

if "padding" in style {
Expand Down Expand Up @@ -409,14 +414,20 @@
angle: style.tick.label.angle)
}

if grid-mode.major and major or grid-mode.minor and not major {
let show-major-grid = grid-mode.major and major
if show-major-grid or grid-mode.minor and not major {
let (grid-begin, grid-end) = if name in ("top", "bottom") {
((x, 0), (x, h))
} else {
((0, y), (w, y))
}

line(grid-begin, grid-end, ..style.grid)
let grid-style = if show-major-grid {
style.grid
} else {
style.minor-grid
}
line(grid-begin, grid-end, ..grid-style)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/chart/barchart.typ
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#import "/src/styles.typ"

#let barchart-default-style = (
axes: (tick: (length: 0)),
axes: (tick: (length: 0), grid: (stroke: (dash: "dotted"))),
bar-width: .8,
y-inset: 1,
)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/chart/boxwhisker.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#import "/src/styles.typ"

#let boxwhisker-default-style = (
axes: (tick: (length: 0)),
axes: (tick: (length: 0), grid: (stroke: (dash: "dotted"))),
box-width: 0.75,
whisker-width: 0.5,
mark-size: 0.15,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/chart/columnchart.typ
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#import "/src/styles.typ"

#let columnchart-default-style = (
axes: (tick: (length: 0)),
axes: (tick: (length: 0), grid: (stroke: (dash: "dotted"))),
bar-width: .8,
x-inset: 1,
)
Expand Down
Binary file modified tests/axes/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/chart/boxwhisker/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Binary file added tests/plot/grid/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions tests/plot/grid/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *

/* X grid */
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(size: (3, 3),
x-grid: true,
x-tick-step: .5,
y-tick-step: .5,
{
plot.add(((0,0), (1,1)))
})
}))

/* X grid */
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(size: (3, 3),
x-grid: "both",
x-tick-step: .5,
x-minor-tick-step: .25,
y-tick-step: .5,
{
plot.add(((0,0), (1,1)))
})
}))

/* Y grid */
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(size: (3, 3),
y-grid: true,
x-tick-step: .5,
y-tick-step: .5,
{
plot.add(((0,0), (1,1)))
})
}))

/* Y grid */
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(size: (3, 3),
y-grid: "both",
x-tick-step: .5,
y-tick-step: .5,
y-minor-tick-step: .25,
{
plot.add(((0,0), (1,1)))
})
}))

/* X-Y grid */
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(size: (3, 3),
x-grid: "both",
y-grid: "both",
x-tick-step: .5,
x-minor-tick-step: .25,
y-tick-step: .5,
y-minor-tick-step: .25,
{
plot.add(((0,0), (1,1)))
})
}))
Binary file modified tests/plot/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 cc8bbdf

Please sign in to comment.