gpt4 book ai didi

javascript - D3 : How to show only top 3 of 6 items in bar chart?

转载 作者:行者123 更新时间:2023-11-28 06:19:46 24 4
gpt4 key购买 nike

我是 D3 的新手。我正在使用 LeafletJS 构建的网络 map ,并将 D3 条形图放入弹出窗口中,显示阿富汗某个地区的各个民族。在我的实际项目中,该地区有超过 26 个民族,我只想在条形图中显示前 3 个最大的民族。

我已经设置了 demo in jsfiddle 目前只有 6 个民族。

如何在条形图中仅显示前 3 个最大的种族群体并隐藏其余的种族群体?

Example of my JSON data:

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]]]}}]}];

My D3 code:

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; });

最佳答案

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

我已经 fork 了你的 fiddle 。这是链接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 + ")"; });

关于javascript - D3 : How to show only top 3 of 6 items in bar chart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com