coder
coder

Reputation: 85

How can hide dc.js row chart values if values equal zero

How can we hide if row chart values equal zero in dc.js after filtering .We have a code like this:

    var kurum=data.dimension(function(d){return ""+ d.KURUM;});
    var kurumGroup=kurum.group().reduceSum(function(d){return +d.INSIDANS});

    kurumRowMapChart
                    .width(300)
                    .height(200)
                    .margins({top: 5, left: 10, right: 10, bottom: 20})
                    .dimension(kurum)
                    .group(kurumGroup)
                    .colors(d3.scale.category10())
                    .elasticX(true)
                    .ordering(function(d) { return -d.value })
                    .xAxis().ticks(4);

This code works normally but we want to hiding when has filter if values equal zero.

Thanks

Upvotes: 4

Views: 3131

Answers (2)

Marcel Schürmann
Marcel Schürmann

Reputation: 395

@Gordon: Great answer! remove_empty_bins() worked for me! :)

I had to use d.value.count instead of d.value but I assume this depends on which group-reduce function you are using:

return d.value.count !== 0;

Upvotes: 0

Gordon
Gordon

Reputation: 20120

You can create a "fake group" which removes the bins containing zeros when .all() is called:

function remove_empty_bins(source_group) {
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                return d.value != 0;
            });
        }
    };
}

using it like this:

var filtered_group = remove_empty_bins(kurumGroup);

kurumRowMapChart.dimension(kurum)
    .group(filtered_group)
    ...

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

The idea is to create an object which sits between crossfilter and dc.js which looks like a crossfilter group and filters it on demand.

Here is a somewhat complicated example intended to test the transitions between varying numbers of bars:

http://dc-js.github.io/dc.js/transitions/ordinal-row-transitions.html

(Currently the transitions are not very good but it demonstrates remove_empty_bins well.)

Upvotes: 8

Related Questions