1
votes

I have a piechart I'm displaying using the following dimension -

var types = facts.dimension(function (d) {
   if (d.types === 2)
      return "Type 2";
   else if (d.types === 3)
      return "Type 3";
   else
     return "Other";
 });

I would like to not return, ignore, all other types, so the pie chart would just display Type 2 and Type 3. I cannot seem to get this working though I'm sure it's simple. Can I do something within that dimension or do I need to filter before?

Thanks for any help.

2
What do you want to do with the rows that are not Type 2 or Type 3? Do you want them to display in other charts, or do you want them to be dropped from all charts as well as the pie? - Gordon
I ended up using an fake group and getting rid of the unwanted results there. - NorthSide
Nice. That's where I was going with the first suggestion, but I wasn't sure which you were asking. - Gordon

2 Answers

1
votes

Have you tried creating a new type and then creating a dimension on top of that?

facts.forEach(function(d) {
  if (d.types == 2) {
     d._type = "Type 2";
  } else if (d.types == 3) {
     d._type = "Type 3";
  } else {
     d._type = "Other";
  }
});

var types = facts.dimension(dc.pluck('_type'))
1
votes

You need to (1) Filter your data then (2) remove it from the bin. Run your data through ensure_group_bins(myGroup) you'll get the chart you're after

function remove_empty_bins(source_group) {
return {
    all:function () {
        return source_group.all().filter(function(d) {
            return d.key != "unwanted" ;
        });
    }
};}

function ensure_group_bins(source_group, bins) {
return {
    all:function () {
        var result = source_group.all().slice(0), // copy original results (we mustn't modify them)
            found = {};
        result.forEach(function(d) {
            found[d.key] = true;
        });
        bins.forEach(function(d) {
            if(!found[d])
                result.push({key: d, value: 0});
        });
        return result;
    }
};};

dc.js Github