Public
Edited
Dec 23, 2022
1 star
Insert cell
# Time scale ticks
Insert cell
startDate = new Date('01/20/2022')
Insert cell
endDate = new Date('02/10/2022')
Insert cell
timeScale = d3.scaleTime().domain([startDate, endDate]).range([0, width]);
Insert cell
tickNum = 5
Insert cell
tickDates = timeScale.ticks(tickNum);
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, 26]);

svg.append("g")
.call(d3.axisBottom().scale(timeScale).tickFormat(formatDate) );

return svg.node();
}
Insert cell
width = 1000
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, 26]);

svg.append("g")
.call(
d3.axisBottom(timeScale)
.tickValues(interValArray.map(d => new Date(d)))
.tickFormat(formatDate)
.tickSizeInner(5)
.tickSizeOuter(0)
//.tickSize(5)
.tickPadding(5)
)
//.call(g => g.select(".domain")
//.remove())

return svg.node();
}
Insert cell
dayCount = d3.timeDay.count(startDate, endDate)
Insert cell
everyInterval = Math.floor(dayCount/tickNum)
Insert cell
intervalF = d3.timeDay.filter(d => d3.timeDay.count(0, d) % (everyInterval - 1) === 0)
Insert cell
interValArray = timeScale.ticks(intervalF)
Insert cell
formatDate = (date) => {
return date.toLocaleString("en", {
month: 'short',
day: '2-digit',
year: 'numeric',
})
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more