简体   繁体   English

如何在 d3.js 中获取不同的值?

[英]How to get Distinct values in d3.js?

How to get Distinct values in d3.js.如何在 d3.js 中获取不同的值。

Here is my code这是我的代码

var bar = svg.selectAll(".nodes")
    .data(d3.values(nodes.filter(function(d){return d.group==1;})).sort(function(a, b){return b.value - a.value;}))
;

bar.enter().append("rect")
    .filter(function(d) { return d.value > 0 })
    .attr("transform", function(d, i) { return "translate(1200," + i * barHeight + ")"; })
   // .attr("transform", function(d, i) { return "translate(850," + i * barHeight + ")"; })
    .attr("x", function(d) { return width - x(d.value) ; }) 
    .attr("y",56)
    .attr("width", function(d) { return x(d.value); })
    .attr("height", barHeight - 1)
    .attr("fill", function(d) { return color(d.group); })
    //.attr('class', function(d) { return d.node_name;} ); 
    .attr('class', function(d) { return "bar"+d.node_name.replace(/[^a-zA-Z0-9 ]/gi, "");} ); 

Here is the sample of data这是数据样本

 var nodes = [{ node_name: "insider.webmetro.com", name: "insider.webmetro.com", group: 1, value: 15.54, type: "circle" }, { node_name: "internetmarketing.webmetro.com/About.htm", name: "internetmarketing.webmetro.com/About.htm", group: 1, value: 15.54, type: "circle" }, { node_name: "webmetro.com", name: "webmetro.com", group: 1, value: 606, type: "circle" }, { node_name: "webmetro.com/About.htm", name: "webmetro.com/About.htm", group: 1, value: 19.5, type: "circle" }, { node_name: "webmetro.com/about1.htm", name: "webmetro.com/about1.htm", group: 1, value: 15, type: "circle" }, { node_name: "webmetro.com/blog", name: "webmetro.com/blog", group: 1, value: 212, type: "circle" }, { node_name: "webmetro.com/blog/pay_per_click/amazing_ppc_tactics_and_bid_simulator_at_smx_advanced.aspx", name: "webmetro.com/blog/pay_per_click/amazing_ppc_tactics_and_bid_simulator_at_smx_advanced.aspx", group: 1, value: 42, type: "circle" }, { node_name: "webmetro.com/blog/search_engine_optimization/importance_of_latent_semantic_indexing_in_seo.aspx", name: "webmetro.com/blog/search_engine_optimization/importance_of_latent_semantic_indexing_in_seo.aspx", group: 1, value: 28, type: "circle" }, { node_name: "webmetro.com/brochure", name: "webmetro.com/brochure", group: 1, value: 212, type: "circle" }, { node_name: "webmetro.com/careers", name: "webmetro.com/careers", group: 1, value: 15.54, type: "circle" }];

I am not really sure what exactly you are after.我不确定你到底在追求什么。 Are you are after the distinct elements of the nodes array, or the distinct values field from the nodes array?你是的不同的元素之后nodes阵列,或不同的values从外地nodes阵列?

Since there are no duplicated entries in the array itself, I assume you are refering to the unique values field.由于数组本身没有重复的条目,我假设您指的是唯一values字段。 In that case, you can use the following method to retrieve the unique(distinct) values:在这种情况下,您可以使用以下方法来检索唯一(不同)值:

function unique(x) {
    return x.reverse().filter(function (e, i, x) {return x.indexOf(e, i+1) === -1;}).reverse();
}

and to obtain the unique values, you can do:并获得唯一值,您可以执行以下操作:

var unique_values = unique(nodes.map(function(d){return d.value}))

I hope this helps.我希望这有帮助。

If I got right, you want distinct objects based on object.value .如果我object.value对了,您需要基于object.value不同对象。

To find object based on it's properties, you can use following function:要根据其属性查找对象,您可以使用以下函数:

function findByValue(source, value) {
    return source.filter(function( obj ) {
        return +obj.value === +value;
    })[ 0 ];
}

Include it in your filter function like this:将它包含在您的filter功能中,如下所示:

var result = nodes.filter(function(element_value, element_index, element_array){return element_value.group==1 && element_array.indexOf(findByValue( nodes, element_value.value )) === element_index });

Demo fiddle: http://jsfiddle.net/j3vST/354/ - you will see in console log that only distinct objects (based on object.value are returned).演示小提琴: http : //jsfiddle.net/j3vST/354/ - 您将在控制台日志中看到只有不同的对象(基于object.value被返回)。

使用 ES6,您可以使用传播语法

const distinctValues = [...new Set(data.map((d) => d.column))]

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

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