Skip to content

Commit

Permalink
Moved prepare_plot and render to plotting.typ
Browse files Browse the repository at this point in the history
  • Loading branch information
Pegacraft committed Jul 9, 2023
1 parent 810ebf1 commit f5590c7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
58 changes: 58 additions & 0 deletions plotst/plotting.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,64 @@
#import "util/util.typ": *
#import calc: *

// hackyish solution to split axis and content
#let render(plot, plot_code, render_axis) = style(style => {
let widths = 0pt
let heights = 0pt
// Draw coordinate system
for axis in plot.axes {
let (w,h) = measure_axis(axis, style)
widths += w
heights += h
}

let x_axis = plot.axes.filter(it => not is_vertical(it)).first()
let y_axis = plot.axes.filter(it => is_vertical(it)).first()

let offset_y = 0pt
let offset_x = 0pt
if x_axis.location == "bottom" {
offset_y = -heights
}
if y_axis.location == "left" {
offset_x = widths
}
place(dx: offset_x, dy: -100% + heights+offset_y, box(width: 100% - widths, height: 100% - heights, {
if render_axis {
for axis in plot.axes {
draw_axis(axis)
}
} else {
plot_code()
}
}))
})

// Prepares everything for a plot and executes the function that draws a plot. Supplies it with width and height
// size: the size of the plot either as array(width, height) or length
// caption: the caption for the plot
// capt_dist: distance from plot to caption
//-------
// width: the width of the plot
// height: the height of the plot
// the plot code: a function that needs to look accept parameters (width, height)
// plot: if set this function will attempt to render the axes and prepare everything. If not, the setup is up to you
// if you want to make the axes visible (only if plot is set)
//-------
#let prepare_plot(size, caption, plot_code, plot: (), render_axis: true) = {
let (width, height) = if type(size) == "array" {size} else {(size, size)}
figure(caption: caption, supplement: "Graph", kind: "plot", {
// Graph box
set align(left + bottom)
box(width: width, height: height, fill: none, if plot == () { plot_code() } else {
if render_axis { render(plot, plot_code, true) }
render(plot, plot_code, false)
})
})
}



/// The constructor function for a plot. This combines the `data` with the `axes` you need to display a graph/plot. The exact structure of `axes` and `data` varies from the visual representation you choose. An exact specification of how these have to look will be found there.
/// === Examples
/// This is how your plot initialisation will look most of the time:
Expand Down
57 changes: 0 additions & 57 deletions plotst/util/util.typ
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@
#import "/plotst/axis.typ": *

// hackyish solution to split axis and content
#let render(plot, plot_code, render_axis) = style(style => {
let widths = 0pt
let heights = 0pt
// Draw coordinate system
for axis in plot.axes {
let (w,h) = measure_axis(axis, style)
widths += w
heights += h
}

let x_axis = plot.axes.filter(it => not is_vertical(it)).first()
let y_axis = plot.axes.filter(it => is_vertical(it)).first()

let offset_y = 0pt
let offset_x = 0pt
if x_axis.location == "bottom" {
offset_y = -heights
}
if y_axis.location == "left" {
offset_x = widths
}
place(dx: offset_x, dy: -100% + heights+offset_y, box(width: 100% - widths, height: 100% - heights, {
if render_axis {
for axis in plot.axes {
draw_axis(axis)
}
} else {
plot_code()
}
}))
})

// Prepares everything for a plot and executes the function that draws a plot. Supplies it with width and height
// size: the size of the plot either as array(width, height) or length
// caption: the caption for the plot
// capt_dist: distance from plot to caption
//-------
// width: the width of the plot
// height: the height of the plot
// the plot code: a function that needs to look accept parameters (width, height)
// plot: if set this function will attempt to render the axes and prepare everything. If not, the setup is up to you
// if you want to make the axes visible (only if plot is set)
//-------
#let prepare_plot(size, caption, plot_code, plot: (), render_axis: true) = {
let (width, height) = if type(size) == "array" {size} else {(size, size)}
figure(caption: caption, supplement: "Graph", kind: "plot", {
// Graph box
set align(left + bottom)
box(width: width, height: height, fill: none, if plot == () { plot_code() } else {
if render_axis { render(plot, plot_code, true) }
render(plot, plot_code, false)
})
})
}

// Calculates step size for an axis
// full_dist: the distance of the axis while drawing (most likely width or height)
Expand Down

0 comments on commit f5590c7

Please sign in to comment.