<legend id='AX2op'><style id='AX2op'><dir id='AX2op'><q id='AX2op'></q></dir></style></legend>
  1. <i id='AX2op'><tr id='AX2op'><dt id='AX2op'><q id='AX2op'><span id='AX2op'><b id='AX2op'><form id='AX2op'><ins id='AX2op'></ins><ul id='AX2op'></ul><sub id='AX2op'></sub></form><legend id='AX2op'></legend><bdo id='AX2op'><pre id='AX2op'><center id='AX2op'></center></pre></bdo></b><th id='AX2op'></th></span></q></dt></tr></i><div id='AX2op'><tfoot id='AX2op'></tfoot><dl id='AX2op'><fieldset id='AX2op'></fieldset></dl></div>

      <tfoot id='AX2op'></tfoot>
        <bdo id='AX2op'></bdo><ul id='AX2op'></ul>

      <small id='AX2op'></small><noframes id='AX2op'>

    1. Plotly.js 在点击时创建一个点

      时间:2023-09-30
      <i id='dJP8u'><tr id='dJP8u'><dt id='dJP8u'><q id='dJP8u'><span id='dJP8u'><b id='dJP8u'><form id='dJP8u'><ins id='dJP8u'></ins><ul id='dJP8u'></ul><sub id='dJP8u'></sub></form><legend id='dJP8u'></legend><bdo id='dJP8u'><pre id='dJP8u'><center id='dJP8u'></center></pre></bdo></b><th id='dJP8u'></th></span></q></dt></tr></i><div id='dJP8u'><tfoot id='dJP8u'></tfoot><dl id='dJP8u'><fieldset id='dJP8u'></fieldset></dl></div>
      • <legend id='dJP8u'><style id='dJP8u'><dir id='dJP8u'><q id='dJP8u'></q></dir></style></legend>
      • <tfoot id='dJP8u'></tfoot>

              <tbody id='dJP8u'></tbody>
              <bdo id='dJP8u'></bdo><ul id='dJP8u'></ul>

              <small id='dJP8u'></small><noframes id='dJP8u'>

              • 本文介绍了Plotly.js 在点击时创建一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有问题.使用 plotly.js 绘制散点图,我需要能够点击图表并检索点的值,以便我可以在点击的位置绘制一个新点.plotly_click 事件仅在已创建的点上触发.

                Having an issue. Using plotly.js to draw a scatter chart, I need to be able to click on the chart and retrieve the values of the point so that I can draw a new point at the clicked location. The plotly_click event only fires on already created points.

                任何想法如何获得点击事件以从该位置获取当前值以便绘制新点?

                Any ideas how I can get a click event to get the current values from the location so I can plot the new point?

                到目前为止,我只得到这个:

                So far I'm only getting this:

                var trace1 = {
                    x: [1, 2, 3, 4],
                    y: [10, 15, 13, 17],
                    mode: 'markers',
                    type: 'scatter'
                };
                
                var trace2 = {
                    x: [2, 3, 4, 5],
                    y: [16, 5, 11, 9],
                    mode: 'lines',
                    type: 'scatter'
                };
                
                var trace3 = {
                    x: [1, 2, 3, 4],
                    y: [12, 9, 15, 12],
                    mode: 'lines+markers',
                    type: 'scatter'
                };
                
                var data = [trace1, trace2, trace3];
                
                Plotly.newPlot('myDiv', data);
                
                document.getElementById('myDiv').on('plotly_click', function(data){
                    var pts = '';
                
                    for(var i=0; i < data.points.length; i++){
                        pts = 'x = ' + data.points[i].x +'
                y = ' + data.points[i].y.toPrecision(4) + '
                
                ';
                    }
                
                    alert('Closest point clicked:
                
                '+pts);
                });
                

                https://codepen.io/mayasky76/pen/ZXRRMJ/

                推荐答案

                非常感谢 Maximillian Peters 的这个

                Many thanks to Maximillian Peters for this

                现在有一个散点图,它报告鼠标移动时的位置 (xy) 值,并在点击时在图表上绘制一个点

                Now have a scatter graph that reports location (xy) value on mousemove and plots a point to the chart on click

                <head>
                  <!-- Plotly.js -->
                   <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
                </head>
                
                <body>
                  x :<input id="xvalue" size="5"  /> y :<input id="yvalue" size="5" /> 
                 <div id="myPlot" style="width:100%;height:100%"></div>
                  <script>
                    <!-- JAVASCRIPT CODE GOES HERE -->
                  </script>
                </body>
                

                Javascript

                var traces = [{
                  x: [1, 2, 3, 4],
                  y: [10, 15, 13, 17],
                  mode: 'markers',
                  type: 'scatter'
                }];
                
                traces.push({
                  x: [2, 3, 4, 5],
                  y: [16, 5, 11, 9],
                  mode: 'markers',
                  type: 'scatter'
                });
                
                traces.push({
                  x: [1, 2, 3, 4],
                  y: [12, 9, 15, 12],
                  mode: 'markers',
                  type: 'scatter'
                });
                
                traces.push({
                  x: [],
                  y: [],
                  mode: 'markers',
                  type: 'scatter'
                });
                
                var myPlot = document.getElementById('myPlot')
                Plotly.newPlot('myPlot', traces, {hovermode: 'closest'});
                
                Number.prototype.between = function(min, max) {
                  return this >= min && this <= max;
                };
                
                
                Plotly.d3.select(".plotly").on('click', function(d, i) {
                  var e = Plotly.d3.event;
                  var bg = document.getElementsByClassName('bg')[0];
                  var x = ((e.layerX - bg.attributes['x'].value + 4) / (bg.attributes['width'].value)) * (myPlot.layout.xaxis.range[1] - myPlot.layout.xaxis.range[0]) + myPlot.layout.xaxis.range[0];
                  var y = ((e.layerY - bg.attributes['y'].value + 4) / (bg.attributes['height'].value)) * (myPlot.layout.yaxis.range[0] - myPlot.layout.yaxis.range[1]) + myPlot.layout.yaxis.range[1]
                  if (x.between(myPlot.layout.xaxis.range[0], myPlot.layout.xaxis.range[1]) &&
                    y.between(myPlot.layout.yaxis.range[0], myPlot.layout.yaxis.range[1])) {
                    Plotly.extendTraces(myPlot, {
                      x: [
                        [x]
                      ],
                      y: [
                        [y]
                      ]
                    }, [3]);
                  }
                });
                
                Plotly.d3.select(".plotly").on('mousemove', function(d, i) {
                  var e = Plotly.d3.event;
                  var bg = document.getElementsByClassName('bg')[0];
                  var x = ((e.layerX - bg.attributes['x'].value + 4) / (bg.attributes['width'].value)) * (myPlot.layout.xaxis.range[1] - myPlot.layout.xaxis.range[0]) + myPlot.layout.xaxis.range[0];
                  var y = ((e.layerY - bg.attributes['y'].value + 4) / (bg.attributes['height'].value)) * (myPlot.layout.yaxis.range[0] - myPlot.layout.yaxis.range[1]) + myPlot.layout.yaxis.range[1]
                  if (x.between(myPlot.layout.xaxis.range[0], myPlot.layout.xaxis.range[1]) &&
                    y.between(myPlot.layout.yaxis.range[0], myPlot.layout.yaxis.range[1])) {
                    console.log("Location X:"+x+" Y"+y)
                   document.getElementById("xvalue").value = x;
                   document.getElementById("yvalue").value = y;
                  }
                });
                

                https://codepen.io/mayasky76/pen/ZXRRMJ

                这篇关于Plotly.js 在点击时创建一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:修复标题在两行上拆分时的 plotly ggplotly() 标题重叠图 下一篇:如何在单个浏览器页面上向 Dash 应用程序添加多个图表?

                相关文章

                • <bdo id='umoSv'></bdo><ul id='umoSv'></ul>

                <small id='umoSv'></small><noframes id='umoSv'>

                  <legend id='umoSv'><style id='umoSv'><dir id='umoSv'><q id='umoSv'></q></dir></style></legend>

                  1. <tfoot id='umoSv'></tfoot>

                    <i id='umoSv'><tr id='umoSv'><dt id='umoSv'><q id='umoSv'><span id='umoSv'><b id='umoSv'><form id='umoSv'><ins id='umoSv'></ins><ul id='umoSv'></ul><sub id='umoSv'></sub></form><legend id='umoSv'></legend><bdo id='umoSv'><pre id='umoSv'><center id='umoSv'></center></pre></bdo></b><th id='umoSv'></th></span></q></dt></tr></i><div id='umoSv'><tfoot id='umoSv'></tfoot><dl id='umoSv'><fieldset id='umoSv'></fieldset></dl></div>