forked from chartjs/chartjs-plugin-annotation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable callout in the label of line annotation (chartjs#740)
* Enable callout in the label of line annotation * adds test cases * adds test case with oblique labels and auto rotation * solves CC issue of duplicate code * Add element diagrams to the annotation types guide * revert wrong merging
- Loading branch information
1 parent
236111e
commit 1cfbfc6
Showing
12 changed files
with
560 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
# Callout | ||
|
||
```js chart-editor | ||
// <block:setup:5> | ||
const DATA_COUNT = 16; | ||
const MIN = 20; | ||
const MAX = 100; | ||
|
||
Utils.srand(8); | ||
|
||
const labels = []; | ||
for (let i = 0; i < DATA_COUNT; ++i) { | ||
labels.push('' + i); | ||
} | ||
|
||
const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX}; | ||
|
||
const data = { | ||
labels: labels, | ||
datasets: [{ | ||
data: Utils.numbers(numberCfg) | ||
}] | ||
}; | ||
// </block:setup> | ||
|
||
// <block:annotation1:1> | ||
const annotation1 = { | ||
type: 'line', | ||
borderColor: 'rgb(100, 149, 237)', | ||
borderDash: [6, 6], | ||
borderDashOffset: 0, | ||
borderWidth: 3, | ||
label: { | ||
display: true, | ||
backgroundColor: 'rgb(100, 149, 237)', | ||
content: (ctx) => 'Average: ' + average(ctx).toFixed(2) | ||
}, | ||
scaleID: 'y', | ||
value: (ctx) => average(ctx) | ||
}; | ||
// </block:annotation1> | ||
|
||
// <block:annotation2:2> | ||
const annotation2 = { | ||
type: 'line', | ||
borderColor: 'rgba(102, 102, 102, 0.5)', | ||
borderDash: [6, 6], | ||
borderDashOffset: 0, | ||
borderWidth: 3, | ||
label: { | ||
display: true, | ||
backgroundColor: 'rgba(102, 102, 102, 0.5)', | ||
borderWidth: 1, | ||
borderColor: 'rgba(102, 102, 102, 0.5)', | ||
callout: { | ||
display: true, | ||
borderColor: 'rgba(102, 102, 102, 0.5)', | ||
borderDash: [6, 6], | ||
borderWidth: 2, | ||
margin: 0 | ||
}, | ||
color: 'black', | ||
content: (ctx) => (average(ctx) + standardDeviation(ctx)).toFixed(2), | ||
position: 'start', | ||
xAdjust: 100, | ||
yAdjust: -50 | ||
}, | ||
scaleID: 'y', | ||
value: (ctx) => average(ctx) + standardDeviation(ctx) | ||
}; | ||
// </block:annotation2> | ||
|
||
// <block:annotation3:3> | ||
const annotation3 = { | ||
type: 'line', | ||
borderColor: 'rgba(102, 102, 102, 0.5)', | ||
borderDash: [6, 6], | ||
borderDashOffset: 0, | ||
borderWidth: 3, | ||
label: { | ||
display: true, | ||
backgroundColor: 'rgba(102, 102, 102, 0.5)', | ||
borderWidth: 1, | ||
borderColor: 'rgba(102, 102, 102, 0.5)', | ||
callout: { | ||
display: true, | ||
borderColor: 'rgba(102, 102, 102, 0.5)', | ||
borderDash: [6, 6], | ||
borderWidth: 2, | ||
margin: 0 | ||
}, | ||
color: 'black', | ||
content: (ctx) => (average(ctx) - standardDeviation(ctx)).toFixed(2), | ||
position: 'end', | ||
xAdjust: -100, | ||
yAdjust: 50 | ||
}, | ||
scaleID: 'y', | ||
value: (ctx) => average(ctx) - standardDeviation(ctx) | ||
}; | ||
// </block:annotation3> | ||
|
||
/* <block:config:0> */ | ||
const config = { | ||
type: 'line', | ||
data, | ||
options: { | ||
scale: { | ||
y: { | ||
beginAtZero: true, | ||
max: 120, | ||
min: 0 | ||
} | ||
}, | ||
plugins: { | ||
annotation: { | ||
annotations: { | ||
annotation1, | ||
annotation2, | ||
annotation3 | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
/* </block:config> */ | ||
|
||
// <block:utils:4> | ||
function average(ctx) { | ||
const values = ctx.chart.data.datasets[0].data; | ||
return values.reduce((a, b) => a + b, 0) / values.length; | ||
} | ||
|
||
function standardDeviation(ctx) { | ||
const values = ctx.chart.data.datasets[0].data; | ||
const n = values.length; | ||
const mean = average(ctx); | ||
return Math.sqrt(values.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n); | ||
} | ||
|
||
// </block:utils> | ||
|
||
const actions = [ | ||
{ | ||
name: 'Randomize', | ||
handler: function(chart) { | ||
chart.data.datasets.forEach(function(dataset, i) { | ||
dataset.data = dataset.data.map(() => Utils.rand(MIN, MAX)); | ||
}); | ||
chart.update(); | ||
} | ||
}, | ||
{ | ||
name: 'Add data', | ||
handler: function(chart) { | ||
chart.data.labels.push(chart.data.labels.length); | ||
chart.data.datasets.forEach(function(dataset, i) { | ||
dataset.data.push(Utils.rand(MIN, MAX)); | ||
}); | ||
chart.update(); | ||
} | ||
}, | ||
{ | ||
name: 'Remove data', | ||
handler: function(chart) { | ||
chart.data.labels.shift(); | ||
chart.data.datasets.forEach(function(dataset, i) { | ||
dataset.data.shift(); | ||
}); | ||
chart.update(); | ||
} | ||
} | ||
]; | ||
|
||
module.exports = { | ||
actions: actions, | ||
config: config, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module.exports = { | ||
config: { | ||
type: 'scatter', | ||
options: { | ||
scales: { | ||
x: { | ||
display: false, | ||
min: 0, | ||
max: 10 | ||
}, | ||
y: { | ||
display: false, | ||
min: 0, | ||
max: 10 | ||
} | ||
}, | ||
plugins: { | ||
annotation: { | ||
annotations: { | ||
line1: { | ||
type: 'line', | ||
scaleID: 'y', | ||
value: 8, | ||
borderColor: 'black', | ||
borderWidth: 5, | ||
label: { | ||
display: true, | ||
backgroundColor: '#f5f5f5', | ||
borderColor: 'black', | ||
borderRadius: 0, | ||
borderWidth: 1, | ||
content: 'yAdjust: -40, callout position: auto', | ||
yAdjust: -40, | ||
callout: { | ||
display: true | ||
} | ||
} | ||
}, | ||
line2: { | ||
type: 'line', | ||
scaleID: 'y', | ||
value: 6, | ||
borderColor: 'black', | ||
borderWidth: 5, | ||
label: { | ||
display: true, | ||
backgroundColor: '#f5f5f5', | ||
borderColor: 'black', | ||
borderRadius: 0, | ||
borderWidth: 1, | ||
content: ['yAdjust: -40, xAdjust: -150', 'callout position: auto'], | ||
yAdjust: -40, | ||
xAdjust: -150, | ||
callout: { | ||
display: true | ||
} | ||
} | ||
}, | ||
line3: { | ||
type: 'line', | ||
scaleID: 'y', | ||
value: 4, | ||
borderColor: 'black', | ||
borderWidth: 5, | ||
label: { | ||
display: true, | ||
backgroundColor: '#f5f5f5', | ||
borderColor: 'black', | ||
borderRadius: 0, | ||
borderWidth: 1, | ||
content: ['yAdjust: 40, xAdjust: 150', 'callout position: auto'], | ||
yAdjust: 40, | ||
xAdjust: 150, | ||
callout: { | ||
display: true | ||
} | ||
} | ||
}, | ||
line4: { | ||
type: 'line', | ||
scaleID: 'y', | ||
value: 2, | ||
borderColor: 'black', | ||
borderWidth: 5, | ||
label: { | ||
display: true, | ||
backgroundColor: '#f5f5f5', | ||
borderColor: 'black', | ||
borderRadius: 0, | ||
borderWidth: 1, | ||
content: 'yAdjust: 40, callout position: auto', | ||
yAdjust: 40, | ||
callout: { | ||
display: true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
options: { | ||
spriteText: true | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.