Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample about interaction options #721

Merged
merged 3 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ module.exports = {
'charts/line',
],
},
'interaction',
'utils',
]
}
Expand Down
184 changes: 184 additions & 0 deletions docs/samples/interaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# Interaction

```js chart-editor
// <block:setup:3>
const AXIS = ['xy', 'x', 'y'];
const MODE = ['nearest', 'point', 'x', 'y'];

let axisIndex = 0;
let modeIndex = 0;

Utils.srand(8);

const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
type: 'line',
label: 'Dataset 1',
borderColor: 'rgb(54, 162, 235)',
borderWidth: 2,
fill: false,
data: Utils.numbers({count: 7, min: 0, max: 100}),
}]
};
// </block:setup>

// <block:printer:4>
const printer = {
id: 'printer',
afterDraw(chart) {
const options = chart.options.plugins.annotation.interaction;
const mode = options.mode || 'nearest';
const axis = options.axis || 'xy';
const intersect = !!options.intersect;
const ctx = chart.ctx;
ctx.save();
ctx.font = '14px monospace';
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillText('Mode: ' + mode + ', axis: ' + axis + ', intersect: ' + intersect, chart.chartArea.left, chart.canvas.offsetHeight - 16);
ctx.restore();
}
};
// </block:printer>

// <block:annotation1:1>
const annotation1 = {
type: 'box',
backgroundColor: 'rgba(255, 245, 157, 0.2)',
borderColor: 'rgb(255, 245, 157)',
borderWidth: 2,
enter: function({element}) {
console.log(element.options.label.content + ' entered');
},
click: function({element}) {
console.log(element.options.label.content + ' clicked');
},
leave: function({element}) {
console.log(element.options.label.content + ' left');
},
label: {
display: true,
content: 'Outer box annotation',
position: {
y: 'start'
},
font: {
size: 14
}
},
xMax: 'June',
xMin: 'February',
xScaleID: 'x',
yMax: 90,
yMin: 10,
yScaleID: 'y'
};
// </block:annotation1>

// <block:annotation2:2>
const annotation2 = {
type: 'box',
backgroundColor: 'rgba(165, 214, 167, 0.2)',
borderColor: 'rgb(165, 214, 167)',
borderWidth: 2,
enter: function({element}) {
console.log(element.options.label.content + ' entered');
},
click: function({element}) {
console.log(element.options.label.content + ' clicked');
},
leave: function({element}) {
console.log(element.options.label.content + ' left');
},
label: {
display: true,
content: 'Inner box annotation',
position: {
y: 'start'
},
font: {
size: 14
}
},
xMax: 'May',
xMin: 'March',
xScaleID: 'x',
yMax: 75,
yMin: 25,
yScaleID: 'y'
};
// </block:annotation2>

/* <block:config:0> */
const config = {
type: 'line',
plugins: [printer],
stockiNail marked this conversation as resolved.
Show resolved Hide resolved
data,
options: {
layout: {
padding: {
bottom: 24
}
},
scales: {
y: {
beginAtZero: true,
min: 0,
max: 100
}
},
plugins: {
annotation: {
annotations: {
outer: annotation1,
inner: annotation2
}
}
}
}
};
/* </block:config> */

const actions = [
{
name: 'Change mode',
handler: function(chart) {
const options = chart.options.plugins.annotation.interaction;
modeIndex++;
if (modeIndex > MODE.length) {
modeIndex = 0;
}
options.mode = MODE[modeIndex];
chart.update();
}
},
{
name: 'Change axis',
handler: function(chart) {
const options = chart.options.plugins.annotation.interaction;
axisIndex++;
if (axisIndex > AXIS.length) {
axisIndex = 0;
}
options.axis = AXIS[axisIndex];
chart.update();
}
},
{
name: 'Toogle intersect',
handler: function(chart) {
const options = chart.options.plugins.annotation.interaction;
options.intersect = !options.intersect;
chart.update();
}
},
];

module.exports = {
actions: actions,
config: config,
output: 'console.log output is shown here, click one of the annotations'
};
```