NorthSide
NorthSide

Reputation: 1499

dc.js - pieChart ignore unwanted values

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.

Upvotes: 1

Views: 567

Answers (2)

Ryan
Ryan

Reputation: 427

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

Upvotes: 1

helios
helios

Reputation: 859

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'))

Upvotes: 1

Related Questions