score:1

Accepted answer

reason it didnt work because you tried to fill to a specific dataset by specifying that dataset index. this didnt work because that dataset was hidden so no fill anymore. so you either need to specify the correct datasets or use relative fills for dataset. more info can be found here.

reason your stepsize didnt work is because your scale config was in v2 and in v3 the way scales are condfigured have changed. also by default the x axis is a category axis which doesnt have the stepsize property.

for all changes between v2 and v3 you can read the migration guide

let predictdata = [21, 22, 23, 24, 25, 26, 27, 28];
let low80data = [20, 21, 22, 23, 24, 25, 26, 27];
let up80data = [22, 23, 24, 25, 26, 27, 28, 29];
let low90data = [18, 19, 20, 21, 22, 23, 24, 25];
let up90data = [23, 24, 25, 26, 27, 28, 29, 30];

const data = {
  labels: [1, 2, 3, 4, 5, 6, 7, 8],
  datasets: [{
      label: "lower_80%ci",
      backgroundcolor: "rgb(75, 192, 255, 0.3)",
      bordercolor: "transparent",
      pointradius: 0,
      data: low80data,
    },
    {
      label: "80% ci",
      backgroundcolor: "rgb(75, 192, 255)",
      bordercolor: "rgb(75, 192, 255)",
      hoverbordercolor: "rgb(75, 192, 255)",
      pointradius: 0,
      data: predictdata,
    },
    {
      label: "upper_80%ci",
      backgroundcolor: "rgb(75, 192, 255, 0.3)",
      bordercolor: "transparent",
      pointradius: 0,
      fill: '-2',
      data: up80data,
    },
    {
      label: "lower_90%ci",
      backgroundcolor: "rgb(255, 75, 75, 0.3)",
      bordercolor: "transparent",
      pointradius: 0,
      fill: '+2',
      data: low90data,
    },
    {
      label: "90% ci",
      backgroundcolor: "rgb(255, 75, 75)",
      bordercolor: "rgb(255, 75, 75)",
      hoverbordercolor: "rgb(255, 75, 75)",
      pointradius: 0,
      data: predictdata,
    },
    {
      label: "upper_90%ci",
      backgroundcolor: "rgb(255, 75, 75, 0.3)",
      bordercolor: "transparent",
      pointradius: 0,
      data: up90data,
    },
  ],
};

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: {
    x: {
      type: 'linear',
      ticks: {
        stepsize: 7, // this is not working as well(?)
        color: "white",
      },
    },

  },
};

new chart('chartjscontainer', {
  type: 'line',
  data: data,
  options: options
})
<body>
  <canvas id="chartjscontainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.6.2/chart.js"></script>
</body>


Related Query