0

I'm plotting a basic bar chart with ordinal x-axis and metric plotted in y-axis. Now, it works good using this post

However, now I want to select only specific data points to show. Example : if my ordinal x-axis has - a, b & c. But, I need to show only - b & c.

Issue :

When I'm adding only b & c in the domain using the following code, even a is showing up in the background.

Code :

.x(d3.scale.ordinal().domain(["b", "c"])) // Need the empty val to offset the first value
.xUnits(dc.units.ordinal)

I have made a fiddle to show the issue here

Another place for more visibility would be the link here (Look for ordinal bar chart at the bottom - starting from California, London is excluded but still in background)

Is there a fix for this yet/or any workaround? Any approach/suggestions will be highly appreciated

Community
  • 1
  • 1
Pravin
  • 461
  • 5
  • 26

1 Answers1

1

I think this has been asked before but I couldn't find it in a quick search.

This is the kind of thing you want to use a fake group for, because it is changing the shape of the data. The crossfilter group will behave as normal but there will be an intervening object which will intercept the results and remove particular bins:

function remove_bins(source_group) { // (source_group, bins...}
    var bins = Array.prototype.slice.call(arguments, 1);
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                return bins.indexOf(d.key) === -1;
            });
        }
    };
}

Add it to your usual crossfilter code like this:

var ndx = crossfilter(...)
var dim = ndx.dimension(...)
var group = dim.group(...) ... 

var filtered_group = remove_bins(group, 'a');

chart.dimension(dim)
    .group(filtered_group)
    ...

https://github.com/dc-js/dc.js/wiki/FAQ#remove-particular-bins

Gordon
  • 19,811
  • 4
  • 36
  • 74
  • Got it, thanks! Working fiddle : http://jsfiddle.net/tbamW/232/ However, we are adding the bins we want in the statement (var filtered_group = remove_bins(group, 'a');) and not the vice-versa. Isn't this the opposite technically? – Pravin Jan 11 '16 at 12:16
  • Huh. I guess the function is backwards, should be `===`. I'll correct it in my answer and in the FAQ. thanks! – Gordon Jan 11 '16 at 15:04
  • Sure, that worked now. Do update in FAQ for the larger public. – Pravin Jan 12 '16 at 09:05