Reputation: 39
I am new to Crossfilter and JS. I can't figure out why this barchart won't render. I've tried several date formats. Link to my jsfiddle is below. Any help, please?
It is basically reading a loan dataset in JSON format and attempting to barchart the count by day over time.
https://jsfiddle.net/5q1dddgt/2/
<div id="time-chart"></div>
/**
* Created on 3/13/17.
*/
var mortgageJson = [{
"index": 0,
"full_fips": "04013",
"defaulted_balance": 0.0,
"original_balance": 148000.0,
"county_name": "Maricopa County",
"state": "AZ",
"oltv": 79,
"dti": 30.0,
"originationMth": "1999-02-01T00:00:00.000Z",
"defaultRate": 0.0
}, {
"index": 1,
"full_fips": "04021",
"defaulted_balance": 0.0,
"original_balance": 148000.0,
"county_name": "Pinal County",
"state": "AZ",
"oltv": 79,
"dti": 30.0,
"originationMth": "1999-02-01T00:00:00.000Z",
"defaultRate": 0.0
}];
var mortgageSummary = mortgageJson;
var dateformat=d3.time.format("%m-%d-%Y");
mortgageSummary.forEach(function(d) {
d.originationMonth = new Date(d.originationMth).toLocaleDateString("en-GB");
});
var ndx = crossfilter(mortgageSummary);
var dateDim = ndx.dimension(function(d) {
return d["originationMonth"];
});
var originationByTime = dateDim.group().reduceCount(function(d) { return d.originationMonth;});
var totalOrigination = ndx.groupAll().reduceSum(function(d) {
return d["original_balance"];
});
var max_county = originationByTime.top(1)[0].value;
var minDate = dateDim.bottom(1)[0]["originationMonth"];
var maxDate = dateDim.top(1)[0]["originationMonth"];
console.log(minDate);
console.log(maxDate);
var timeChart = dc.barChart("#time-chart");
timeChart
.width(600)
.height(160)
.margins({
top: 10,
right: 50,
bottom: 30,
left: 50
})
.dimension(dateDim)
.group(originationByTime)
.transitionDuration(500)
.x(d3.time.scale().domain([minDate, maxDate]))
.xUnits(d3.time.days)
.elasticX(true)
.elasticY(true)
.xAxisLabel("Year")
.yAxis().ticks(4);
dc.renderAll();
Upvotes: 1
Views: 152
Reputation: 6010
Take a look at the browser console and you'll see some of the problems. Your inclusion of firebug-lite is causing lots of errors. I'd recommend you just use the developer tools included in your browser.
Other changes:
You want an actual date object as your dimension rather than a string, so I changed
mortgageSummary.forEach(function(d) {
d.originationMonth = new Date(d.originationMth).toLocaleDateString("en-GB");
});
to
mortgageSummary.forEach(function(d) {
d.originationMonth = new Date(d.originationMth);
});
And group.reduceCount
doesn't take any arguments (reduceSum
does), so this will work fine:
var originationByTime = dateDim.group().reduceCount();
I also included dc.css as a dependency, which helps a lot with formatting.
Here's the working example: https://jsfiddle.net/5q1dddgt/4/
Upvotes: 2