Sorting method in d3js not working

早过忘川 提交于 2020-01-06 06:48:13

问题


In this bar chart you can sort by clicking the "sort values" checkbox, it works initialy, but if you try to uncheck the box again it stops working.

I get no errors from this either, I suspect that it has something to do with data not being loaded properly or some part of my code is structured poorly.

Here's a Plunker


回答1:


The reason for the unchecking of the sort values to NOT work was the sortIndex.

As the data was being sorted, the original data itself was manipulated and the new sortIndex was based on this new data which resulted in the same array as the sorted one.

What I did is added an init() function to initialize the data and sortIndex for the very first time. Also, on every data update, change the sortIndex based on the original data and NOT in the update function.

Relevant code:

d3.select("#year").on('change', function() {
  data = d3.select('#year').property('value') == '2017' ? data1 : data2;
  sortIndex = data.map( function(d) { return d.month} );
  update();
});

Here's an updated Plunkr with the changes:

Plunkr with sort values onChange fixed

Hope this helps. :)



来源:https://stackoverflow.com/questions/48344419/sorting-method-in-d3js-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!