JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js"></script>
<body>
<div class="container">
<div class="chart-container">
<canvas id="myChart1"></canvas>
</div>
<div class="chart-container">
<canvas id="myChart2"></canvas>
</div>
</div>
</body>
<script>
window.chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(231,233,237)'
};
</script>
CSS
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.chart-container {
width: 500px;
margin-left: 40px;
margin-right: 40px;
margin-bottom: 40px;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
JavaScript
// report for chart.js v2.4.0
// Wrong category size of Bar with min option.
//from Issues #3506 (duplicate of #3491 and #2870)
//---------
Chart.plugins.register({
beforeDatasetsDraw: function(chartInstance) {
var ctx = chartInstance.chart.ctx;
var chartArea = chartInstance.chartArea;
ctx.save();
ctx.beginPath();
ctx.rect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
ctx.clip();
},
afterDatasetsDraw: function(chartInstance) {
chartInstance.chart.ctx.restore();
},
});
//---------
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My Second dataset",
type: "line",
backgroundColor: window.chartColors.red,
borderColor: window.chartColors.red,
data: [10, 30, 39, 20, 25, 34, 1],
fill: false,
}, {
label: "My First dataset",
type: "bar",
backgroundColor: window.chartColors.orange,
borderColor: window.chartColors.orange,
data: [10, 30, 39, 20, 25, 34, 1],
}]
};
//---------
// OK
//---------
var ctx1 = document.getElementById("myChart1");
var config1 = {
type: "bar",
data: data,
options: {
title: {
display: true,
text: "No min option"
},
scales: {
xAxes: [{
categoryPercentage: 1,
barPercentage: 1,
}]
}
}
};
var myLineChart1 = new Chart(ctx1, config1);
//---------
// wrong category size of Bar with min option.
//---------
var ctx2 = document.getElementById("myChart2");
var config2 = {
type: "bar",
data: data,
options: {
title: {
display: true,
text: "Wrong category size of Bar with min option."
},
scales: {
xAxes: [{
ticks: {
min: "March", // <<<<<<<<<<<<<<
//max also
},
categoryPercentage: 1,
barPercentage: 1,
}]
}
}
};
var myLineChart2 = new Chart(ctx2, config2);