I have a ChartJS that plots multiple line chart together with confidence interval areas. The problem is that the first grouped line chart with CI is buggy so that when I toggle it, other groups have their areas hidden as well. This is pretty difficult to explain in words so I recorded it and put it up here on youtube.
Code I have so far is as such:
let predictData = [21,22,23,24,25,26,27,28];
let lowerCI80 = [20,21,22,23,24,25,26,27];
let upperCI80 = [22,23,24,25,26,27,28,29];
let lowerCI90 = [18,19,20,21,22,23,24,25];
let upperCI90 = [23,24,25,26,27,28,29,30];
const data = {
labels: [1,2,3,4,5,6,7,8],
datasets: [
{
label: "Lower_80%CI",
type: "line",
backgroundColor: "rgb(75, 192, 255, 0.3)",
borderColor: "transparent",
pointRadius: 0,
fill: 0,
tension: 0,
data: low80Data,
yAxisID: "y",
xAxisID: "x",
},
{
label: "80% CI",
type: "line",
backgroundColor: "rgb(75, 192, 255)",
borderColor: "rgb(75, 192, 255)",
hoverBorderColor: "rgb(75, 192, 255)",
pointRadius: 0,
fill: false,
tension: 0,
data: predictData,
yAxisID: "y",
xAxisID: "x",
},
{
label: "Upper_80%CI",
type: "line",
backgroundColor: "rgb(75, 192, 255, 0.3)",
borderColor: "transparent",
pointRadius: 0,
fill: 0,
tension: 0,
data: up80Data,
yAxisID: "y",
xAxisID: "x",
},
{
label: "Lower_90%CI",
type: "line",
backgroundColor: "rgb(255, 75, 75, 0.3)",
borderColor: "transparent",
pointRadius: 0,
fill: 0,
tension: 0,
data: low90Data,
yAxisID: "y",
xAxisID: "x",
},
{
label: "90% CI",
type: "line",
backgroundColor: "rgb(255, 75, 75)",
borderColor: "rgb(255, 75, 75)",
hoverBorderColor: "rgb(255, 75, 75)",
pointRadius: 0,
fill: false,
tension: 0,
data: predictData,
yAxisID: "y",
xAxisID: "x",
},
{
label: "Upper_90%CI",
type: "line",
backgroundColor: "rgb(255, 75, 75, 0.3)",
borderColor: "transparent",
pointRadius: 0,
fill: 0,
tension: 0,
data: up90Data,
yAxisID: "y",
xAxisID: "x",
},
],
};
const options = {
plugins: {
title: {
display: false,
},
legend: {
display: true,
position: "top",
labels: {
filter: function (item, chart) {
return !item.text.includes("_");
},
},
onClick: function (e, legendItem) {
// need to hide index -1 and index +1
var index = legendItem.datasetIndex;
var ci = this.chart;
var alreadyHidden =
ci.getDatasetMeta(index).hidden === null
? false
: ci.getDatasetMeta(index).hidden;
var meta_lo = ci.getDatasetMeta(index - 1);
var meta = ci.getDatasetMeta(index);
var meta_hi = ci.getDatasetMeta(index + 1);
if (!alreadyHidden) {
meta_lo.hidden = true;
meta.hidden = true;
meta_hi.hidden = true;
} else {
meta_lo.hidden = null;
meta.hidden = null;
meta_hi.hidden = null;
}
ci.update();
},
},
tooltip: {
mode: "index",
intersect: false,
},
},
hover: {
mode: "nearest",
intersect: false,
},
scales: {
xAxes: [
{
ticks: {
stepSize: 7, // This is not working as well(?)
fontColor: "white",
},
},
],
},
};
return <Line data={data} options={options} />