简体   繁体   English

对条形进行排序并获取分组条形图中的组数据

[英]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:我目前的里程碑:

  1. In the tooltip, I tried to index the respective x-axis mark to be defined as "Drug Category", but I'm stuck on indexing the right values because the array I'm dealing with is nested:在工具提示中,我尝试索引相应的 x 轴标记以定义为“药物类别”,但我坚持索引正确的值,因为我正在处理的数组是嵌套的:

在此处输入图片说明

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),以便在工具提示中将其定义为药物类别?

  1. How to sort the within-group counts in ascending order?如何按升序对组内计数进行排序?

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.

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