Question

I have some data that has duplicate dates in it, but the values will always be the same - how do I count occurrence only once and reflect it on a bar chart?

I'm guessing I need to setup a filter function on grouping, I'm not sure how to go about this.

The data:

var jsonStr = ([
   {"date": "2011-11-14", "value": 0.1121},
   {"date":"2011-11-25", "value": -0.0231},
   {"date":"2011-11-25", "value": -0.0231},
   {"date":"2011-11-25", "value": -0.0231}
]);

The code:

//setup dimensions
var dateDimension = ndx.dimension(function (d) {
    return d.value;
});

var fluctuation = ndx.dimension(function (d) {
    return Math.round(d.value *10)/10;
});

var fluctuationGroup = fluctuation.group();

fiddle: http://jsfiddle.net/wbott70/xtjJn/

Was it helpful?

Solution

This probably isn't the most efficient way to do this, but using a map is simple and short:

var uniqueValues = d3.map([])
jsonStr.forEach(function(d){ uniqueValues.set(d.date, d); });

newJsonStr = []
uniqueValues.forEach(function(d){ newJsonStr.push(uniqueValues.get(d)); });

The result:

[{"date":"2011-11-14","value":0.1121},{"date":"2011-11-25","value":-0.0231}]

OTHER TIPS

The better would probably be to remove the duplicates in the data (if you never need this information), before using the Crossfilter library, using the following code, just after your definition of jsonStr:

var newJson = [];
for (var i = 0; i < jsonStr.length; i++) {
    var found = false;
    for (var j = 0; j < newJson.length; j++) {
        if (jsonStr[i].date === newJson[j].date) {
            found = true;
            break;
        }
    }
    if (!found) {
        newJson.push(jsonStr[i]);
    }
}
jsonStr = newJson;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top