P粉6623617402023-09-03 09:53:17
研究了您的示例,我发现您的 console.log
没有提供预期的数据,因为 data
是循环的。但是,由于 data.points
具有单个元素,因此您可以通过 data.points[0].x
、data.points[0] 获取坐标。分别为 y
和 data.points[0].z
。
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>