So, in this dynamic chart, I want to change the Y-axis' min and max values when any of the Y-axis' Tick Label is clicked.
Asked
Active
Viewed 3,791 times
1 Answers
9
You could add a d3 event listener to all y-ticks and make sure that the SVG group gets all the events.
Wrapping the whole snippet in Plotly's afterplot
event makes sure that the event listener does not get lost after updating the graph.
var trace1 = {
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
};
var trace2 = {
x: [1, 2, 3, 4],
y: [16, 5, 11, 9],
type: 'scatter'
};
var data = [trace1, trace2];
var myPlot = document.getElementById('myDiv');
Plotly.newPlot(myPlot, data);
myPlot.on('plotly_afterplot', function(){
Plotly.d3.selectAll(".yaxislayer-above").selectAll('text')
.on("click", function(d) {
alert("Hello, I am " + d.x);
});
});
.yaxislayer-above {
cursor: pointer;
pointer-events: all;
}
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv"></div>

Maximilian Peters
- 30,348
- 12
- 86
- 99
-
Btw after zooming in, it doesn't listen to any click events. And, there's nothing mentioned on the Plotly.js Full Reference about this issue. – Yash P Shah Nov 20 '17 at 23:07
-
1Good catch! The answer was modified accordingly – Maximilian Peters Nov 20 '17 at 23:16
-
I am completely new to d3.js, that's why I couldn't figure out. Anyway thanks a ton! – Yash P Shah Nov 20 '17 at 23:34
-
I think some behavior changed in plotly 2.x since you need to use d3 by itself now AND `d.x` is not the right number any more – qwr Jul 14 '21 at 21:06
-
1Try with cdn.plot.ly/plotly-2.2.0.min.js – qwr Jul 14 '21 at 21:06
-
1I found the original value is accessible through `d.target.__data__.x` but I don't know d3 at all so I there might be an easier way of getting this value – qwr Jul 14 '21 at 21:34