JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-dragdata@1.1.3/dist/chartjs-plugin-dragdata.min.js"></script>
<canvas id="chartJSContainer" style="height: 100%; width: 100%;"></canvas>
JavaScript
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
pointHitRadius: 25
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1,
pointHitRadius: 25
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
},
dragData: true,
onDragStart: function(e, element) {
startingValue = window.test.data.datasets[element._datasetIndex].data[element._index]
},
onDrag: function(e, datasetIndex, index, value) {
// update the point left and right of clicked point
// if the first point is clicked, only update the point to the right
if (index === 0) {
window.test.data.datasets[datasetIndex].data[index + 1] = value
// if the last point is clicked, only update the point before
} else if (index === (window.test.data.datasets[datasetIndex].data.length - 1)) {
window.test.data.datasets[datasetIndex].data[index - 1] = value
} else {
// all other cases
window.test.data.datasets[datasetIndex].data[index - 1] = value
window.test.data.datasets[datasetIndex].data[index + 1] = value
}
window.test.update()
//console.log(datasetIndex, index, value)
},
onDragEnd: function(e, datasetIndex, index, value) {
console.log(datasetIndex, index, value)
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
window.test = new Chart(ctx, options);