Reputation: 85
I have a large JSON file with values similar to this example:
[
{"date": "12/27/2012", "temp": 23, "pH": ".4"},
{"date": "12/27/2012", "temp": 23, "pH": ".7"},
{"date": "12/27/2012", "temp": 23, "pH": ".2"},
{"date": "12/27/2012", "temp": 23}
]
There will be instances where the pH value will not exist. I need to keep track on the page of how many instances of "temp" and "pH" there are. I don't have a problem doing this with a JavaScript loop, but I'd also like to see if I can do it with crossfilter.
Basically, is there something similar to groupAll().reduceCount().value(), that counts just the individual instances of the strings?
Here's a sample of the code inside the d3.json() block:
var tempDim = xFilter.dimension(function(d) {return d.temp;});
var tempGroup = tempDim.group().reduceCount(function(d) {return d.temp;});
var tempCount = tempDim.groupAll().reduceCount().value();
console.log("tempCount :"+tempCount); // 4
var tempSum = tempDim.groupAll().reduceSum(function(d) {return d.temp;}).value();
console.log("tempSum :"+tempSum); // 92
//pH
var phDim = xFilter.dimension(function(d) {return d.pH;});
var phGroup = phDim.group().reduceCount(function(d) {return d.pH;});
var pHCount = phDim.groupAll().reduceCount().value();
console.log("pHCount :"+pHCount); // Equal to 4, looking for 3
Upvotes: 0
Views: 3713
Reputation: 20150
Or, if you need a count of the number of values whether or not they're distinct, you could use a custom reduce function, something like:
tempDim.groupAll().reduce(
function(p,v) { return (v.temp !== undefined) ? p+1 : 0; },
function(p,v) { return (v.temp !== undefined) ? p-1 : 0; },
function() { return 0; })\
Then you can generalize using the reusable reduce function pattern.
Upvotes: 0
Reputation: 6010
Won't this get you the number of distinct temps and phs?
var tempDim = xFilter.dimension(function(d) {return d.temp;});
var tempGroup = tempDim.group();
console.log(tempGroup.top(Infinity).length);
var phDim = xFilter.dimension(function(d) {return d.pH;});
var phGroup = phDim.group()
console.log(phGroup.top(Infinity).length);
Upvotes: 1