I want to use the Charts.js Library and make the data displayed in the graph dependent on what a user has entered.
Right now I am not quite sure how to get the data in the right format so that I can use if for the library.
Currently I have a problem when combining the labels and values so that it matches the right index for the right label. I am having a table with two values ( A Month (Specified as Jan,Feb,Mar,Apr.etc.) and a numeric Value)
Now I need to combine months that appear twice and add the values associated with each month. I dont really know if I should use a 2d Array or 2 arrays for this. For example
var labels = ["Jan 17", "Jan 17", "Feb 17", "Mar 17", "Mar 17"];
var values = [10,10,25,40,100];
becomes
var newlabels = ["Jan 17", "Feb 17", "Mar 17"];
var newvalues = [20,25,140];
or as 2D Array:
var graphdata = [["Jan 17", 10],["Jan 17",10],["Feb 17",25],["Mar 17",40],["Mar 17",100]];
becomes
var newgraphData =[["Jan 17", 20],["Feb 17",25],["Mar 17",140]];
Either way is fine but I dont know which to use. Also I wouldn't quite know how to get the data in a 2d Array with jQuery selectors and the .map() function.
I was thinking of using the .map() and .reduce() function with a loop to go through each index but I cant quite seem to figure out how to do it.
[{ 'label': 'Jan 17', val: '100'}, ....]?