Skip to content

Commit ea65dcf

Browse files
committed
Refactor annotation API to use label to be consistent with axis
1 parent 834e759 commit ea65dcf

File tree

5 files changed

+18
-33
lines changed

5 files changed

+18
-33
lines changed

jest.config.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const config = {
1414
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '<rootDir>/{src,test}/**/?(*.)+(spec|test).[jt]s?(x)'],
1515
transformIgnorePatterns: ['<rootDir>/node_modules/(?!d3-\\.*|internmap)'],
1616
testEnvironment: 'node',
17+
globals: {
18+
__COMMIT_HASH__: 'unknown'
19+
},
1720
extensionsToTreatAsEsm: ['.ts']
1821
}
1922

src/helpers/annotations.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,25 @@ export default function annotations(options: { owner: Chart }) {
5454
.merge(enter)
5555
.selectAll('text')
5656
.data(function (d) {
57-
return [
58-
{
59-
text: d.text || '',
60-
hasX: 'x' in d
61-
}
62-
]
57+
return [{ text: d.label || '' }]
6358
})
6459
text
6560
.enter()
6661
.append('text')
6762
.attr('y', function (d) {
68-
return d.hasX ? 3 : 0
63+
return 'x' in d ? 3 : 0
6964
})
7065
.attr('x', function (d) {
71-
return d.hasX ? 0 : 3
66+
return 'x' in d ? 0 : 3
7267
})
7368
.attr('dy', function (d) {
74-
return d.hasX ? 5 : -5
69+
return 'x' in d ? 5 : -5
7570
})
7671
.attr('text-anchor', function (d) {
77-
return d.hasX ? 'end' : ''
72+
return 'x' in d ? 'end' : ''
7873
})
7974
.attr('transform', function (d) {
80-
return d.hasX ? 'rotate(-90)' : ''
75+
return 'x' in d ? 'rotate(-90)' : ''
8176
})
8277
.text(function (d) {
8378
return d.text

src/types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,15 @@ export interface AbstractFunctionDatum {
151151
fnType?: 'linear' | 'parametric' | 'implicit' | 'polar' | 'points' | 'vector'
152152

153153
/**
154-
* The sampler to take samples from `range`, available values are `interval|builtIn`
154+
* The sampler to take samples from `range`, available values are `builtIn|interval|asyncInterval`
155155
*
156156
* - **NOTE: `builtIn` should only be used when `graphType` is `polyline|scatter`**
157157
*/
158158
sampler?: 'interval' | 'builtIn' | 'asyncInterval'
159159

160160
/**
161-
* The number of values to be taken from `range` to evaluate the function, note that if interval-arithmetic is used the function
161+
* The number of values to be taken from `range` to evaluate the function,
162+
* note that if interval-arithmetic is used the function
162163
* will be evaluated with intervals instead of single values
163164
*/
164165
nSamples?: number
@@ -316,7 +317,7 @@ export interface TextDatum {
316317
text: string
317318

318319
/**
319-
* An array of 2-number array for the position of the text when `fnType: 'text'`
320+
* An array of 2-number array for the position of the text when `graphType: 'text'`
320321
*/
321322
location?: [number, number]
322323
}
@@ -345,9 +346,9 @@ export interface FunctionPlotAnnotation {
345346
y?: number
346347

347348
/**
348-
* The text displayed next to the line
349+
* The label displayed next to the line
349350
*/
350-
text?: string
351+
label?: string
351352
}
352353

353354
export interface FunctionPlotOptions {
-1.1 KB
Loading

test/e2e/snippets.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ const snippets = [
131131
x: -1
132132
}, {
133133
x: 1,
134-
text: 'x = 1'
134+
label: 'x = 1'
135135
}, {
136136
y: 2,
137-
text: 'y = 2'
137+
label: 'y = 2'
138138
}]
139139
})
140140
}
@@ -391,21 +391,7 @@ const snippets = [
391391
fn: function () {
392392
const instance = functionPlot({
393393
target: '#playground',
394-
data: [
395-
{
396-
graphType: 'text',
397-
location: [1, 1],
398-
text: 'hello world'
399-
} as TextFunction,
400-
{
401-
graphType: 'text',
402-
location: [-1, -1],
403-
text: 'foo bar',
404-
attr: {
405-
'text-anchor': 'end'
406-
}
407-
} as TextFunction
408-
]
394+
data: [{ fn: 'x^2' } as LinearFunction]
409395
})
410396
instance.destroy()
411397
}

0 commit comments

Comments
 (0)