Creation of histogram with ChartJS #10699
Tristan1551
started this conversation in
Ideas
Replies: 1 comment
-
Is it possible to show specification limits on the histogram? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
In a personnal projet i wanted to create histograms.
I mainly use ChartJS but there is no option for native histograms.
So i tried to use the ones from Plotly, but i was not happy with the results.
I then tried to do it with chartjs but i could not manage to find any relevant information on how to get unique values, count it, or create groups from the values and then count the occurence like excel of plotly would do but automaticaly.
So i figured a way to create automatic histograms with raw data arrays with editable intervals and grouping. I hope it will help.
The code is able to
The first step is to declare my dataset
Here is a dataset containing a large range of data with a normal distribution
var numbers = [2.1,2.2,2.3,2.3,2.5,2.6,2.6,2.8,2.8,2.8,2.9,3,3,3.2,3.3,3.4,3.4,3.4,3.5,3.6,3.6,3.6,3.6,3.7,3.9,3.9,4,4.1,4.1,4.179,4.2,4.4,4.418,4.5,4.5,4.537,4.6,4.6,4.6,4.7,4.7,4.8,4.9,5.1,5.2,5.2,5.3,5.3,5.4,5.4,5.4,5.4,5.4,5.5,5.5,5.5,5.5,5.6,5.6,5.6,5.6,5.7,5.7,5.7,5.7,5.7,5.8,5.8,5.8,5.8,5.8,5.8,5.8,5.8,5.8,5.9,5.9,5.9,5.97,6,6,6,6,6,6,6,6,6.1,6.1,6.1,6.1,6.2,6.2,6.2,6.2,6.2,6.3,6.3,6.3,6.3,6.3,6.4,6.4,6.4,6.4,6.4,6.5,6.5,6.5,6.5,6.6,6.6,6.6,6.6,6.7,6.7,6.7,6.7,6.7,6.7,6.7,6.8,6.8,6.8,6.8,6.9,6.9,6.9,6.9,6.9,7,7.1,7.1,7.1,7.2,7.2,7.3,7.3,7.3,7.3,7.4,7.4,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.7,7.7,7.7,7.7,7.7,7.7,7.8,7.8,7.8,7.8,7.8,7.8,7.9,7.9,7.9,7.9,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9,12,12,12.1,12.1,12.2,12.2,12.3,12.3,12.7,12.7,12.8,12.8,12.9,12.9,12.9,13.2,13.2,13.3,13.4,13.6,13.6,13.7,14.1,14.6,14.8,15.1,15.1,15.6,16,16.2,17,17.1,17.7];
Because there is a lot of values and i need the graph to automaticaly create groups with a specific interval here i choose to have 0.5 as interval.
Var inter = 0.5;
Then i group my list by using the interval i choose (here 0.5).
After that i keep only the first and last element of each group created and separate each first and last with a "-".
Now i push the group name into each element of my dataset
Now i get the unique values of the pushed grouped dataset
And then i count the occurence of each unique in my pushed group
And now i can display my graph using with X the unique values and with y the count
Here the result
You can find the code here https://jsfiddle.net/tristanmrrpm/72c6pg4a/3/
Beta Was this translation helpful? Give feedback.
All reactions