[英]Sorting the bars and getting the group data in a grouped bar chart
I've been trying to learn Javascript on my own for data visualization in the past couple days and is working/struggling with a grouped bar chart.在过去的几天里,我一直在尝试自己学习 Javascript 以进行数据可视化,并且正在使用分组条形图工作/挣扎。
Chart working in progress: https://blockbuilder.org/lydiawawa/261aebe55bef8b556d257f3693cca37e正在进行中的图表: https : //blockbuilder.org/lydiawawa/261aebe55bef8b556d257f3693cca37e
x-axis: Drug Categories x 轴:药物类别
y-axis: Count of Drug Categories x S Categories y 轴:药物类别数 x S 类别
My current milestones:我目前的里程碑:
How do I point to the 'key' value (1,2,3,4,5,6) in the first level of nestedData so that it is defined as the drug category in the tooltip?如何指向nestedData 第一级中的'key' 值(1,2,3,4,5,6),以便在工具提示中将其定义为药物类别?
I think the code should be simlar to:我认为代码应该类似于:
nestedData.sort(function(x, y){
return d3.ascending(x.value, y.value);
})
How should I implement the sort with animation triggered by a radio button?我应该如何通过单选按钮触发的动画实现排序? Similar to this effect:
类似于这个效果:
https://bl.ocks.org/fabiomainardi/2971d4ac0978634c3d15 https://bl.ocks.org/fabiomainardi/2971d4ac0978634c3d15
Appreciate for any help.感谢任何帮助。
Regarding the tooltip issue: the data for the outer array was bound to the groups that contain the rectangles.关于工具提示问题:外部数组的数据绑定到包含矩形的组。
Therefore, you can get it with this.parentNode
:因此,您可以使用
this.parentNode
获取它:
const parentData = d3.select(this.parentNode).datum();
Regarding the sorting issue, you can sort the nested data with:关于排序问题,您可以使用以下方法对嵌套数据进行排序:
nestedData.forEach(function(d){
d.values.sort(function(a,b){
return a.value - b.value
});
});
However, this won't make any difference: the order of the bars depend on the domain you passed to x1
.但是,这没有任何区别:条形的顺序取决于您传递给
x1
的域。 So, unless you change the domain for each group (which is not a good practice in data visualisation), you can't sort the bars within groups.因此,除非您更改每个组的域(这在数据可视化中不是一个好习惯),否则您无法对组内的条形进行排序。
Here is the updated blockbuilder: https://blockbuilder.org/GerardoFurtado/f4b4608bf07588f2b9291ac74c88f82c这是更新的区块构建器: https : //blockbuilder.org/GerardoFurtado/f4b4608bf07588f2b9291ac74c88f82c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.