javascript - dc.js 按 y 轴/值排序顺序折线图

标签 javascript d3.js charts dc.js

我有一个 dc.js 序数图表,其 x 轴由诸如“化妆品”之类的内容组成,y 轴是销售数量。我想按销售额减少对图表进行排序,但是当我使用 .ordering(function(d){return -d.value.ty}) 时,折线图的路径仍按 x 排序-轴。 Mangled Graph

var departmentChart = dc.compositeChart('#mystore_department_chart'),
                ndx = crossfilter(response.data),
                dimension = ndx.dimension(function(d) {return d.name}),
                group = dimension.group().reduce(function(p, v) {
                    p.ty += v.tyvalue;
                    p.ly += v.lyvalue;
                    return p;
                }, function(p, v) {
                    p.ty -= v.tyvalue;
                    p.ly -= v.lyvalue;
                    return p;
                }, function() {
                    return {
                        ty: 0,
                        ly: 0
                    }
                });

            departmentChart
                .ordering(function(d){return -d.value.ty})
                //dimensions
                //.width(768)
                .height(250)
                .margins({top: 10, right: 50, bottom: 25, left: 50})
                //x-axis
                .x(d3.scale.ordinal())
                .xUnits(dc.units.ordinal)
                .xAxisLabel('Department')
                //left y-axis
                .yAxisLabel('Sales')
                .elasticY(true)
                .renderHorizontalGridLines(true)
                //composition
                .dimension(dimension)
                .group(group)
                .compose([
                    dc.barChart(departmentChart)
                        .centerBar(true)
                        .gap(5)
                        .dimension(dimension)
                        .group(group, 'This Year')
                        .valueAccessor(function(d) {return d.value.ty}),

                    dc.lineChart(departmentChart)
                        .renderArea(false)
                        .renderDataPoints(true)
                        .dimension(dimension)
                        .group(group, 'Last Year')
                        .valueAccessor(function(d) {return d.value.ly})
                ])
                .brushOn(false)
                render();

最佳答案

这是我最终做的黑客攻击。请注意,它可能会在大型数据集上出现性能问题,因为 all() 比 top(Infinity) 更快。出于某种原因,我无法获得 Gordon's answer工作,但理论上它应该。

在我的组中我指定了一个排序函数

group.order(function(p) {
    return p.myfield;
});

然后因为 all() 不使用排序函数,所以我覆盖了默认的 all 函数

group.all = function() {
    return group.top(Infinity);
}

然后在我的图表上我必须指定一个订单函数

chart.ordering(function(d){
    return -d.value.myfield
}); // order by myfield descending

关于javascript - dc.js 按 y 轴/值排序顺序折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23774871/

相关文章:

javascript - 如何使用 jQuery 将行追加到表中?

javascript - twitter bootstrap 选项卡和 getBBox

excel - 使用 mousemove 在用户窗体中显示图表坐标

javascript - 显示单个类别 Axis For Amchart 多面板水平图

javascript - 如何在html表单 Action 中添加参数?

javascript - Node js session 覆盖我的第一个 session

javascript - 如何在 jquery 评级星中使用字体 Awesome 而不是图像

javascript - 如何设置 D3 Zoom Packed Circle children 的颜色

javascript - D3 - 堆栈函数条形图过滤器

javascript - nvd3 格式化日期始终返回 1970-01-01