adamist521
adamist521

Reputation: 33

Handling two different data with Crossfilter and make chart

I have two data, one with time dependent and one not.

The data which is time dependent would be like below.

year,  name    , value
2001, "AAAAAAA", 200
2002, "AAAAAAA", 300
2003, "AAAAAAA", 400
2001, "BBBBBBB", 150
2002, "BBBBBBB", 250
2000, "CCCCCCC", 500
2001, "CCCCCCC", 600
2002, "CCCCCCC", 550
2001, "DDDDDDD", 100
2002, "DDDDDDD", 300

On the other hand, I have data independent to time which describes property of names.

 name      ,  type
"AAAAAAAA", "red"
"BBBBBBBB", "blue"
"CCCCCCCC", "green"
"DDDDDDDD", "red"

One way to handle data would be to hold data as following.

year,  name    , value,  type
2001, "AAAAAAA", 200  , "red"
2002, "AAAAAAA", 300  , "red"
2003, "AAAAAAA", 400  , "red"
2001, "BBBBBBB", 150  , "blue"
2002, "BBBBBBB", 250  , "blue"
2000, "CCCCCCC", 500  , "green"
2001, "CCCCCCC", 600  , "green"
2002, "CCCCCCC", 550  , "green"
2001, "DDDDDDD", 100  , "red"
2002, "DDDDDDD", 300  , "red"

If you want to handle data on crossfilter, is the above data format the best way to handle it? Or could you handle it separately which is more data efficient?

Related to the data format, how could I make chart with type in the X-axis and average of the names value's average in the Y-axis. Just using type as a dimension and calculating average would end up in averaging all result at once. I assume it would be normal to calculate average first with names then average again afterward.

Upvotes: 1

Views: 140

Answers (1)

Gordon
Gordon

Reputation: 20120

Regarding the average-of-averages, I'd suggest something like this (untested).

Basically, keep a running sum and count for each name, and then when you need the average-of-averages, calculate all the averages and sum them, and divide by the number of names.

group.reduce(function(p, v) { // add
    p.sum[v.name] = (p.sum[v.name] || 0) + v.value;
    p.count[v.name] = (p.count[v.name] || 0) + 1;
    return p;
}, function(p, v) { // remove
    p.sum[v.name] -= v.value;
    p.count[v.name]--;
    return p;
} function() { // init
    return {sum: {}, count: {}};
});

chart.valueAccessor(function(kv) {
    var names = Object.keys(kv.value.sum),
        sumavg = 0;
    names.forEach(function(name) {
        sumavg += kv.value.sum[name] / kv.value.count[name];
    });
    return sumavg / names.length;
});

Upvotes: 2

Related Questions