Skip to content

Commit 943bc93

Browse files
committed
Refactor annotation to a class
1 parent e270bf6 commit 943bc93

File tree

6 files changed

+117
-111
lines changed

6 files changed

+117
-111
lines changed

site/js/site.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function onSiteDependenciesLoaded() {
212212
*
213213
* - `x`: x coordinate of a line parallel to the y-axis
214214
* - `y`: y coordinate of a line parallel to the x-axis
215-
* - `text` (optional) text shown next to the parallel line
215+
* - `label` (optional) text shown next to the parallel line
216216
*
217217
* NOTE: either `x` or `y` need to be set on the object, setting both of them
218218
* will raise an exception
@@ -231,11 +231,11 @@ function onSiteDependenciesLoaded() {
231231
},
232232
{
233233
x: 1,
234-
text: 'x = 1'
234+
label: 'x = 1'
235235
},
236236
{
237237
y: 2,
238-
text: 'y = 2'
238+
label: 'y = 2'
239239
}
240240
]
241241
})

site/partials/examples.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<ul>
114114
<li><code>x</code>: x coordinate of a line parallel to the y-axis</li>
115115
<li><code>y</code>: y coordinate of a line parallel to the x-axis</li>
116-
<li><code>text</code> (optional) text shown next to the parallel line</li>
116+
<li><code>label</code> (optional) text shown next to the parallel line</li>
117117
</ul>
118118
<p>NOTE: either <code>x</code> or <code>y</code> need to be set on the object, setting both of them
119119
will raise an exception</p></div><div class="code"><pre><code class="javascript">functionPlot({
@@ -130,11 +130,11 @@
130130
},
131131
{
132132
x: 1,
133-
text: 'x = 1'
133+
label: 'x = 1'
134134
},
135135
{
136136
y: 2,
137-
text: 'y = 2'
137+
label: 'y = 2'
138138
}
139139
]
140140
})</code></pre></div></div><div class="col-md-6 center-block demos"><span class="graph" id="annotations"></span></div></div></div></div><div class="example"><div class="container"><div class="row"><div class="col-md-6"><div class="comment"><h3>Range and closed path</h3>

src/chart.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import { FunctionPlotDatum, FunctionPlotOptions, FunctionPlotScale, FunctionPlot
1111
import { IntervalWorkerPool } from './samplers/interval_worker_pool.js'
1212

1313
import { Mark } from './graph-types/mark.js'
14-
import { interval, polyline, scatter, text } from './graph-types/index.js'
14+
import { annotation, interval, polyline, scatter, text } from './graph-types/index.js'
1515

16-
import annotations from './helpers/annotations.js'
1716
import mousetip from './tip.js'
1817
import { helpers } from './helpers/index.js'
1918
import datumDefaults from './datum-defaults.js'
@@ -543,7 +542,12 @@ export class Chart extends EventEmitter.EventEmitter {
543542
}
544543

545544
// annotations
546-
content.merge(contentEnter).call(annotations({ owner: self }))
545+
content.merge(contentEnter).each(function (d: Mark | FunctionPlotDatum) {
546+
const selection = d3Select(this)
547+
const ann = annotation(d)
548+
ann.chart = self
549+
ann.render(selection as any)
550+
})
547551

548552
// content construction
549553
// - join options.data to <g class='graph'> elements

src/graph-types/annotation.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { line as d3Line } from 'd3-shape'
2+
import { Selection } from 'd3-selection'
3+
4+
import { FunctionPlotOptions } from '../types.js'
5+
import { Mark } from '../graph-types/mark.js'
6+
7+
const line = d3Line()
8+
.x((d) => d[0])
9+
.y((d) => d[1])
10+
11+
export class Annotation extends Mark {
12+
constructor(options: any) {
13+
super(options)
14+
}
15+
16+
render(parentSelection: Selection<any, FunctionPlotOptions, any, any>) {
17+
// join
18+
const selection = parentSelection.selectAll('g.annotations').data(function (d: FunctionPlotOptions) {
19+
return d.annotations || []
20+
})
21+
22+
// enter
23+
const enter = selection.enter().append('g').attr('class', 'annotations')
24+
25+
const xScale = this.chart.meta.xScale
26+
const yScale = this.chart.meta.yScale
27+
28+
// enter + update
29+
// - path
30+
const yRange = yScale.range()
31+
const xRange = xScale.range()
32+
33+
// prettier-ignore
34+
const path = selection.merge(enter).selectAll('path')
35+
.data(function (d) {
36+
if ('x' in d) {
37+
return [[[0, yRange[0]], [0, yRange[1]]]]
38+
} else {
39+
return [[[xRange[0], 0], [xRange[1], 0]]]
40+
}
41+
})
42+
// enter
43+
const pathEnter = path.enter().append('path')
44+
// enter + update
45+
path
46+
.merge(pathEnter)
47+
.attr('stroke', '#eee')
48+
.attr('d', line as any)
49+
path.exit().remove()
50+
51+
// join
52+
const text = selection
53+
.merge(enter)
54+
.selectAll('text')
55+
.data((d) => [
56+
{
57+
label: d.label || '',
58+
// used to determine if x or y is set.
59+
xIsSet: 'x' in d
60+
}
61+
])
62+
// enter
63+
const textEnter = text.enter().append('text')
64+
// enter + update
65+
text
66+
.merge(textEnter)
67+
.text((d) => d.label)
68+
.attr('y', function (d) {
69+
return d.xIsSet ? 3 : 0
70+
})
71+
.attr('x', function (d) {
72+
return d.xIsSet ? 0 : 3
73+
})
74+
.attr('dy', function (d) {
75+
return d.xIsSet ? 5 : -5
76+
})
77+
.attr('text-anchor', function (d) {
78+
return d.xIsSet ? 'end' : ''
79+
})
80+
.attr('transform', function (d) {
81+
return d.xIsSet ? 'rotate(-90)' : ''
82+
})
83+
text.exit().remove()
84+
85+
// enter + update
86+
// move group
87+
selection.merge(enter).attr('transform', function (d) {
88+
if ('x' in d) {
89+
return 'translate(' + xScale(d.x) + ', 0)'
90+
} else {
91+
return 'translate(0, ' + yScale(d.y) + ')'
92+
}
93+
})
94+
95+
// exit
96+
selection.exit().remove()
97+
}
98+
}
99+
100+
export function annotation(options: any) {
101+
return new Annotation(options)
102+
}

src/graph-types/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Interval, interval } from './interval.js'
22
import { Polyline, polyline } from './polyline.js'
33
import { Scatter, scatter } from './scatter.js'
44
import { Text, text } from './text.js'
5+
import { Annotation, annotation } from './annotation.js'
56
import { Attr, Mark } from './mark.js'
67

7-
export { Polyline, polyline, Scatter, scatter, Interval, interval, Text, text, Attr, Mark }
8+
export { Polyline, polyline, Scatter, scatter, Interval, interval, Text, text, Annotation, annotation, Attr, Mark }

src/helpers/annotations.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)