Histogram in JavaScript?

前端 未结 2 526
清歌不尽
清歌不尽 2021-01-02 15:06

I have this dataset for income:

Income      Number of people
0           245981
8.8         150444
30          126063
49.9        123519
70          115029
90.7           


        
相关标签:
2条回答
  • 2021-01-02 15:46

    If you just want or need to sort the data into bins, without plotting take a look at histogram.js.

    0 讨论(0)
  • 2021-01-02 16:00

    The existing histogram examples are based on computing the histogram from samples, say if you had a list of individual people and their incomes. In this case, you already have the data for the histogram—you just want to display it.

    The tricky thing here is that your histogram has variable-width bins. The first thing you can do is ignore the variable-width of each bin and just display a simple lollipop chart. The x-axis is a linear scale for income, and the y-axis is a linear scale for count of people:

    http://bl.ocks.org/1624656

    If you want to convert this to a histogram, you can't just replace those fixed-width lines with variable-width bars; you need to normalize the data so that the area of the bar encodes the frequency of people with that income. Therefore, the width of the bar is the income range (such as from 0 to 8.8 for the first bin), and the height of the bar is the quantity of people divided by the width. As a result, the area (width × height) is proportional to the number of people. That looks like this:

    http://bl.ocks.org/1624660

    0 讨论(0)
提交回复
热议问题