Skip to content

Comb plot #80

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

Closed
wants to merge 7 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/plot.typ
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "/src/plot/errorbar.typ": add-errorbar
#import "/src/plot/mark.typ"
#import "/src/plot/violin.typ": add-violin
#import "/src/plot/comb.typ": add-comb
#import "/src/plot/formats.typ"
#import plot-legend: add-legend

Expand Down Expand Up @@ -459,7 +460,7 @@
}

draw.set-style(..d.style, ..d.mark-style)
mark.draw-mark(d.data, x, y, d.mark, d.mark-size, size)
mark.draw-mark(d.data, (x, y), d.mark, d.mark-size, size)
})
}
}
Expand Down
115 changes: 115 additions & 0 deletions src/plot/comb.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#import "/src/cetz.typ": draw, vector
#import "util.typ"
#import "line.typ"
#import "annotation.typ"

#let _prepare(self, ctx) = {
let (x-axis, y-axis) = (ctx.x, ctx.y)
self.stroke-paths = self.line-data
.map(((x, y, s, ..)) => {
(
lines: util.compute-stroke-paths(
((x, 0), (x,y)),
x-axis,
y-axis
),
style: s,
)

})
self
}

#let _fill(self, ctx) = {}

#let _stroke(self, ctx) = {
for (lines, style) in self.stroke-paths {
for p in lines {
draw.line(..p, fill: none, ..self.style, ..style)
}
}
}

#let _legend-preview(self) = {
draw.line((0,.5), (1,.5), ..self.style)
}

#let add-comb(
domain: auto,
mz-key: 0,
intensity-key: 1,
label-key: none,
style-key: none,
style: (:),
mark: none,
mark-size: 0.05,
mark-style: (:),
axes: ("x", "y"),
label: none,
label-padding: none,
annotations: auto,
data
) = {

let line-data = data.map(d=>(
d.at(mz-key),
d.at(intensity-key),
if style-key != none {d.at(style-key, default: none)} else {style}
))

let x-domain = (
calc.min(..line-data.map(t => t.at(0))),
calc.max(..line-data.map(t => t.at(0)))
)

let y-domain = if line-data != none {(
calc.min(..line-data.map(t => t.at(1))),
calc.max(..line-data.map(t => t.at(1)))
)}

let annotations = if annotations == auto {
if (label-key == none) {
()
} else {
data.filter(it=>it.at(label-key, default: none) != none)
}
} else if annotations == none {
()
} else {
annotations
}

((:
type: "comb",
label: label,
data: data, /* Raw data */
line-data: line-data, /* Transformed data */
axes: axes,
x-domain: x-domain,
y-domain: y-domain,
style: style,
mark: mark,
mark-size: mark-size,
mark-style: mark-style,
plot-prepare: _prepare,
plot-fill: _fill,
plot-stroke: _stroke,
// plot-legend-preview: _legend-preview,
mz-key: mz-key,
intensity-key: intensity-key,
label-key: label-key,
width: 0.5,
),)

for (x, y, a) in annotations {
annotation.annotate(
draw.content((x,y), [#a], anchor: "south"),
axes: ("x", "y"),
resize: true,
padding: none,
background: false
)
}

}

15 changes: 9 additions & 6 deletions src/plot/mark.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "/src/cetz.typ": draw
#import "/src/axes.typ"
#import "/src/plot/util.typ"

// Draw mark at point with size
#let draw-mark-shape(pt, size, mark, style) = {
Expand Down Expand Up @@ -34,12 +35,14 @@
}
}

#let draw-mark(pts, x, y, mark, mark-size, plot-size) = {
let pts = pts.map(pt => {
axes.transform-vec(plot-size, x, y, none, pt)
}).filter(pt => pt != none)

/// Draw mark shapes for each point in pts
/// - pts (list): Points
/// - all-axes (list): List of axes, order must match pts value order!
#let draw-mark(pts, all-axes, mark, mark-size, plot-size) = {
for pt in pts {
draw-mark-shape(pt, mark-size, mark, (:))
if util.point-in-range(pt, all-axes) {
pt = axes.transform-vec(plot-size, all-axes.at(0), all-axes.at(1), none, pt)
draw-mark-shape(pt, mark-size, mark, (:))
}
}
}
15 changes: 15 additions & 0 deletions src/plot/util.typ
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,18 @@

return axis-dict
}

/// Tests if point pt is contained in the
/// axis interval of each axis in axes
/// - pt (list): Data array
/// - axes (list): List of axes
#let point-in-range(pt, axes) = {
for i in range(0, axes.len()) {
let a = axes.at(i)
let v = pt.at(i)
if v < a.min or v > a.max {
return false
}
}
return true
}
2 changes: 1 addition & 1 deletion tests/plot/annotation/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import draw: *
set-style(rect: (stroke: none))

plot.plot(size: (6, 4), x-tick-step: 1, {
plot.plot(size: (6, 4), x-tick-step: 0.5, {
plot.add(domain: (100, 101), calc.sin)
plot.annotate(padding: .1, {
content( (101.5, 0), [A])
Expand Down
162 changes: 162 additions & 0 deletions tests/plot/comb/ref.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#set page(width: auto, height: auto)
#import "/src/cetz.typ": *
#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#let data = csv("testdata.csv").map(
((x, y,..))=>{
(
float(x),
float(y),
if x in ("41",) {
(stroke: (paint: red))
} else if x in ("93",){
(stroke: (paint: blue))
},
)
}
)

= General case
- Input data is an array of the form (mz, int, ..)
- keys are not explicitly set.
- X, Y ranges not set

#test-case({
plot.plot(
size: (10,6),
// y-max: 100,
// x-min: 0, x-max: 175,
{
plot.add-comb(
label: "Linalool, 70eV",
// style-key: 2,
// style: (stroke: (paint: black)),
data
)
}
)
})


= With domain set
- General case, but X Y domains are defined explicitly and without mistake

#table(
columns: 3,
..(for i in range(0, 9) {
let (x,y) = (calc.div-euclid(i, 3),calc.rem-euclid(i, 3))
(table.cell( x: x, y: 3-y, test-case({
plot.plot(
x-label: none, y-label: none,
x-tick-step: none, y-tick-step: none,
size: (3,3),
x-min: x * 50, x-max: (x+1) * 50,
y-min: y * 33, y-max: (y+1) * 33,
{
plot.add-comb(
data
)
}
)
})),)
})
)

= With uniform style
Applying the same style to the whole series

#test-case({
plot.plot(
size: (10,6),
// y-max: 100,
// x-min: 0, x-max: 175,
{
plot.add-comb(
label: "Linalool, 70eV",
// style-key: 2,
style: (stroke: (paint: black, dash: "dashed")),
data
)
}
)
})

= With uniform style and individual style
Applying the same style across a whole series, except for some for which it is defined explicitly\ as a field set by `style-key`

#test-case({
plot.plot(
size: (10,6),
// y-max: 100,
// x-min: 0, x-max: 175,
{
plot.add-comb(
label: "Linalool, 70eV",
style-key: 2,
style: (stroke: (paint: black, dash: "dashed")),
data
)
}
)
})

= With Marks
Uniform marks across the series

#test-case({
plot.plot(
size: (10,6),
// y-max: 100,
x-min: 35, x-max: 45,
{
plot.add-comb(
label: "Linalool, 70eV",
mark: "-",
mark-size: 0.2,
data
)
// plot.add(domain: (0, 100), x=>x, mark: "x")
}
)
})

= Axis swap
// Test pending upstream
#test-case({
plot.plot(
size: (10,6),
y-max: 0, y-min: 180,
// x-min: 35, x-max: 45,
{
plot.add-comb(
axes: ("y", "x"),
label: "Linalool, 70eV",
// mark: "-",
mark-size: 0.2,
data
)
// plot.add(domain: (0, 100), x=>x, mark: "x")
}
)
})

= Logarithym
// Test pending upstream
#test-case({
plot.plot(
size: (10,6),
// x-min: 35, x-max: 45,
y-max: 100,
y-mode: "log", y-tick-step: 1, y-base: 10, y-format: "sci", y-minor-tick-step: 1,
{
plot.add-comb(
label: "Linalool, 70eV",
// mark: "-",
mark-size: 0.2,
data
)
// plot.add(domain: (0, 100), x=>x, mark: "x")
}
)
})
Loading
Loading