Adjusting small multiples sparklines

拈花ヽ惹草 提交于 2019-12-04 14:10:56

The vertical and horizontal re-positioning/redrawing of those sparklines require different approaches:

Vertical adjustment

For this solution I'm using selection.sort, which:

Returns a new selection that contains a copy of each group in this selection sorted according to the compare function. After sorting, re-inserts elements to match the resulting order.

So, first, we set our selection:

var sortedSVG = d3.selectAll(".data-svg")

Then, since selection.sort deals with data, we bind the datum, which is the index of the SVG regarding your sorted array:

.datum(function(d){
    return sorted.indexOf(+this.dataset.r)
})

Finally, we compare them in ascending order:

.sort(function(a,b){
    return d3.ascending(a,b)
});

Have in mind that the change is immediate, not a slow and nice transition. This is because the elements are re-positioned in the DOM, and the new structure is painted immediately. For having a slow transition, you'll have to deal with HTML and CSS inside the container div (which may be worth a new specific question).

Horizontal adjustment

The issue here is getting all the relevant data from the selection:

var sel = d3.selectAll('rect[data-r=\'' + k + '\']')
    .each(function() {
        arr.push({value:+d3.select(this).attr('data-value'),
            pos: +d3.select(this).attr('data-c')});
    });

And sorting it according to data-c. After that, we map the result to a simple array:

var result = arr.sort(function(a,b){
        return sorted.indexOf(a.pos) - sorted.indexOf(b.pos)
    }).map(function(d){
        return d.value
    });

Conclusion

Here is the updated Plunker: http://next.plnkr.co/edit/85fIXWxmX0l42cHx or http://plnkr.co/edit/85fIXWxmX0l42cHx

PS: You'll need to re-position the circles as well.

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