简体   繁体   English

dc.js渲染条形图

[英]dc.js render bar chart

I'm trying to render a bar chart using the dc.js library. 我正在尝试使用dc.js库渲染条形图。 My csv dataset is formatted like so: 我的csv数据集的格式如下:

q, year, category, subcategory, total, q,年份,类别,子类别,总数,
q1, 2010, x, xa, 200 q1,2010,x,xa,200
q2, 2010, y, yb, 100 q2,2010,y,yb,100
q3, 2010, x, xa, 300 q3,2010,x,xa,300
q4, 2010, z, zb, 45 q4,2010,z,zb,45
q1, 2010, x, xa, 80 q1,2010,x,xa,80
q2, 2010, y, yb, 200 q2,2010,y,yb,200
q3, 2010, x, xc, 301 q3,2010,x,xc,301
q4, 2010, z, za, 205 q4,2010,z,za,205
q1, 2011, x, xa, 80 q1,2011,x,xa,80
q2, 2011, y, yb, 200 q2,2011,y,yb,200
q3, 2011, x, xc, 301 q3,2011,x,xc,301
q4, 2011, z, za, 205 q4,2011,z,za,205

So far, I'm able to get a bar chart, but the actual data isn't rendered to the chart, also the scaling on the x-axis is off as well, since it should be according to years. 到目前为止,我能够获得一个条形图,但实际数据没有呈现给图表,x轴上的缩放也是关闭的,因为它应该是根据年份。 I'm having difficulty appending the data to the graph. 我很难将数据附加到图表中。 This is all that I've been able to get 这就是我能得到的一切

在此输入图像描述

I'm loading in the data through d3.csv as follows: 我通过d3.csv加载数据如下:

d3.csv("records.csv", function(csv) {
    var data = crossfilter(csv);

    var fiscalyear = data.dimension(function (d) {
        return d.year;
    });

    var spendGroup = fiscalyear.group().reduce(
        function(p,v) {
            return p.total += v.total;
        },
        function(p,v) {
            return p.total -= v.total;
        },
        function() {
            return  {totalSpend:0};
        }
    );

fiscalyearchart.width(600)
    .height(300)
    .margins({top: 10, right: 50, bottom: 30, left: 60})
    .dimension(fiscalyear)
    .group(spendGroup)
    .x(d3.scale.linear().domain([2010,2013]))
    .renderHorizontalGridLines(true)
    .centerBar(true)
    .elasticY(true)
    .brushOn(true);

    dc.renderAll();

});

The default valueAccessor determines the height of each column on Y axis. 默认值valueAccessor确定Y轴上每列的高度。 By default it assumes you it can use the value returned by your crossfilter group. 默认情况下,它假定您可以使用crossfilter组返回的值。 So it expects something like: 所以它期望类似于:

console.log(group.all())

// [{key: 2010, value: 1},
//  {key: 2011, value: 2}]

But instead you are using a custom reduce function returning an object. 但相反,您正在使用返回对象的自定义reduce函数。

Your objects will look like this: 您的对象将如下所示:

console.log(group.all())

// [{key: 2010, value: {total:NaN, totalSpend:0}},
//  {key: 2011, value: {total:NaN, totalSpend:0}}]

DC doesn't know what do with values like: {total:NaN, totalSpend:0} . DC不知道如何使用以下值: {total:NaN, totalSpend:0} You need to pass a valueAccessor to extract a numeric value like 您需要传递valueAccessor来提取数字值

chart.valueAccessor(function(d) {return d.totalSpend;});

Also, I presume you have a typo and really want to use p.totalSpend rather than p.total . 另外,我认为你有一个拼写错误,并且真的想要使用p.totalSpend而不是p.total But you could simplify further by just doing: 但是你可以通过以下方式进一步简化:

var spendGroup = fiscalyear.group().reduce(
    function(p,v) {
        return p += v.total;
    },
    function(p,v) {
        return p -= v.total;
    },
    function() {
        return 0;
    }
);

And this will return a .valueAccessor function dc can understand without customization. 这将返回一个.valueAccessor函数dc可以理解而无需自定义。 Even easier though, crossfilter already has a convenience function for this: 更简单的是,crossfilter已经为此提供了便利功能:

var spendGroup = fiscalyear.group().reduceSum(function(d) {return d.total;});

And DC has utility to create the callback: DC具有创建回调的实用程序:

var spendGroup = fiscalyear.group().reduceSum(dc.pluck('total'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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