UPDATE: Added JSFIDDLE, and I updated the original values in this posting to match. The fiddle is not working because the csv is external. I tried this, but couldn't figure it out. But, at least you can see the code.
On my machine the console logs at lines 52-54 show:
dateArray ["10/17/0014", "10/18/0014", "10/20/0014", "10/21/0014"]
frequencyArray [3, 3, 4, 2]
cumulativeFrequencyArray [3, 6, 10, 12]
When the slider is at 10/19/2014, I want it to show 0 and 6. It's almost like I need somehow to identify dates with no data and either:
- push zero into the
frequencyArray
? - or I need
cumulativeFrequencyArray
to display its previous index?
I also don't like how I'm going to the external csv file twice, but that's another question :) Thanks!
ORIGINAL: Using D3, I have a csv of dates:
timeStamp
10/17/14 02:20:15 PM
10/17/14 08:22:35 AM
10/17/14 09:03:18 AM
10/18/14 02:20:15 PM
10/18/14 08:23:35 AM
10/18/14 09:03:18 AM
10/20/14 08:23:35 AM
10/20/14 10:23:35 AM
10/20/14 02:20:15 PM
10/20/14 02:03:18 AM
10/21/14 04:20:15 PM
10/21/14 09:03:18 AM
I'm making an array of the dates found
dateArray = ['10/17/14', '10/18/14', '10/20/14', '10/21/14']
I'm counting the number of occurrences of each date found an putting them into another array
frequencyArray = [3, 3, 4, 2];
I'm also using reduce()
on frequencyArray
to generate a third array of previous sums
cumulativeFrequencyArray = [3, 6, 10, 12];
I'm using a date slider to display the text of the values of frequencyArray
and cumulativeFrequencyArray
(when I slide to 10/21/14, the text displays "2" and "12"). I'm doing this by saying - "when the slider value equals a value in dateArray
, display the corresponding indexOf
frequencyArray
and cumulativeFrequencyArray
.
This works great until you slide to a date with no value, like 10/19/14, which displays "NaN" and "NaN". Fine. So, if no date is found, make NaN a zero
frequencyArray[indexOfFormattedCurrentDate] = frequencyArray[indexOfFormattedCurrentDate] || 0;
However instead of "0" and "6", the text displays "0" and "NaN". It nicely puts a zero for the frequencyArray
text, but still puts NaN in the cumulativeFrequencyArray
text.
How can I get the cumulativeFrequencyArray
text to put the sum of the previous values?
Thanks.