1

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:

  1. push zero into the frequencyArray?
  2. 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.

Community
  • 1
  • 1
  • 1
    Are you getting any errors in your console? I believe you would get a `Invalid left-hand side in assignment` error for setting `dates.indexOf(currentDate) = dates.indexOf(currentDate) || 0;`. Can you provide the rest of your code? Better yet, a [jsfiddle](http://jsfiddle.net)? – Dom May 16 '15 at 23:24
  • Thanks @Dom. No, no console errors. Please see update above. Thank you! –  May 17 '15 at 19:34
  • Please don't have an "update" section. It makes it hard to read the post. Rewrite it all as one coherent post. – Teepeemm May 17 '15 at 21:08

1 Answers1

0

You're getting NaN because of the following:

var index = dateArray.indexOf('a date that does not exist')   // -1
var el = cumulativeFrequencyArray[index]                      // undefined 
thousandSeparator(el)     // 'NaN' note that it's a string 'NaN', not the numeric NaN

You should do a linear search until the element is found or a greater date is found instead of indexOf, I'd suggest parsing the dates to numbers instead of dealing with them as strings

Mauricio Poppe
  • 4,817
  • 1
  • 22
  • 30
  • Thank you for the insight. Right, I should parse the dates to numbers, that would be better! If I do that, what I'm wondering now is if [getting a zero into the array where no date is found](http://stackoverflow.com/questions/14522667/how-to-configure-flot-to-draw-missing-time-series-on-y-axis-at-point-zero) would be better. I'd like to build time series graphics from this, so having a value for every date between first and last would be ideal. In this case the csv is 20,000 rows. So, would that make sense? –  May 17 '15 at 23:34
  • that's not part of the original question but it really depends on what you need to do, if you don't care about the additional space you could have an empty element for a date that is not in the rows or find the position of a day using binary search – Mauricio Poppe May 18 '15 at 20:51
  • Thank you. Yes, apologies that was an add on question. I'll take a look at those options you suggested, and write another post if I have more questions. And you did answer my original question, so I'm marking it answered. Thanks so much! –  May 18 '15 at 23:54