UPDATE: I have added a JSFIDDLE link and adjusted the original values in this post to match. However, the fiddle is not functioning properly due to the external CSV file. I attempted a solution found on this thread, but was unsuccessful. Nevertheless, you can still view the code.
On my system, the console logs at lines 52-54 display:
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 set to 10/19/2014, I want it to show 0 and 6. It seems like I need to find a way to identify dates with no data and either:
- Add zero into the
frequencyArray
? - Have the
cumulativeFrequencyArray
display its previous index?
I am also concerned about accessing the external CSV file twice, but that's a topic for another discussion :) Thank you!
ORIGINAL: Using D3, I imported a CSV file containing 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 created an array of the dates discovered
dateArray = ['10/17/14', '10/18/14', '10/20/14', '10/21/14']
The number of occurrences of each date were counted and stored in another array
frequencyArray = [3, 3, 4, 2];
Additionally, I applied reduce()
on frequencyArray
to produce a third array with cumulative sums
cumulativeFrequencyArray = [3, 6, 10, 12];
A date slider is used to exhibit the text values of frequencyArray
and cumulativeFrequencyArray
(when sliding to 10/21/14, the text shows "2" and "12"). The logic involves checking if the slider value matches a value in dateArray
, then displaying the corresponding indexOf
values from frequencyArray
and cumulativeFrequencyArray
.
This method works well until reaching a date without a value, such as 10/19/14, which displays "NaN" and "NaN". To handle this scenario, I attempt to change NaN to zero
frequencyArray[indexOfFormattedCurrentDate] = frequencyArray[indexOfFormattedCurrentDate] || 0;
However, instead of showing "0" and "6", the text displays "0" and "NaN". While it successfully replaces NaN in the frequencyArray
text, NaN remains in the cumulativeFrequencyArray
text.
How can I ensure the cumulativeFrequencyArray
text reflects the sum of the preceding values?
Thank you.