Created
July 13, 2012 03:29
-
-
Save rjurney/3102527 to your computer and use it in GitHub Desktop.
Fill in blanks in a time series in Javascript where the range of the data is "00"-"23"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"total":16,"sent_hour":"08"},{"total":16,"sent_hour":"09"},{"total":24,"sent_hour":"10"},{"total":14,"sent_hour":"11"},{"total":6,"sent_hour":"12"},{"total":22,"sent_hour":"13"},{"total":32,"sent_hour":"14"},{"total":14,"sent_hour":"15"},{"total":10,"sent_hour":"16"},{"total":10,"sent_hour":"17"},{"total":4,"sent_hour":"18"},{"total":8,"sent_hour":"20"},{"total":6,"sent_hour":"21"},{"total":20,"sent_hour":"22"},{"total":2,"sent_hour":"23"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"sent_hour":"00","total":0},{"sent_hour":"01","total":0},{"sent_hour":"02","total":0},{"sent_hour":"03","total":0},{"sent_hour":"04","total":0},{"sent_hour":"05","total":0},{"sent_hour":"06","total":0},{"sent_hour":"07","total":0},{"total":16,"sent_hour":"08"},{"total":16,"sent_hour":"09"},{"total":24,"sent_hour":"10"},{"total":14,"sent_hour":"11"},{"total":6,"sent_hour":"12"},{"total":22,"sent_hour":"13"},{"total":32,"sent_hour":"14"},{"total":14,"sent_hour":"15"},{"total":10,"sent_hour":"16"},{"total":10,"sent_hour":"17"},{"total":4,"sent_hour":"18"},{"sent_hour":"19","total":0},{"total":8,"sent_hour":"20"},{"total":6,"sent_hour":"21"},{"total":20,"sent_hour":"22"},{"total":2,"sent_hour":"23"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeHourRange(num) { | |
return num < 10 ? "0" + num.toString() : num.toString(); | |
} | |
function fillBlanks(ourHours, rawData) { | |
var ourData = Array(); | |
alert(JSON.stringify(rawData)); | |
for (hour in ourHours) | |
{ | |
var hourString = makeHourRange(hour); | |
var found = false; | |
for(x in rawData) | |
{ | |
if(rawData[x]['sent_hour'] == hourString) | |
{ | |
found = true; | |
break; | |
} | |
} | |
if(found == true) | |
{ | |
ourData.push(rawData[x]); | |
} | |
else | |
{ | |
ourData.push({'sent_hour': hourString, 'total': 0}) | |
} | |
} | |
return ourData; | |
} | |
var rawData = {{ chart_json|safe }}; | |
var hourRange = d3.range(0,24); | |
alert(hourRange); //["00","01",.."23"] | |
var filledData = fillBlanks(hourRange, rawData); | |
alert(JSON.stringify(filledData)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment