daedelus_j daedelus_j - 3 months ago 13
Javascript Question

Trying to render data d3 crossfilter dc.js BarChart. The chart is drawn, but it's empty

I must be doing something naive, but I can't get my data to render within the chart.

here's my example data:

var data = [
{
date: '02-12-2013',
val: 13,
}
{
date: '02-13-2013',
val: 6,
}
...
]


here's my code:

var cf = crossfilter(data);
var dim = cf.dimension(function(d){ return d.val });
var group = dim.group();
var barchart = dc.barChart('#container');

barchart.width(850)
.height(250)
.margins({top: 10, right: 50, bottom: 30, left: 40})
.dimension(dim)
.group(group)
.gap(1)
.x(d3.time.scale()
.domain([new Date(data[0].date),
new Date(data[data.length-1].date)]))
.xUnits(d3.time.day)
.centerBar(true)
.renderHorizontalGridLines(true)
.renderVerticalGridLines(true)
.render();


What am I missing here? Here's the screenshot of the chart:

Here's the screenshot of the chart

Answer

Try to use date as your dimension:

var dim = cf.dimension(function(d){ return d.date });

For bar chart your dimension usually is what needs to be matched with your X axis scale.