Skip to content

Commit

Permalink
add sample plugin for doughnut empty state
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeLenaleee committed Feb 6, 2022
1 parent aebbb5a commit d45d49b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ module.exports = {
children: [
'plugins/chart-area-border',
'plugins/quadrants',
'plugins/doughnut-empty-state',
]
},
'utils'
Expand Down
77 changes: 77 additions & 0 deletions docs/samples/plugins/doughnut-empty-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Doughnut Empty State

```js chart-editor
// <block:data:2>
const data = {
labels: [],
datasets: [
{
label: 'Dataset 1',
data: []
}
]
};
// </block:data>

// <block:plugin:1>
const plugin = {
id: 'emptyDoughnut',
afterDraw(chart, args, options) {
const {datasets} = chart.data;
const {color, width, radiusDecrease} = options;
let hasData = false;

for (let i = 0; i < datasets.length; i += 1) {
const dataset = datasets[i];
hasData |= dataset.data.length > 0;
}

if (!hasData) {
const {chartArea: {left, top, right, bottom}, ctx} = chart;
const centerX = (left + right) / 2;
const centerY = (top + bottom) / 2;
const r = Math.min(right - left, bottom - top) / 2;

ctx.beginPath();
ctx.lineWidth = width || 2;
ctx.strokeStyle = color || 'rgba(255, 128, 0, 0.5)';
ctx.arc(centerX, centerY, (r - radiusDecrease || 0), 0, 2 * Math.PI);
ctx.stroke();
}
}
};
// </block:plugin>

// <block:config:0>
const config = {
type: 'doughnut',
data: data,
options: {
plugins: {
emptyDoughnut: {
color: 'rgba(255, 128, 0, 0.5)',
width: 2,
radiusDecrease: 20
}
}
},
plugins: [plugin]
};
// </block:config>

const actions = [
{
name: 'Randomize',
handler(chart) {
chart.data.datasets.forEach(dataset => {
dataset.data = Utils.points(NUMBER_CFG);
});
chart.update();
}
},
];

module.exports = {
actions,
config,
};

0 comments on commit d45d49b

Please sign in to comment.