Public
Edited
Oct 23, 2023
13 forks
73 stars
Insert cell
Insert cell
Insert cell
localtime = d3.scaleTime()
Insert cell
Insert cell
domain = localtime.domain() // 1 Jan. 2000, 00h00 to 2 Jan. 2000, 00h00
Insert cell
Insert cell
range = localtime.range()
Insert cell
Insert cell
Insert cell
visualizeTicks(
d3.scaleTime().domain([Date.now(), Date.now() + 24 * 60 * 60 * 1000])
)
Insert cell
Insert cell
visualizeTicks(
d3
.scaleTime()
.domain([Date.now(), Date.now() + 21 * 60 * 60 * 1000])
.nice()
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
[
localtime.invert(0),
localtime.invert(0.5),
localtime.invert(1),
localtime.invert(2)
]
Insert cell
Insert cell
d3.scaleTime().range([0, 500])(new Date("2000-01-01T13:02"))
Insert cell
d3.scaleTime().rangeRound([0, 500])(new Date("2000-01-01T13:02"))
Insert cell
Insert cell
Insert cell
d3.scaleTime().clamp(true)(Date.now()) // result clamped to [0,1]
Insert cell
d3.scaleTime()(Date.now()) // by default, the scale does not clamp
Insert cell
Insert cell
year2year = d3
.scaleTime()
.domain([new Date("2012-01-01"), new Date("2013-01-01")])
.range([new Date("1970-01-01"), new Date("1971-01-01")])
Insert cell
year2year(new Date("2012-02-29T02:00")) // at 2am
Insert cell
year2year(new Date("2012-02-29T14:00")) // at 2pm
Insert cell
Insert cell
utc = d3.scaleUtc()
Insert cell
utc.invert(0.00001)
Insert cell
utc.invert(0)
Insert cell
utc.invert(0.999999)
Insert cell
utc.invert(0)
Insert cell
Insert cell
Insert cell
Insert cell
+domain[0] // number of milliseconds since 1 Jan 1970.
Insert cell
domain[1] - domain[0] // number of milliseconds in the domain
Insert cell
24 * 60 * 60 * 1000 // 24 hours x 60 minutes x 60 seconds x 1000 milliseconds
Insert cell
The default scaleTime domain goes
from _${domain[0]}_
to _${domain[1]}_.
Insert cell
Insert cell
format = date =>
date.toLocaleString("en-US", {
weekday: "short",
month: "short",
year: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric"
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
leapyear = d3
.scaleTime()
.domain([new Date("1972-02-24T00:00:00"), new Date("1972-03-06T00:00:00")])
Insert cell
leapyear.range([0, 1]).invert(0.5) // 29 Feb 1972
Insert cell
Insert cell
nonleapyear = d3
.scaleTime()
.domain([new Date('December 17, 1995 03:24:00'), new Date('December 17, 1998 03:24:00')])
Insert cell
nonleapyear.range([0, 1]).invert(0.5) // 1 March 1970
Insert cell
visualizeTicks(nonleapyear, [10, d3.timeFormat("%d %b")])
Insert cell
Insert cell
leapsecond = d3
.scaleUtc()
.domain([Date.UTC(2016, 11, 31, 23, 59, 56), Date.UTC(2017, 0, 1, 0, 0, 4)])
Insert cell
visualizeTicks(leapsecond, [10, d3.timeFormat("%S")]) // the leap second is ignored by JavaScript 🕷
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3-scale@3", "d3-selection@2", "d3-time-format@3")
Insert cell
import {visualizeTicks} from "@d3/continuous-scales"
Insert cell
import {clock, style} with {clockRadius} from "@d3/simple-clock"
Insert cell
clockRadius = Math.min(200, width / 2 - 40)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more