P粉6623617402023-09-03 09:53:17
Studying your example, I found that your console.log
does not provide the expected data because the data
is circular. However, since data.points
has a single element, you can get the coordinates via data.points[0].x
, data.points[0]. They are y
and data.points[0].z
respectively.
const z = Array.from({ length: 50 }, () => Array.from({ length: 50 }, () => Math.floor(Math.random() * 255))); const plot = document.querySelector("#plot"); const data = [{ type: 'heatmap', z: z }]; const layout = { 'yaxis': { 'scaleanchor': 'x' } }; const config = { modeBarButtons: [ ["zoom2d"], ["zoomIn2d"], ["zoomOut2d"], ["autoScale2d"], ["select2d"], ["drawcircle"] ], displaylogo: false, displayModeBar: true }; Plotly.newPlot('plot', data, layout, config); plot.on("plotly_selected", (data) => { console.log(data); }); plot.on('plotly_click', function(data) { console.log({x: data.points[0].x, y: data.points[0].y, z: data.points[0].z}); });
<script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script> <div id="plot"></div>