D3.js: 5942 Error: Invalid value for <g> attribute transform = "translate (NaN, 0)"
Using d3.js v3.55, crossfilter.js v1.311 and dc.js v1.73 and try to build a reduction function that returns the average value, not the amount or score. Reducing Sum, which is built-in, works fine, but when I try to add my own reduction function, based on many examples on the Internet. I get messages: d3.js: 5942 Error: invalid value for attribute transform = "translate (NaN, 0)" d3.js: 8718 Error: invalid value for attribute width = "NaN"
Here is code that can be reduced as far as I can, without exception of important information:
<script type="text/javascript">
//
// Create the Chart Objects
//
var GeoZoneChart = dc.rowChart("#chart-geo-zone");
var pdcData = [
{GeoZone: 'New York Metro', PDCLast365: 0.43},
{GeoZone: 'New York Metro', PDCLast365: 0.427},
{GeoZone: 'New York Metro', PDCLast365: 0.418},
{GeoZone: 'Los Angeles Metro', PDCLast365: 0.4085},
{GeoZone: 'Los Angeles Metro', PDCLast365: 0.40565},
{GeoZone: 'Chicago Metro', PDCLast365: 0.46789457},
{GeoZone: 'Chicago Metro', PDCLast365: 0.46366023},
{GeoZone: 'Chicago Metro', PDCLast365: 0.447781455}
];
//
// normalize/parse data
// in this case, turn the decimel into a percentage
pdcData.forEach(function(d) {
d.PDCLast365 = d.PDCLast365 * 100;
d.PDCLast365 = d.PDCLast365.toFixed(2);
});
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function reduceAddAvg(attr) {
return function(p,v) {
if (isNumeric(v[attr]) ){
++p.count;
p.sum += v[attr];
p.avg = p.sum/p.count;
}
return p;
};
}
function reduceRemoveAvg(attr) {
return function(p,v) {
if (isNumeric(v[attr]) ){
--p.count;
p.sum -= v[attr];
p.avg = p.sum/p.count;
}
return p;
};
}
function reduceInitAvg() {
return {count:0, sum:0, avg:0};
}
//
// set crossfilter
//
var ndx = crossfilter(pdcData),
GeoZoneDim = ndx.dimension(function(d) {return d.GeoZone;}),
PDCLast365PerGeoZone =
GeoZoneDim.group().reduce(
reduceAddAvg('PDCLast365'),
reduceRemoveAvg('PDCLast365'),
reduceInitAvg);
GeoZoneChart
.width(400).height(200)
.dimension(GeoZoneDim)
.group(PDCLast365PerGeoZone)
.elasticX(true);
dc.renderAll();
</script>
+4
2