Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Refactoring II #92

Open
wants to merge 21 commits into
base: refactor-plot-part-one
Choose a base branch
from
Open
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
920 changes: 0 additions & 920 deletions src/axes.typ

This file was deleted.

74 changes: 74 additions & 0 deletions src/axis.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#import "/src/ticks.typ"
#import "/src/plot/util.typ"

// Grid modes
#let _get-grid-mode(mode) = {
return if mode == true or mode == "major" {
1
} else if mode == "minor" {
2
} else if mode == "both" {
3
} else {
0
}
}

/// Transform linear axis value to linear space (low, high)
#let _transform-lin(ax, value, low, high) = {
let range = high - low

return low + (value - ax.min) * (range / (ax.max - ax.min))
}

/// Transform log axis value to linear space (low, high)
#let _transform-log(ax, value, low, high) = {
let range = high - low

let f(x) = {
calc.log(calc.max(x, util.float-epsilon), base: ax.base)
}

return low + (f(value) - f(ax.min)) * (range / (f(ax.max) - f(ax.min)))
}

/// Linear Axis Constructor
#let linear(name, min, max, ..options) = (
label: [#name],
name: name, min: min, max: max, base: 10, transform: _transform-lin,
auto-domain: (none, none),
ticks: (step: auto, minor-step: none, format: auto, list: none),
grid: 0,
compute-ticks: ticks.compute-ticks.with("lin"),
) + options.named()

/// Log Axis Constructor
#let logarithmic(name, min, max, base, ..options) = (
label: [#name],
name: name, min: min, max: max, base: base, transform: _transform-log,
auto-domain: (none, none),
ticks: (step: auto, minor-step: none, format: auto, list: none),
grid: 0,
compute-ticks: ticks.compute-ticks.with("log"),
) + options.named()

// Prepare axis
#let prepare(ptx, ax) = {
ax.grid = _get-grid-mode(ax.grid)
if ax.min == none { ax.min = ax.auto-domain.at(0) }
if ax.max == none { ax.max = ax.auto-domain.at(1) }
if ax.min == none or ax.max == none { ax.min = -1e-6; ax.max = +1e-6 }
if "compute-ticks" in ax {
ax.computed-ticks = (ax.compute-ticks)(ax)
}
return ax
}

/// Transform an axis value to a linear value between low and high
/// - ax (axis): Axis
/// - value (number): Value to transform from axis space to linear space
/// - low (number): Linear minimum
/// - high (number): Linear maximum
#let transform(ax, value, low, high) = {
return (ax.transform)(ax, value, low, high)
}
75 changes: 75 additions & 0 deletions src/compat.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#import "/src/plot/util.typ"
#import "/src/cetz.typ"
#import cetz: draw

#let make-cptx(ptx, old) = {
let axes = old.axes.map(name => ptx.axes.at(name))
axes = axes.map(ax => {
if ax.min == none or ax.max == none {
// Compat elements need the axis domain very early
ax.min = ax.auto-domain.at(0)
ax.max = ax.auto-domain.at(1)
}
return ax
})
return (
axes: axes,
)
}

#let draw-old(ptx, transform, body) = {
if body != none {
(ctx => {
ctx.transform = ((1, 0, 0, 0),
(0,-1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1))
let (ctx: _, drawables, bounds) = cetz.process.many(ctx, body)
drawables = cetz.drawable.apply-transform(v => {
let (x, y) = transform(v.slice(0, 2)).first()
return (x, y, 0)
}, drawables)

return (
ctx: ctx,
drawables: drawables,
)
},)
}
}

#let wrap(old) = {
return (
priority: 0,
fn: ptx => {
let old = old
if "x-domain" in old {
ptx = util.set-auto-domain(ptx, (old.axes.at(0),), (old.x-domain,))
}
if "y-domain" in old {
ptx = util.set-auto-domain(ptx, (old.axes.at(1),), (old.y-domain,))
}
if "plot-prepare" in old {
old = (old.plot-prepare)(old, make-cptx(ptx, old))
}

let data = (
axes: old.axes,
label: old.at("label", default: none),
style: old.at("style", default: none),
stroke: (ptx, transform) => {
if "plot-stroke" in old {
draw-old(ptx, transform, (old.plot-stroke)(old, make-cptx(ptx, old)))
}
},
fill: (ptx, transform) => {
if "plot-fill" in old {
draw-old(ptx, transform, (old.plot-fill)(old, make-cptx(ptx, old)))
}
},
)
ptx.data.push(data)
return ptx
}
)
}
1 change: 0 additions & 1 deletion src/lib.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#let version = version(0,1,0)

#import "/src/axes.typ"
#import "/src/plot.typ"
#import "/src/chart.typ"
Loading
Loading