I’m writing my js code to implement the customized feature to copy the data for a selected (by click the point) plotted point to the clipboard. For example, if the selected point reflect the data (x=1,y=3), a string such as ‘x=1, y=3’ is automatically copied to the clipboard.
I read https://plot.ly/javascript/plotlyjs-events/ and understand that calling myPlot.on will register a callback for a specific plotly event such as ‘click’, ‘hover’ etc. I implemented my own callback as following (with taking the example code from plot.ly/dash), but it ended up with that my code wasn’t get called when the point is clicked. After some debugging work, it turns out that the myPlot.on was called too earlier and it has to been called after the point when the plot chart has finish rendering in the window. After calling myPlot.on at a later point, my callback gets called correctly when the point is clicked.
But when I did some interactive operation on the user interface e.g. select a different value in a dropdown list which result in the whole plot being updated. My ‘plotly_click’ callback doesn’t take effect again. I’m wondering whether I’m doing the right thing to achieve my objective or I’m lost in a wrong direction.
I am using dash version 0.17.7 and dash_core_components version 0.5.1
myPlot.on('plotly_click', function(data){
for(var i=0; i < data.points.length; i++){
annotate_text = 'x = '+data.points[i].x +
'y = '+data.points[i].y.toPrecision(4);
copyTextToClipboard(annotate_text)
annotation = {
text: annotate_text,
x: data.points[i].x,
y: parseFloat(data.points[i].y.toPrecision(4))
}
annotations = [];
annotations.push(annotation);
Plotly.relayout('indicator-graphic',{annotations: annotations})
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
}
})