Skip to content

Commit 5ad80cc

Browse files
committed
Update annotation text on selection update
1 parent a0e1d08 commit 5ad80cc

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/chart.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,13 @@ export class Chart extends EventEmitter.EventEmitter {
503503
.merge(canvas.enter)
504504
.attr('transform', 'translate(' + this.meta.margin.left + ',' + this.meta.margin.top + ')')
505505

506-
const content = (this.content = canvas
506+
const content = canvas
507507
.merge(canvas.enter)
508508
.selectAll(':scope > g.content')
509509
.data(function (d: FunctionPlotOptions) {
510510
return [d]
511-
}))
511+
})
512+
this.content = content
512513

513514
// g tag clipped to hold the data
514515
const contentEnter = content

src/helpers/annotations.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,46 @@ export default function annotations(options: { owner: Chart }) {
4141
return [[[xRange[0], 0], [xRange[1], 0]]]
4242
}
4343
})
44+
// enter
45+
const pathEnter = path.enter().append('path')
46+
// enter + update
4447
path
45-
.enter()
46-
.append('path')
48+
.merge(pathEnter)
4749
.attr('stroke', '#eee')
4850
.attr('d', line as any)
4951
path.exit().remove()
5052

51-
// enter + update
52-
// - text
53+
// join
5354
const text = selection
5455
.merge(enter)
5556
.selectAll('text')
56-
.data(function (d) {
57-
return [{ text: d.label || '' }]
58-
})
57+
.data((d) => [
58+
{
59+
label: d.label || '',
60+
// used to determine if x or y is set.
61+
xIsSet: 'x' in d
62+
}
63+
])
64+
// enter
65+
const textEnter = text.enter().append('text')
66+
// enter + update
5967
text
60-
.enter()
61-
.append('text')
68+
.merge(textEnter)
69+
.text((d) => d.label)
6270
.attr('y', function (d) {
63-
return 'x' in d ? 3 : 0
71+
return d.xIsSet ? 3 : 0
6472
})
6573
.attr('x', function (d) {
66-
return 'x' in d ? 0 : 3
74+
return d.xIsSet ? 0 : 3
6775
})
6876
.attr('dy', function (d) {
69-
return 'x' in d ? 5 : -5
77+
return d.xIsSet ? 5 : -5
7078
})
7179
.attr('text-anchor', function (d) {
72-
return 'x' in d ? 'end' : ''
80+
return d.xIsSet ? 'end' : ''
7381
})
7482
.attr('transform', function (d) {
75-
return 'x' in d ? 'rotate(-90)' : ''
76-
})
77-
.text(function (d) {
78-
return d.text
83+
return d.xIsSet ? 'rotate(-90)' : ''
7984
})
8085
text.exit().remove()
8186

0 commit comments

Comments
 (0)