D3:如何仅在条形图中显示6个项目中的前3个?

时间:2016-02-26 01:03:38

标签: javascript d3.js leaflet

我是D3的极端新手。我正在使用LeafletJS内置的网络地图,并将一个D3条形图放入弹出窗口,显示阿富汗某个地区的各种族群。在我的真实项目中,我在该地区有超过26个种族群体,我只想在条形图中显示前三大种族群体。

我现在只设置了6个种族的 demo in jsfiddle

我如何仅在条形图中显示前三大种族并隐藏其他种族群体?

  

我的JSON数据示例:

var myData = [{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"Name":"Gulran","Province":"Hirat","Ethnic1":0.19,"Ethnic2":0.32,"Ethnic3":"0.10","Ethnic4":"0.00","Ethnic5":"0.10","Ethnic6":"0.00"},"geometry":{"type":"Polygon","coordinates":[[[60.941162109375,29.897805610155874],[61.92993164062499,31.034108344903512],[63.34716796874999,31.3348710339506],[64.05029296875,30.401306519203583],[64.412841796875,29.735762444449076],[64.09423828125,29.36302703778376],[62.29248046875,29.36302703778376],[60.941162109375,29.897805610155874]]]}},{"type":"Feature","properties":{"Name":"Chahar Burjak","Province":"Nimroz","Ethnic1":0.25,"Ethnic2":0.12,"Ethnic3":0.03,"Ethnic4":0.01,"Ethnic5":"0.00","Ethnic6":"0.00"},"geometry":{"type":"Polygon","coordinates":[[[63.38012695312499,31.3348710339506],[65.06103515625,31.80289258670676],[65.6982421875,31.156408414557],[66.016845703125,30.467614102257855],[65.291748046875,30.164126343161097],[64.22607421875,30.0405664305846],[63.38012695312499,31.3348710339506]]]}}]}];
  

我的D3代码:

var div = $('<div id="chart" style="width: 600px; height: 400px;"><svg></div>')[0];

        var popup = L.popup({minWidth: 600}).setContent(div);
        layer.bindPopup(popup);

        var values = feature.properties;
        var data = [
            {name:"Ethnic1",value:values["Ethnic1"]},
            {name:"Ethnic2",value:values["Ethnic2"]},
            {name:"Ethnic3",value:values["Ethnic3"]},
            {name:"Ethnic4",value:values["Ethnic4"]},
            {name:"Ethnic5",value:values["Ethnic5"]},
            {name:"Ethnic6",value:values["Ethnic6"]}
        ];

        var margin = {top: 20, right: 30, bottom: 40, left: 40},
            width = 600 - margin.left - margin.right,
            height = 400 - margin.top - margin.bottom,
            barHeight = height / data.length;

        var formatPercent = d3.format(".0%");


        var x = d3.scale.linear()
            .domain([0, d3.max(data, function(d){return d.value;})])
            .range([0, width]);
        var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom")
            .tickFormat(formatPercent);

        var svg = d3.select(div).select("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
          .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
            .classed("chart", true);

        svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);

        var bar = svg.selectAll("g.bar")
            .data(data)
            .enter()
          .append("g")
            .attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });

        bar.append("rect")
            .attr("width", function(d){return x(d.value);})
            .attr("height", barHeight - 1);

        bar.append("text")
            .attr("x", function(d) { return x(d.value) - 3; })
            .attr("y", barHeight / 2)
            .attr("dy", ".35em")
            .text(function(d) { return d.name; });

2 个答案:

答案 0 :(得分:0)

您这样做的方法是首先对数据进行排序。排序后,您可以过滤数据以仅使用前3个值来创建条形图。

我把你的小提琴分开了。这是链接https://jsfiddle.net/ankit89/x8u31jdo/

我添加/修改的重要代码是

       var sortedData = data.sort(function(a,b){
        return b.value - a.value;
       });

       //if you want to just keep top three
       sortedData = sortedData.filter(function(d,i){
        return i < 3;
       });

        var bar = svg.selectAll("g.bar")
            .data(sortedData)
            .enter()
            .append("g")
            .attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });

答案 1 :(得分:0)

以下是您的Working Fiddle

使用的代码如下

function GetTopThreeEthnic(arrayData){  //sorting to top 3 function
  arrayData.sort(function(a, b) {
                   return parseFloat(b.value) - parseFloat(a.value);
                 });
  return arrayData.slice(0, 3); 
}

初始化data变量后,只需调用此函数。

    var data = [
                    {name:"Ethnic1",value:values["Ethnic1"]},
                    {name:"Ethnic2",value:values["Ethnic2"]},
                    {name:"Ethnic3",value:values["Ethnic3"]},
                    {name:"Ethnic4",value:values["Ethnic4"]},
                    {name:"Ethnic5",value:values["Ethnic5"]},
                    {name:"Ethnic6",value:values["Ethnic6"]}
                ];                   
    data = GetTopThreeEthnic(data); // this is the code added