1

Related to this entry. I am trying to create a rotated group with the dataset below. However, the reduce() functions do not seem to be getting called. I am a bit unclear as to why. Below is my code.

queue()
    .defer(d3.json, "assets/data/datatest1.json")
    .await(grpData);

function grpData(err, statData) {
    var ndx = crossfilter(statData);
    console.log(`Number of records in dataset: ${ndx.size()}`);

    buildStatYearSel(ndx);
}

function buildStatYearSel(ndx) {
    var dim = ndx.dimension(function(d) {return d.Year});
    var rotatedGrp = rotate(dim, ['Year', 'StatKey']);

    var foo = 1; // Nice place to set a break point
}

function rotate(dim, cols) {
    var _groupAll = dim.groupAll().reduce(
        function(p, v) {
            cols.forEach(function(c) { // add
                p[c] += v[c];
            });
            return p;
        },
        function(p, v) { // remove
            cols.forEach(function(c) {
                p[c] -= v[c];
            });
            return p;
        },
        function() { // init
            console.log("In init"); // never see this in console.
            var p = {};
            cols.forEach(function(c) {
                p[c] = 0;
            });
            return p;
        });
    return {
        all: function() {
            return d3.map(_groupAll.value()).entries();
        }
    };
}

The data looks like:

[
{"Year": "2002", "StatKey": "02437", "First": "108.26", "Tenth": "101.20", 
"Last": "81.73"},
{"Year": "2002", "StatKey": "102", "First": "81.24", "Tenth": "75.59", 
"Last": "50.37"},
{"Year": "2002", "StatKey": "326", "First": "76.47", "Tenth": "57.14", 
"Last": "5.88"},
{"Year": "2003", "StatKey": "02437", "First": "106.03", "Tenth": "101.79", 
"Last": "87.44"},
{"Year": "2003", "StatKey": "102", "First": "77.86", "Tenth": "73.95", 
"Last": "48.02"},
{"Year": "2003", "StatKey": "326", "First": "55.56", "Tenth": "47.50", 
"Last": "11.76"},
{"Year": "2004", "StatKey": "328", "First": "74.24", "Tenth": "68.65", 
"Last": "48.03"},
{"Year": "2004", "StatKey": "103", "First": "73.27", "Tenth": "69.55", 
"Last": "54.68"},
{"Year": "2004", "StatKey": "102", "First": "77.23", "Tenth": "73.13", 
"Last": "49.88"}
]

Any guidance is greatly appreciated.

Gordon
  • 19,811
  • 4
  • 36
  • 74
MichaelB
  • 175
  • 1
  • 10
  • Looks like I was able to add the data. ;-) Sorry for the unfriendly welcome to SO, everyone is trying to keep the quality of questions high, but they might not always know what they are looking at. I hope to give this a try soon, if no one else gets to it. – Gordon Mar 16 '19 at 17:48
  • I wouldn't expect the reduce functions to be called until `rotatedGrp.all()` is called - it's lazily evaluated when the chart draws. Did you try logging the result of `rotatedGrp.all()`? – Gordon Mar 16 '19 at 17:52
  • @Gordon: thank you for adding the data. Something you said caused me to tilt my head. I didn't think I had anything in `rotatedGrp` given I wasn't getting into the initialiser function. Because of your comment, after the call to `rotate()`, I put `console.log(rotatedGrp.all())` and all seemed to work! (the data isn't exactly the way I want, but I think I can figure that out). I have to profess though, I don't get it. I had already returned from the `rotate()` function when I made call to `rotatedGrp.all()` ?? – MichaelB Mar 17 '19 at 06:31
  • 1
    Right: the idea of a fake group is that only when its `.all()` method is called, it pulls the data and postprocesses it. This is what you want because the sequence is filter a chart -> redrawAll -> each chart calls `group.all()` and redraws. You wouldn't want to do the work up front because then it would get out of sync with the filters. So it makes sense to do it on demand, "lazily". (Might not make a if you're not filtering, but it doesn't hurt.) – Gordon Mar 17 '19 at 14:01
  • Ok, got it. Really appreciate it! – MichaelB Mar 19 '19 at 05:02

0 Answers0