问题
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