简体   繁体   English

[帮助]:d3.js Scatterplot遍历3个不同的数组

[英][Help]: d3.js Scatterplot traverse through 3 different arrays

Description of what i am doing: I generated a scatterplot. 我正在做的描述:我生成了一个散点图。 when user clicks on cluster button, old scatter plot erases and the new clustered data shows up. 当用户点击群集按钮时,旧的散点图将被删除,新的群集数据将显示出来。 Initial data to draw scatterplot was array of coordinates. 绘制散点图的初始数据是坐标数组。 After clustering with dimas-kmeans clustering package,(src: https://www.npmjs.com/package/dimas-kmeans ), The clustered data is stored in 3 different arrays.(3 different clusters) 在使用dimas-kmeans集群包(src: https ://www.npmjs.com/package/dimas-kmeans)进行集群后,集群数据存储在3个不同的阵列中。(3个不同的集群)

Problem: How i can traverse through this new clustered data (which is 3 different arrays) to plot scatterplot with each cluster representing different color ? 问题:我如何遍历这个新的聚类数据(这是3个不同的数组)来绘制散点图,每个聚类代表不同的颜色?

link to jsfiddle: https://jsfiddle.net/data_x/nk2mhhtc/12/ 链接到jsfiddle: https ://jsfiddle.net/data_x/nk2mhhtc/12/

Important code: (At line 126 in jsfiddle) 重要代码:(在jsfiddle的第126行)

       .attr("cx", function (d,i) { //d is each array out of 3 arrays
            console.log(d);
            return  x(d[0]);
        })
        .attr("cy", function (d) {               
            return y(d[1]);
        })
        .style("fill", color)//Different color to each different array out of 3. 3 different color patterns to distinguish 3 different clusters.  

Any kind of hints/help is appreciated, 任何类型的提示/帮助表示赞赏,

Thank you, 谢谢,

clustered_datachangeplot是对象数组,而你最初的data是坐标的数组...大概getClusters必须做更多的收拾正确的数据结构。

Donot know why i myself answer all my questions within few days. 不知道为什么我自己在几天内回答了我的所有问题。

Sol: I donot need to change the data format i got from getclusters() rather i need to parse through different clusters and output color in the following way: Sol:我不需要改变我从getclusters()获得的数据格式,而是需要通过以下方式解析不同的集群并输出颜色:

//Create circles
    var circles1 = svg.selectAll("circle")
                        //.data(cluster_result);
                        .data(clustered_data)
                        .enter()
                        .append('g')
                       .each ( function(d,i) {
                            d3.select(this).selectAll("circle")
                                .data(d.data)
                                .enter()
                                .append("circle")
                                .attr("cx", function(d) { return x(d[0])})
                                .attr("cy", function(d){ return y(d[1])})
                                .attr("r", 3)
                                .style("fill", color(d.data));
                        })

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

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