Skip to content

Commit 36ca1f2

Browse files
committed
Documentation and cleanup
1 parent a49f528 commit 36ca1f2

File tree

2 files changed

+60
-59
lines changed

2 files changed

+60
-59
lines changed

manual.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module imported into the namespace.
5454
= Plot
5555

5656
#doc-style.parse-show-module("/src/plot.typ")
57-
#for m in ("line", "bar", "boxwhisker", "contour", "errorbar", "annotation", "formats", "violin") {
57+
#for m in ("line", "bar", "boxwhisker", "contour", "errorbar", "annotation", "formats", "violin", "comb") {
5858
doc-style.parse-show-module("/src/plot/" + m + ".typ")
5959
}
6060

src/plot/comb.typ

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33
#import "line.typ"
44
#import "annotation.typ"
55

6+
// Internal: This function takes the line-data (a sanitized input) and calculates
7+
// which points should be visible, and if they are partially clipped, recalcuates
8+
// positions
69
#let _prepare(self, ctx) = {
710
self.stroke-paths = self.line-data
8-
.map(((x, y, s, ..)) => {
9-
(
10-
lines: util.compute-stroke-paths(
11-
((x, 0), (x,y)),
12-
ctx.axes
13-
),
14-
style: s,
15-
)
16-
17-
})
11+
.map(
12+
((x, y, style, ..)) => {(
13+
lines: util.compute-stroke-paths( ((x, 0), (x,y)), ctx.axes),
14+
style: style,
15+
)})
1816
self
1917
}
2018

21-
#let _fill(self, ctx) = {}
22-
19+
// Visible: Draw the lines using the pre-calculated stroke paths from earlier.
20+
// The overall style is first applied, and then overriden
2321
#let _stroke(self, ctx) = {
2422
for (lines, style) in self.stroke-paths {
2523
for p in lines {
@@ -28,60 +26,78 @@
2826
}
2927
}
3028

31-
#let _legend-preview(self) = {
32-
draw.line((0,.5), (1,.5), ..self.style)
33-
}
34-
29+
/// Add a comb plot to a plot environment.
30+
///
31+
/// Must be called from the body of a `plot(..)` command.
32+
///
33+
/// #example(```
34+
/// let points = (
35+
/// (0,4),
36+
/// (1,2),
37+
/// (2,5, (stroke: red)),
38+
/// (3,1),
39+
/// (4,3)
40+
/// )
41+
/// plot.plot(size: (12, 3), y-min: 0, x-inset: 0.5, y-inset: (0,0.5), {
42+
/// plot.add-comb(
43+
/// points,
44+
/// style-key: 2 // Indicate which key sfor tyle overrides (optional)
45+
/// )
46+
/// })
47+
/// ```, vertical: true)
48+
///
49+
/// - data (array,dictionary): Array of 2D data points (and optionally a style
50+
/// override)
51+
/// - x-key (int,string): Key to use for retrieving an x-value from
52+
/// a single data entry. This value gets passed to the `.at(...)`
53+
/// function of a data item. Resulting value must be a number.
54+
/// - y-key (int,string): Key to use for retrieving a
55+
/// y-value. Resulting value must be a number.
56+
/// - style (style): Style to use, can be used with a `palette` function
57+
/// - style-key (int,string,none): Key to use for retrieving a `style`
58+
/// with which to override the current style. Resulting value must
59+
/// be either a `style` or `none`
60+
/// - mark (string): Mark symbol to place at each distinct value of the
61+
/// graph. Uses the `mark` style key of `style` for drawing.
62+
/// - mark-size (float): Mark size in cavas units
63+
/// - mark-style (style): Style override for marks.
64+
/// - axes (axes): Name of the axes to use for plotting. Reversing the axes
65+
/// means rotating the plot by 90 degrees
66+
/// - label (none, content): The name of the category to be shown in the legend.
3567
#let add-comb(
36-
domain: auto,
37-
mz-key: 0,
38-
intensity-key: 1,
39-
label-key: none,
68+
x-key: 0,
69+
y-key: 1,
4070
style-key: none,
4171
style: (:),
4272
mark: none,
4373
mark-size: 0.05,
4474
mark-style: (:),
4575
axes: ("x", "y"),
4676
label: none,
47-
label-padding: none,
48-
annotations: auto,
4977
data
5078
) = {
5179

5280
let line-data = data.map(d=>(
53-
d.at(mz-key),
54-
d.at(intensity-key),
55-
if style-key != none {d.at(style-key, default: none)} else {style}
81+
x: d.at(x-key),
82+
y: d.at(y-key),
83+
style: if style-key != none {d.at(style-key, default: none)} else {style},
5684
))
5785

5886
let x-domain = (
59-
calc.min(..line-data.map(t => t.at(0))),
60-
calc.max(..line-data.map(t => t.at(0)))
87+
calc.min(..line-data.map(t => t.x)),
88+
calc.max(..line-data.map(t => t.x))
6189
)
6290

6391
let y-domain = if line-data != none {(
64-
calc.min(..line-data.map(t => t.at(1))),
65-
calc.max(..line-data.map(t => t.at(1)))
92+
calc.min(..line-data.map(t => t.y)),
93+
calc.max(..line-data.map(t => t.y))
6694
)}
6795

68-
let annotations = if annotations == auto {
69-
if (label-key == none) {
70-
()
71-
} else {
72-
data.filter(it=>it.at(label-key, default: none) != none)
73-
}
74-
} else if annotations == none {
75-
()
76-
} else {
77-
annotations
78-
}
79-
8096
((:
8197
type: "comb",
8298
label: label,
83-
data: data, /* Raw data */
84-
line-data: line-data, /* Transformed data */
99+
data: line-data.map(((x, y,..))=>(x,y)), /* X-Y data */
100+
line-data: line-data, /* formatted data */
85101
axes: axes,
86102
x-domain: x-domain,
87103
y-domain: y-domain,
@@ -90,23 +106,8 @@
90106
mark-size: mark-size,
91107
mark-style: mark-style,
92108
plot-prepare: _prepare,
93-
plot-fill: _fill,
94109
plot-stroke: _stroke,
95-
// plot-legend-preview: _legend-preview,
96-
mz-key: mz-key,
97-
intensity-key: intensity-key,
98-
label-key: label-key,
99110
width: 0.5,
100111
),)
101112

102-
for (x, y, a) in annotations {
103-
annotation.annotate(
104-
draw.content((x,y), [#a], anchor: "south"),
105-
axes: ("x", "y"),
106-
resize: true,
107-
padding: none,
108-
background: false
109-
)
110-
}
111-
112113
}

0 commit comments

Comments
 (0)