Thanks @AIMPED. This works: when we draw a second line, it erases both the previous one and the current one: live demo.
<script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script>
<div id="myDiv"></div>
<script>
var data = [{x: [1, 2, 3, 4], y: [10, 15, 13, 17], mode: 'markers'}];
Plotly.newPlot('myDiv', data, {}, {modeBarButtons: [["drawline"]]});
var myDiv = document.getElementById('myDiv');
myDiv.on('plotly_relayout', (event) => {
if ('shapes' in event) {
if (event["shapes"].length > 1) {
console.log(event["shapes"].slice(-1));
Plotly.relayout(myDiv, { shapes: [] });
}
}
});
</script>
Would you know how to remove the first line when we draw a second line? It would be a better UX: we only keep one line. When drawing a second line, we remove the previous one.