Last active
November 30, 2020 09:49
-
-
Save mbostock/5827353 to your computer and use it in GitHub Desktop.
Counting Weekdays
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
weekday = (function() { | |
// Returns the weekday number for the given date relative to January 1, 1970. | |
function weekday(date) { | |
var weekdays = weekdayOfYear(date), | |
year = date.getFullYear(); | |
while (--year >= 1970) weekdays += weekdaysInYear(year); | |
return weekdays; | |
} | |
// Returns the date for the specified weekday number relative to January 1, 1970. | |
weekday.invert = function(weekdays) { | |
var year = 1970, | |
yearWeekdays; | |
// Compute the year. | |
while ((yearWeekdays = weekdaysInYear(year)) <= weekdays) { | |
++year; | |
weekdays -= yearWeekdays; | |
} | |
// Compute the date from the remaining weekdays. | |
var days = weekdays % 5, | |
day0 = ((new Date(year, 0, 1)).getDay() + 6) % 7; | |
if (day0 + days > 4) days += 2; | |
return new Date(year, 0, (weekdays / 5 | 0) * 7 + days + 1); | |
}; | |
// Returns the number of weekdays in the specified year. | |
function weekdaysInYear(year) { | |
return weekdayOfYear(new Date(year, 11, 31)) + 1; | |
} | |
// Returns the weekday number for the given date relative to the start of the year. | |
function weekdayOfYear(date) { | |
var days = d3.time.dayOfYear(date), | |
weeks = days / 7 | 0, | |
day0 = (d3.time.year(date).getDay() + 6) % 7, | |
day1 = day0 + days - weeks * 7; | |
return Math.max(0, days - weeks * 2 | |
- (day0 <= 5 && day1 >= 5 || day0 <= 12 && day1 >= 12) // extra saturday | |
- (day0 <= 6 && day1 >= 6 || day0 <= 13 && day1 >= 13)); // extra sunday | |
} | |
return weekday; | |
})(); |
Hello,
It is awesome job but I cannot find a graph with Time frame such as H1, H4, D1, W1, M1.
Do you have plan to develop this sample ?
I need to know that How the Lib is support multiple time frame ?
And How we make UI tool overlay on graph area ?
if it able to support please let me know at [email protected]
I have a project that need your help with good wage if you interest.
Thank you,
Rocky
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Mike, sorry for posting on wrong thread. But my issue is related to d3.scaleLinear/d3.scaleTime. We are using D3 V4. Need to display date on x-axis and number on Y-axis. When set x-axis values to domain and specify ticks(5), I am facing 2 issues mentioned below.
Could you please us a direction to overcome this issue? Our organization just started using D3 so we have less help from our IT teams.