d3.js sorting by rollup field

前端 未结 3 799
面向向阳花
面向向阳花 2021-01-21 08:24

I am having a JSON data and i want to group by a field and then sort by the count.

var data = [{\"Name\":\"Ravi\",\"Country\":\"India\"},
            {\"Name\":         


        
3条回答
  •  太阳男子
    2021-01-21 08:50

    d3 provides a method sortKeys in nest function which will sort your nested list based on the key you selected. You can pass d3.ascending or d3.descending based on your requirement.

    From your example:

    var countryCount = d3.nest()
                        .key(function(d) { return d.Country; })
                        .sortKeys(d3.ascending)
                        .entries(data);

    which will give you:

    [{"key":"India","values":4},{"key":"UK","values":3},{"key":"USA","values":1}]

提交回复
热议问题