Zero values

32 views
Skip to first unread message

sevil yılmaz

unread,
Jan 31, 2021, 7:40:49 AM1/31/21
to dc-js user group
Hello ,
I want to remove those with zero values from tables and charts. I also want it don't come when the filter is made.

I used the "remove_empty_bins" function but it doesn't seem to work. When I filter, values that are zero values come. I'm attach 2 different links to below. One is an example that is a table and the other is an example I made with a rowchart.


In the example which is a table, when I filtered from the select menu, the records with zero  value appears under the table.
When I filtered from the select menu in the the rowchart, there are zero values.

How can I make   zero values don't come in. I just want to show data that is greater than zero in the graph (also When I filtered).


Thanks,
Best Regards.
rowchart1.png
table1.png

Gordon Woodhull

unread,
Jan 31, 2021, 9:12:37 AM1/31/21
to dc.js user group
Hi Sevil,

You are on the right track. In the table example I guess there is a typo: you have >= when > is intended

function remove_empty_bins(source_group) {
  return {
    top: function(N) {
      return source_group.all().filter(function(d) {
        return d.value.totalAno > 0
      }).slice(0, N);
    }
  };
}
https://jsfiddle.net/gordonwoodhull/gs2mhf7o/2/

In the row chart case you have a specialized version of remove_empty_bins which looks at a field, but examining your data with console.log, I found that it was simple integer values, so the plain version will work:

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

Cheers,
Gordon


--
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dc-js-user-group/baac5a23-433a-44bf-953c-d111cfa17c59n%40googlegroups.com.
<rowchart1.png><table1.png>

sevil yılmaz

unread,
Jan 31, 2021, 1:57:08 PM1/31/21
to dc-js user group
Hi Gordon,

Thank you very much for your help. This works :))

But I have a problem. I understood why zero values come in my  project. Zero values come from formatting the numbers. 

For example, when I do not use formatting, when the filter is done, the value "1.8189894035458565e-12" comes like this, but when I format it sets it to 0 and shows it in the table.

I use d3.formatLocale when formatting. My formatting example is 124.254.452.You can see it in the example.I attach screenshots.

When formatted I don't want zero values.How can I do that? Do I need to change the formatting?

Thanks,
Cheers,
Sevil.

31 Ocak 2021 Pazar tarihinde saat 17:12:37 UTC+3 itibarıyla gor...@woodhull.com şunları yazdı:
tbl1.png
tbl2.png

Gordon Woodhull

unread,
Jan 31, 2021, 8:46:49 PM1/31/21
to dc.js user group
Hi Sevil,

This is also covered in the flat groups section of the FAQ:
Surprisingly, floating point numbers are not associative or distributive, so when you add and subtract the same numbers, the result is likely not be exactly zero. Since it's very small, when you format it to any reasonable precision, you get zero.

The fake group given there:
function snap_to_zero(source_group) {
    return {
        all:function () {
            return source_group.all().map(function(d) {
                return {key: d.key, 
                        value: (Math.abs(d.value)<1e-6) ? 0 : d.value};
            });
        }
    };
}
If you need both snap_to_zero and remove_empty_bins, you can wrap one with the other:

var nothing_tiny = remove_empty_bins(snap_to_zero(group));

or the logic can be combined.

Cheers,
Gordon
 

sevil yılmaz

unread,
Feb 1, 2021, 4:29:49 AM2/1/21
to dc-js user group
Hi Gordon,

Thank you so much.

Cheers,
Sevil

1 Şubat 2021 Pazartesi tarihinde saat 04:46:49 UTC+3 itibarıyla gor...@woodhull.com şunları yazdı:
Reply all
Reply to author
Forward
0 new messages