Я пытаюсь использовать chartjs-datalabels-plugin для отображения значений на диаграмме. но я не могу заставить его работать на jsfiddle
Вот мой код
<div class = "graph">
<div class = "chart-legend">
</div>
<div class = "chart">
<canvas id = "myChart" height = 60></canvas>
</div>
</div>
Я импортирую chartjs-datalabels-plugin в jsfiddle, а затем в опциях устанавливаю для параметра отображения значение true. но на диаграмме значение не отображается.
var ctx = $('#myChart');
ctx.height(100);
var myChart = new Chart(ctx, {
....
maintainAspectRatio: false,
options: {
plugins: {
datalabels: {
color: 'black',
display: true,
font: {
weight: 'bold'
},
formatter: Math.round
}
},
legend: {
display: false
},
maintainAspectRatio: false,
scales: {
yAxes: [{
stacked: true,
gridLines: {
display: false,
drawBorder: false,
},
ticks: {
beginAtZero: true,
},
barThickness: 9
}],
xAxes: [{
stacked: true,
gridLines: {
display: false,
drawBorder: false,
},
ticks: {
beginAtZero: true,
suggestedMax: 100,
display: false
},
barThickness: 9
}]
}
}
});



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Плагин Datalabels требует Chart.js 2.7.0 или новее.
В своей скрипке вы использовали версию 2.4.0.
var ctx = $('#myChart');
ctx.height(100);
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
height: 200,
labels: ["Red"],
datasets: [{
label: '# of Votes',
data: [12],
backgroundColor: [
'#F4B266',
'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)'
],
borderWidth: 1
},
{
label: '# of Votes',
data: [18],
backgroundColor: [
'#AEB7B2',
'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)'
],
borderWidth: 1
}
]
},
maintainAspectRatio: false,
options: {
plugins: {
datalabels: {
color: 'black',
display: function(context) {
return context.dataset.data[context.dataIndex] > 15;
},
font: {
weight: 'bold'
},
formatter: Math.round
}
},
legend: {
display: false
},
maintainAspectRatio: false,
scales: {
yAxes: [{
stacked: true,
gridLines: {
display: false,
drawBorder: false,
},
ticks: {
beginAtZero: true,
},
barThickness: 9
}],
xAxes: [{
stacked: true,
gridLines: {
display: false,
drawBorder: false,
},
ticks: {
beginAtZero: true,
suggestedMax: 100,
display: false
},
barThickness: 9
}]
}
}
});<script src = "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<script src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-plugin-datalabels.min.js"></script>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "graph">
<div class = "chart-legend">
</div>
<div class = "chart">
<canvas id = "myChart" height = 60></canvas>
</div>
</div>