获取在Plotly.js中包含在选择中的点。

huangapple go评论65阅读模式
英文:

Retrieve the points contained in a selection with Plotly.js

问题

我正在尝试检查图表中的点是否包含在预定义的选择中,因此我已经添加了一个带有适当配置的选择框,放置在layout部分:

var N = 100;

function randomArray() {
  var output = new Array(N);
  for (var i = 0; i < N; i++) {
    output[i] = Math.random();
  }
  return output;
}

var myPlot = document.getElementById("plotly-graph");

var settings = [
  {
    mode: "markers",
    x: randomArray(),
    y: randomArray()
  }
];

var layout = {
  selections: [
    {
      type: "rect",
      x0: 0.1,
      x1: 0.2,
      xref: "x",
      y0: 0.1,
      y1: 0.2,
      yref: "y"
    }
  ]
};

var config = {};

Plotly.react(myPlot, settings, layout, config);

问题是,所选点的数组在我手动添加另一个选择之前是空的(使用任务栏上的选择工具进行手动交互)。
之后,所有选择内的点都正确地包含在所选点的数组中。
我想找到一种以编程方式触发此选择事件的方法,而不必手动交互,这是可能的吗?有关如何解决这个问题的任何建议?

谢谢!

英文:

I'm trying to check if the points in the chart are contained in predefined selections, so I've add a selection box with the proper configuration of the layout section:

var N = 100;

function randomArray() {
  var output = new Array(N);
  for (var i = 0; i < N; i++) {
    output[i] = Math.random();
  }
  return output;
}

var myPlot = document.getElementById("plotly-graph");

var settings = [
  {
    mode: "markers",
    x: randomArray(),
    y: randomArray()
  }
];

var layout = {
  selections: [
    {
      type: "rect",
      x0: 0.1,
      x1: 0.2,
      xref: "x",
      y0: 0.1,
      y1: 0.2,
      yref: "y"
    }
  ]
};

var config = {};

Plotly.react(myPlot, settings, layout, config);

The problem is that the array of selected points is empty until I manually add another selection (using manual interaction with the selection tool from the taskbar).
After that, the points within all selections are correctly included in the array of selected points.
I would like to find a way to trigger this selection event programmatically, without having to use a manual interaction, is this possible? Any suggestions on how to solve this problem?

Thank you!

答案1

得分: 1

如何从您已知的点数组中获取已知矩形内的点?

var N = 100;

function randomArray() {
  var output = new Array(N);
  for (var i = 0; i < N; i++) {
    output[i] = Math.random();
  }
  return output;
}

var myPlot = document.getElementById("plotly-graph");

var settings = [
  {
    mode: "markers",
    x: randomArray(),
    y: randomArray()
  }
];

var layout = {};

var config = {};

Plotly.newPlot(myPlot, settings, layout, config);

function getPointsInRectangle(rect) {
  var xData = settings[0].x;
  var yData = settings[0].y;

  var selectedPoints = [];

  for (var i = 0; i < xData.length; i++) {
    if (
      xData[i] >= rect.x0 &&
      xData[i] <= rect.x1 &&
      yData[i] >= rect.y0 &&
      yData[i] <= rect.y1
    ) {
      selectedPoints.push({ x: xData[i], y: yData[i] });
    }
  }

  return selectedPoints;
}

var rect = {
  x0: 0.1,
  x1: 0.2,
  y0: 0.1,
  y1: 0.2
};

var selectedPoints = getPointsInRectangle(rect);
console.log(selectedPoints);

这应该有效:

/* 从您的问题中提取的所有代码都在这里 */

myPlot.on("plotly_selected", function(eventData) {
  var selectedPoints = eventData.points;
  console.log(selectedPoints);
});

// 以编程方式触发选择事件
var eventData = { points: selectedPoints };
myPlot.emit('plotly_selected', eventData);

注意:代码部分未进行翻译。

英文:

How about just getting points in your known rectangle from the point array that you already know?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var N = 100;
function randomArray() {
var output = new Array(N);
for (var i = 0; i &lt; N; i++) {
output[i] = Math.random();
}
return output;
}
var myPlot = document.getElementById(&quot;plotly-graph&quot;);
var settings = [
{
mode: &quot;markers&quot;,
x: randomArray(),
y: randomArray()
}
];
var layout = {};
var config = {};
Plotly.newPlot(myPlot, settings, layout, config);
function getPointsInRectangle(rect) {
var xData = settings[0].x;
var yData = settings[0].y;
var selectedPoints = [];
for (var i = 0; i &lt; xData.length; i++) {
if (
xData[i] &gt;= rect.x0 &amp;&amp;
xData[i] &lt;= rect.x1 &amp;&amp;
yData[i] &gt;= rect.y0 &amp;&amp;
yData[i] &lt;= rect.y1
) {
selectedPoints.push({ x: xData[i], y: yData[i] });
}
}
return selectedPoints;
}
var rect = {
x0: 0.1,
x1: 0.2,
y0: 0.1,
y1: 0.2
};
var selectedPoints = getPointsInRectangle(rect);
console.log(selectedPoints);

<!-- language: lang-html -->

&lt;!-- Plotly.js --&gt;
&lt;script src=&quot;https://cdn.plot.ly/plotly-latest.min.js&quot;&gt;&lt;/script&gt;
&lt;div style=&quot;width: 800px; height: 500px;&quot; id=&quot;plotly-graph&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

This should work :

/* all the code from your question is here */

myPlot.on(&quot;plotly_selected&quot;, function(eventData) {
  var selectedPoints = eventData.points;
  console.log(selectedPoints);
});

// Programmatically trigger the selection event
var eventData = {points: selectedPoints};
myPlot.emit(&#39;plotly_selected&#39;, eventData);

答案2

得分: 1

Here is the translated content you requested:

我记录了由plotly库中的事件发出的所有数据,应该如下所示:

plotly_afterexport
plotly_afterplot
plotly_animated
plotly_animating
plotly_animatingframe
plotly_animationinterrupted
plotly_autosize
plotly_beforeexport
plotly_buttonclicked
plotly_click
plotly_clickannotation
plotly_deselect
plotly_doubleclick
plotly_framework
plotly_hover
plotly_react
plotly_redraw
plotly_relayout
plotly_relayouting
plotly_restyle
plotly_selected
plotly_selecting
plotly_sliderchange
plotly_sliderend
plotly_sliderstart
plotly_transitioned
plotly_transitioning
plotly_transitioninterrupted
plotly_unhover
plotly_update
plotly_webglcontextlost

在探索“plotly_react”事件的事件数据时,我发现了“data”对象内的“selectedpoints”:

{
"data": [
{
"x": [
0.01,
...,
100
],
"y": [
0.0006,
...,
0
],
"mode": "markers",
"selectedpoints": [
4182,
...,
4365
]
}
],
"layout": {
"showlegend": false,
"dragmode": "select",
"shapes": [
{
"editable": true,
"xref": "x",
"yref": "y",
"layer": "above",
"opacity": 0.5,
"line": {
"color": "#444",
"width": 4,
"dash": "solid"
},
"fillcolor": "green",
"fillrule": "evenodd",
"type": "rect",
"x0": 39.021639200761186,
"y0": 0.4910903426791277,
"x1": 46.663120456707894,
"y1": 0.3875887850467289
}
],
"selections": [
{
"editable": true,
"xref": "x",
"yref": "y",
"layer": "above",
"opacity": 0.5,
"line": {
"color": "#444",
"width": 4,
"dash": "solid"
},
"fillcolor": "green",
"fillrule": "evenodd",
"type": "rect",
"x0": 39.021639200761186,
"y0": 0.4910903426791277,
"x1": 46.663120456707894,
"y1": 0.3875887850467289
}
],
"xaxis": {
"type": "linear",
"range": [
-5.809272121788773,
105.81927212178877
],
"autorange": true
},
"yaxis": {
"type": "linear",
"range": [
-1.1214953271028036,
1.1214953271028036
],
"autorange": true
},
"autosize": true
}
}

因此,为了获取图表的选定点,使用内置的选择功能,只需迭代每个数据对象(如果存在多个跟踪)并将相对的“selectedpoints”合并到所有选定点的数组中,例如“wholeSelectedPoints”。

var wholeSelectedPoints = [];
plotly_react_eventData['data'].forEach(element => {
wholeSelectedPoints = [...wholeSelectedPoints, ...element['selectedpoints']]
});
console.log(wholeSelectedPoints);
// [object Array] (4)
// [4182, 4365, 10, 20]
英文:

I logged all the data emitted by the events in the plotly library, which should be as follows:

plotly_afterexport
plotly_afterplot
plotly_animated
plotly_animating
plotly_animatingframe
plotly_animationinterrupted
plotly_autosize
plotly_beforeexport
plotly_buttonclicked
plotly_click
plotly_clickannotation
plotly_deselect
plotly_doubleclick
plotly_framework
plotly_hover
plotly_react
plotly_redraw
plotly_relayout
plotly_relayouting
plotly_restyle
plotly_selected
plotly_selecting
plotly_sliderchange
plotly_sliderend
plotly_sliderstart
plotly_transitioned
plotly_transitioning
plotly_transitioninterrupted
plotly_unhover
plotly_update
plotly_webglcontextlost

Exploring the event data of the plotly_react event, which I use to draw and update the chart, I found the selectedpoints inside the data object:

{
&quot;data&quot;: [
{
&quot;x&quot;: [
0.01,
...,
100
],
&quot;y&quot;: [
0.0006,
...,
0
],
&quot;mode&quot;: &quot;markers&quot;,
&quot;selectedpoints&quot;: [
4182,
...,
4365
]
}
],
&quot;layout&quot;: {
&quot;showlegend&quot;: false,
&quot;dragmode&quot;: &quot;select&quot;,
&quot;shapes&quot;: [
{
&quot;editable&quot;: true,
&quot;xref&quot;: &quot;x&quot;,
&quot;yref&quot;: &quot;y&quot;,
&quot;layer&quot;: &quot;above&quot;,
&quot;opacity&quot;: 0.5,
&quot;line&quot;: {
&quot;color&quot;: &quot;#444&quot;,
&quot;width&quot;: 4,
&quot;dash&quot;: &quot;solid&quot;
},
&quot;fillcolor&quot;: &quot;green&quot;,
&quot;fillrule&quot;: &quot;evenodd&quot;,
&quot;type&quot;: &quot;rect&quot;,
&quot;x0&quot;: 39.021639200761186,
&quot;y0&quot;: 0.4910903426791277,
&quot;x1&quot;: 46.663120456707894,
&quot;y1&quot;: 0.3875887850467289
}
],
&quot;selections&quot;: [
{
&quot;editable&quot;: true,
&quot;xref&quot;: &quot;x&quot;,
&quot;yref&quot;: &quot;y&quot;,
&quot;layer&quot;: &quot;above&quot;,
&quot;opacity&quot;: 0.5,
&quot;line&quot;: {
&quot;color&quot;: &quot;#444&quot;,
&quot;width&quot;: 4,
&quot;dash&quot;: &quot;solid&quot;
},
&quot;fillcolor&quot;: &quot;green&quot;,
&quot;fillrule&quot;: &quot;evenodd&quot;,
&quot;type&quot;: &quot;rect&quot;,
&quot;x0&quot;: 39.021639200761186,
&quot;y0&quot;: 0.4910903426791277,
&quot;x1&quot;: 46.663120456707894,
&quot;y1&quot;: 0.3875887850467289
}
],
&quot;xaxis&quot;: {
&quot;type&quot;: &quot;linear&quot;,
&quot;range&quot;: [
-5.809272121788773,
105.81927212178877
],
&quot;autorange&quot;: true
},
&quot;yaxis&quot;: {
&quot;type&quot;: &quot;linear&quot;,
&quot;range&quot;: [
-1.1214953271028036,
1.1214953271028036
],
&quot;autorange&quot;: true
},
&quot;autosize&quot;: true
}
}

So in order to grab the selected points of the chart, using the built in selection function, is it sufficient to iterate trough each data objects (if more than a single trace is present) and merging the relative selectedpoints in an array of all selected points, e.g. wholeSelectedPoints.

var wholeSelectedPoints = [];
plotly_react_eventData[&#39;data&#39;].forEach(element =&gt; {
wholeSelectedPoints = [...wholeSelectedPoints, ...element[&#39;selectedpoints&#39;]]
});
console.log(wholeSelectedPoints);
// [object Array] (4)
// [4182,4365,10,20]

huangapple
  • 本文由 发表于 2023年6月8日 21:50:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定