简体   繁体   English

带套索的D3.js散点图-记录选择

[英]D3.js Scatterplot with Lasso - record selections

CONTEXT 语境

I'm using d3.js & Lasso in order to allow users to select dots on a scatterplot. 我正在使用d3.js和Lasso,以便允许用户选择散点图上的点。 I want them to be able to select multiple clusters on the same scatterplot, one after the other. 我希望他们能够在一个散点图上一个接一个地选择多个集群。 I've found an example of how to do this right here: http://bl.ocks.org/skokenes/511c5b658c405ad68941 我在这里找到了如何执行此操作的示例: http : //bl.ocks.org/skokenes/511c5b658c405ad68941

PROBLEM 问题

I want to record each selection of dots so that I end up with an array, where each dot has a list of clusters it belongs to. 我想记录每个点的选择,以便得到一个数组,每个点都有一个属于它的簇列表。 For example, Dot1 belongs to clusters [1,3,4]. 例如,Dot1属于群集[1,3,4]。

QUESTION

What would be the best way of storing these selections? 存储这些选择的最佳方法是什么?

What would be the best way of storing these selections? 存储这些选择的最佳方法是什么?

Well, that's too "opinion based" for SO However, I'll share a very crude solution, in which instead of creating for each dot a list of clusters it belongs to, we'll create a list of cluster with their corresponding dots. 好吧,对于SO来说,它也是基于“观点”的。但是,我将分享一个非常粗糙的解决方案,在该解决方案中,我们将为每个点创建一个属于其的簇的列表,而不是为每个点创建一个簇列表。 Pretty much the opposite of what you asked , but you can easily modify the resulting array (an array with the dots for each selection) to create your desired record (one array with the selections for each dot). 几乎与您要的相反 ,但是您可以轻松地修改结果数组(每个选择带有点的数组)以创建所需的记录(每个选择带有一个点的数组)。

The first step is defining the array outside lasso_end : 第一步是在lasso_end外部定义数组:

var clusters = [];

Then, inside lasso_end , we get a list of selected dots: 然后,在lasso_end内部,我们得到一个选定点的列表:

var selected = lasso.items().filter(function(d){
    return d.selected===true
});

var selectedDots = selected[0].map(d=>d.id);

Here, I'm mapping by ID. 在这里,我通过ID进行映射。 Then, we push the array into clusters : 然后,将数组推入clusters

clusters.push(selectedDots);

Every time the user selects some dots, cluster becomes bigger. 每次用户选择一些点时, cluster就会变大。 So, in the first time, you can get something like this: 因此,在第一时间,您可以获得以下内容:

var clusters = [["dot_62","dot_68","dot_87","dot_119"]];

And, in the second time: 并且,第二次:

var clusters = [["dot_62","dot_68","dot_87","dot_119"],
    ["dot_53","dot_57","dot_80","dot_81","dot_93"]];

Here is a plunker, select your dots and check the console: https://plnkr.co/edit/qiZ6bkgZhoSn3XfJW2l7?p=preview 这是一个小矮人,选择您的点并检查控制台: https ://plnkr.co/edit/qiZ6bkgZhoSn3XfJW2l7?p=preview

PS: As I said before, this is a very crude solution: if the user just clicks anywhere in the chart, clusters will have a new empty array. PS:正如我之前所说,这是一个非常粗糙的解决方案:如果用户仅单击图表中的任意位置,则clusters将具有一个新的空数组。 So, you'll have to modify it to your purposes. 因此,您必须根据自己的目的对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM