最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - dc.js render bar chart - Stack Overflow

programmeradmin0浏览0评论

I'm trying to render a bar chart using the dc.js library. My csv dataset is formatted like so:

q, year, category, subcategory, total,
q1, 2010, x, xa, 200
q2, 2010, y, yb, 100
q3, 2010, x, xa, 300
q4, 2010, z, zb, 45
q1, 2010, x, xa, 80
q2, 2010, y, yb, 200
q3, 2010, x, xc, 301
q4, 2010, z, za, 205
q1, 2011, x, xa, 80
q2, 2011, y, yb, 200
q3, 2011, x, xc, 301
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. 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("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();

});

I'm trying to render a bar chart using the dc.js library. My csv dataset is formatted like so:

q, year, category, subcategory, total,
q1, 2010, x, xa, 200
q2, 2010, y, yb, 100
q3, 2010, x, xa, 300
q4, 2010, z, zb, 45
q1, 2010, x, xa, 80
q2, 2010, y, yb, 200
q3, 2010, x, xc, 301
q4, 2010, z, za, 205
q1, 2011, x, xa, 80
q2, 2011, y, yb, 200
q3, 2011, x, xc, 301
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. 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("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();

});
Share Improve this question edited Oct 8, 2013 at 0:52 user1333781 asked Oct 8, 2013 at 0:40 user1333781user1333781 1072 gold badges4 silver badges10 bronze badges 5
  • 1 what result you want ? – Nitish Kumar Commented Oct 8, 2013 at 6:28
  • i'm looking to get the total pull per quarter per year in the bar chart.. so should be about 4 bars per year interval – user1333781 Commented Oct 8, 2013 at 7:00
  • can you give sample chart image or example – Nitish Kumar Commented Oct 8, 2013 at 7:05
  • 1 see this plnkr.co/edit/X5bDDDEr6GHvm7CmXSGe?p=preview is it useful for you ? – Nitish Kumar Commented Oct 8, 2013 at 7:44
  • makes sense, but i'm trying to apply the dc.js library to the dataset, pivoting the data around a quarter dimension within a fiscal year dimension. the graph is the end result that i'm looking for, but not in the way that i want it to be rendered/drafted up – user1333781 Commented Oct 8, 2013 at 8:08
Add a ment  | 

1 Answer 1

Reset to default 7

The default valueAccessor determines the height of each column on Y axis. By default it assumes you it can use the value returned by your crossfilter group. 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.

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}. You need to pass a valueAccessor to extract a numeric value like

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. 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. Even easier though, crossfilter already has a convenience function for this:

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

And DC has utility to create the callback:

var spendGroup = fiscalyear.group().reduceSum(dc.pluck('total'));
发布评论

评论列表(0)

  1. 暂无评论