2021-04-02 14:04:39 +02:00
|
|
|
# Floating Bars
|
|
|
|
|
2022-05-21 17:02:13 +02:00
|
|
|
Using `[number, number][]` as the type for `data` to define the beginning and end value for each bar. This is instead of having every bar start at 0.
|
|
|
|
|
2021-04-02 14:04:39 +02:00
|
|
|
```js chart-editor
|
|
|
|
// <block:actions:2>
|
|
|
|
const actions = [
|
|
|
|
{
|
|
|
|
name: 'Randomize',
|
|
|
|
handler(chart) {
|
|
|
|
chart.data.datasets.forEach(dataset => {
|
|
|
|
dataset.data = chart.data.labels.map(() => {
|
|
|
|
return [Utils.rand(-100, 100), Utils.rand(-100, 100)];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
chart.update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|
|
|
|
// </block:actions>
|
|
|
|
|
|
|
|
// <block:setup:1>
|
|
|
|
const DATA_COUNT = 7;
|
|
|
|
const NUMBER_CFG = {count: DATA_COUNT, min: -100, max: 100};
|
|
|
|
|
|
|
|
const labels = Utils.months({count: 7});
|
|
|
|
const data = {
|
|
|
|
labels: labels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: 'Dataset 1',
|
|
|
|
data: labels.map(() => {
|
|
|
|
return [Utils.rand(-100, 100), Utils.rand(-100, 100)];
|
|
|
|
}),
|
|
|
|
backgroundColor: Utils.CHART_COLORS.red,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Dataset 2',
|
|
|
|
data: labels.map(() => {
|
|
|
|
return [Utils.rand(-100, 100), Utils.rand(-100, 100)];
|
|
|
|
}),
|
|
|
|
backgroundColor: Utils.CHART_COLORS.blue,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
};
|
|
|
|
// </block:setup>
|
|
|
|
|
|
|
|
// <block:config:0>
|
|
|
|
const config = {
|
|
|
|
type: 'bar',
|
|
|
|
data: data,
|
|
|
|
options: {
|
|
|
|
responsive: true,
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
position: 'top',
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Chart.js Floating Bar Chart'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// </block:config>
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
actions: actions,
|
|
|
|
config: config,
|
|
|
|
};
|
|
|
|
```
|
2022-05-21 17:02:13 +02:00
|
|
|
## Docs
|
|
|
|
* [Bar](../../charts/bar.html)
|
|
|
|
* [Data structures (`labels`)](../../general/data-structures.html)
|