Skip to content

Commit

Permalink
Refactor annotation API to use label to be consistent with axis
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Nov 2, 2024
1 parent 834e759 commit 7f75d58
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 33 deletions.
3 changes: 3 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const config = {
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '<rootDir>/{src,test}/**/?(*.)+(spec|test).[jt]s?(x)'],
transformIgnorePatterns: ['<rootDir>/node_modules/(?!d3-\\.*|internmap)'],
testEnvironment: 'node',
globals: {
__COMMIT_HASH__: 'unknown'
},
extensionsToTreatAsEsm: ['.ts']
}

Expand Down
17 changes: 6 additions & 11 deletions src/helpers/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,25 @@ export default function annotations(options: { owner: Chart }) {
.merge(enter)
.selectAll('text')
.data(function (d) {
return [
{
text: d.text || '',
hasX: 'x' in d
}
]
return [{ text: d.label || '' }]
})
text
.enter()
.append('text')
.attr('y', function (d) {
return d.hasX ? 3 : 0
return 'x' in d ? 3 : 0
})
.attr('x', function (d) {
return d.hasX ? 0 : 3
return 'x' in d ? 0 : 3
})
.attr('dy', function (d) {
return d.hasX ? 5 : -5
return 'x' in d ? 5 : -5
})
.attr('text-anchor', function (d) {
return d.hasX ? 'end' : ''
return 'x' in d ? 'end' : ''
})
.attr('transform', function (d) {
return d.hasX ? 'rotate(-90)' : ''
return 'x' in d ? 'rotate(-90)' : ''
})
.text(function (d) {
return d.text
Expand Down
11 changes: 6 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ export interface AbstractFunctionDatum {
fnType?: 'linear' | 'parametric' | 'implicit' | 'polar' | 'points' | 'vector'

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

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

/**
* An array of 2-number array for the position of the text when `fnType: 'text'`
* An array of 2-number array for the position of the text when `graphType: 'text'`
*/
location?: [number, number]
}
Expand Down Expand Up @@ -345,9 +346,9 @@ export interface FunctionPlotAnnotation {
y?: number

/**
* The text displayed next to the line
* The label displayed next to the line
*/
text?: string
label?: string
}

export interface FunctionPlotOptions {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 3 additions & 17 deletions test/e2e/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ const snippets = [
x: -1
}, {
x: 1,
text: 'x = 1'
label: 'x = 1'
}, {
y: 2,
text: 'y = 2'
label: 'y = 2'
}]
})
}
Expand Down Expand Up @@ -391,21 +391,7 @@ const snippets = [
fn: function () {
const instance = functionPlot({
target: '#playground',
data: [
{
graphType: 'text',
location: [1, 1],
text: 'hello world'
} as TextFunction,
{
graphType: 'text',
location: [-1, -1],
text: 'foo bar',
attr: {
'text-anchor': 'end'
}
} as TextFunction
]
data: [{ fn: 'x^2' } as LinearFunction]
})
instance.destroy()
}
Expand Down

0 comments on commit 7f75d58

Please sign in to comment.