1👍
I am unable to reproduce the issue. It seems there is something in your code/environment that is preventing it all from coming together.
See working demo below
Moderators
I am posting my code here for OP to look at it and see if an error can be spotted between my working version and theirs.
Once OP confirms the issue, I can remove this answer if needed.
$(function() {
var endpoint = 'https://jsonplaceholder.typicode.com/todos';
var dataObj = {
"labels4": [
"108951",
"115510",
"118301",
"118360",
"118740",
"118908",
"128245",
"128715",
"269562",
"269945",
"282020",
"284600",
"289217",
"289225",
"289226"
],
"default4": [
1633.2774193548387,
1015.5128205128206,
671.0392156862745,
2421.108108108108,
930.4,
958.5,
521.1617647058823,
2142.0,
9040.0,
625.3037974683544,
516.5,
2045.4548387096775,
413.06666666666666,
363.06451612903226,
300.0
]
};
$.ajax({
method: "GET",
url: endpoint,
success: function(data) {
console.log('data received');
// now let's use the mocked data above instead
labels4 = dataObj.labels4
defaultData4 = dataObj.default4
setChart()
},
error: function(error_data) {
console.log(error_data)
}
});
function setChart() {
var ctx4 = document.getElementById('myChart4').getContext('2d');
var myChart4 = new Chart(ctx4, {
type: 'bar',
data: {
labels: labels4,
datasets: [{
label: 'top sellers',
data: defaultData4,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}],
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
}
})
}
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div class='row'>
<div class="col-sm-12">
<h1>Inventory Management Dashboard</h1>
<canvas id="myChart4" width="100" height="20"></canvas>
</div>
</div>
Source:stackexchange.com