score:-1

use "overflow-x:scroll;" instead of "overflow:scroll;"

score:4

a

chart.js docs: "the following examples do not work:"

= any canvas sizes like (<canvas style="width: 100px;..") + position absolute and other tricks will not work her.

b

chart.js uses its parent container to update the canvas render and display sizes. however, this method requires the container to be relatively positioned and dedicated to the chart canvas only. responsiveness can then be achieved by setting relative values for the container size https://www.chartjs.org/docs/latest/general/responsive.html#important-note

<div class="chart-container" style="position: relative; height:40vh; width:80vw">
    <canvas id="chart"></canvas>
</div>

= set any size you want for chart-container.

c

  • maintainaspectratio - by default true (maintain the original canvas aspect ratio (width / height) when resizing).

  • responive - by deafult true (resizes the chart canvas when its container does)

a+b+c

i do not know why other stackoverflow answers give such "complex" solutions. the overflow-x her works like any other block-level element (put 800px w image inside 600px w div = 200px overflow-x).

"hello world" example

specific for overflow add extra wrapper to chart-container:

<div style="overflow-x: scroll">
  <div class="chart-container" style="position: relative;  width:900px;">
    <canvas id="chart"></canvas>
  </div>
</div>

when the screen width will be smaller than 900px you get horizontal scroll (on mobile for example):

enter image description here

** use css breakpoints to resize the container on mobile if you want.

/* data */
var data = {
  labels: ["africa", "asia", "europe", "america"],
  datasets: [{
    /* data */
    label: "data label",
    backgroundcolor: ["red", "#8e5ea2","#3cba9f", '#1d49b8'],
    data: [5.0,6.7,7.5, 8.6, 3.3, 4.4, 4.5]
  }]
};

var options = {
  responsive: true,
  maintainaspectratio: true,
  title: {
    text: 'hello',
    display: true
  },
  scales: {
    xaxes: [{
      stacked: false,
      ticks: {

      },
    }],
    yaxes: [{
      stacked: true,
      ticks: {

      }
    }]
  }
};

var mychart = new chart(document.getelementbyid("chart"), {
  type: 'bar',
  data: data,
  options: options
});

document.getelementbyid('submitchange').addeventlistener('click', function() {
  console.log("before color: " + mychart.data.datasets[0].backgroundcolor[0]);
  mychart.data.datasets[0].backgroundcolor[0] = "green";
  mychart.update();
});
div{
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>
<div style="overflow-x: scroll">
  <div class="chart-container" style="position: relative;  width:900px;">
    <canvas id="chart"></canvas>
  </div>
</div>


Related Query

More Query from same tag