Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a histogram using d3.js and crossfilter data?

Here's my demo (permanent link; might take a second to load).

I'm having a few problems with it:

  1. The crossfilter key does not reflect the upper boundary. Therefore, the x-axis is missing one step.

    enter image description here

    I managed to fix it by getting one group-step size (group.all()[1] - group.all()[0]) and then adding it to the d3.extent(group.all())[1]. Because it is a dirty fix, I did not include this to the demo (there might by a data gap). What is a less dirty way to get the group-step size/fix the x-axis?

  2. There are uneven gaps between the bars. I can't figure out how to fix x.scale inconsistency with different data sets.

    enter image description here

    I want the bar-width to adapt automatically to the available graph width. Therefore, I calculate the optimal bar width and then (if needed) resize the graph itself:

    bar_width = math.round(graph_width/data_length);
    graph_width = bar_width*data_length;
    

    Under this condition, there is no way x.scale can return uneven data for any key value, yet it does. There is no magic; I am simply overlooking something. But what is it?

    When x.scale is messed then deducting the bar_width from the graph_width before setting the x.scale range fixes the problem. Though, I can't figure out the condition to detect when x.scale is a not acting as expected.

like image 305
Gajus Avatar asked Nov 04 '22 17:11

Gajus


1 Answers

I have spent a lot of time trying to figure the right way to do this. However, I ended up using simple math to guess the upper data boundary. There are many problems that yet remain to be solved, such as how to visually handle extremes.

enter image description here

My attempt to develop a reusable histogram library is accessible on github.

like image 59
Gajus Avatar answered Nov 16 '22 17:11

Gajus