Published
Edited
Mar 8, 2020
Insert cell
Insert cell
Insert cell
Insert cell
holidaysViz = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height]);
const background = svg.append('rect')
.attr('width', width)
.attr('height', height)
.attr('fill', '#22292F');
const wrapper = svg.append('g')
.attr('transform', `translate(${width / 2}, ${height / 2})`);
wrapper.append('g')
.call(labels);
wrapper.append('g')
.call(xAxis);
wrapper.append('g')
.call(yAxis);
wrapper.append('g')
.call(lines);

return svg.node();
}
Insert cell
colorScale = d3.scaleOrdinal(["#38C172", "#5661B3", "#F66D9B"]);
Insert cell
y = d3.scaleBand()
.domain(["my_country", "my_continent", "other_continent"])
.range([innerRadius, outerRadius])
Insert cell
angle = d3.scaleTime()
.domain([new Date(1986, 9, 12), new Date(2020, 9, 12)])
.range([0, 2 * Math.PI])
Insert cell
mybirth = new Date(1986, 9, 12)
Insert cell
birthdays = d3.range(1, 35, 1).map(y => {
return moment(mybirth).add(y, 'year').toDate();
})
Insert cell
labels = g => g
.attr('class', 'labels')
.call(g => g.selectAll('g')
.data(y.domain())
.join('g')
.call(g => g.append('path')
.attr('stroke', 'green')
.attr('stroke-width', 4)
.attr('d', d => `
M${d3.pointRadial(0), y(d)}
A${outerRadius},${outerRadius}
0,0,1 ${d3.pointRadial(angle(d) + 20, outerRadius)}`
)));
Insert cell
xAxis = g => g
.attr('class', 'x-axis')
.call(g => g.selectAll('g')
.data(birthdays)
.join('g')
.call(g => g.append('path')
.attr('stroke', '#3D4852')
.attr('stroke-opacity', 0.6)
.attr('stroke-dasharray', 2)
.attr('d', d => `
M${d3.pointRadial(angle(d), innerRadius)}
L${d3.pointRadial(angle(d), outerRadius)}`))
.call(g => g.append('path')
.attr('class', 'bogen')
.attr('id', d=> `year-${d.getFullYear()}`)
.datum(d => [d, d.getFullYear(), d.getFullYear() + 1, d.getFullYear() - 1986])
.attr('fill', 'none')
.attr('d', ([d, a, b, p]) => {
let shift = p > 9 ? 0.03 : 0.015;
return `M${d3.pointRadial(angle(d) - shift, outerRadius + 4)}
A${outerRadius},${outerRadius}
0,0,1 ${d3.pointRadial(angle(d) + 20, outerRadius)}`
}))
.call(g => g.append('text')
.append('textPath')
.attr('font-size', 13)
.attr('fill', '#8795A1')
.attr('startOffset', 0)
//.attr('text-anchor', 'middle')
.attr('xlink:href', d => `#year-${d.getFullYear()}`)
.text(d => d.getFullYear() - 1986)));
Insert cell
lines = g => g
.attr('class', 'holidays')
.call(g => g.selectAll('path')
.data(holidays)
.join('g')
/*.call(g => g.append('path')
.attr('fill', 'none')
.attr('stroke', 'steelblue')
.attr('stroke-width', 20)
.attr('d', d => {
return `M${d3.pointRadial(angle(d.start), y(d.where))}
A${d3.pointRadial(angle(d.start), y(d.where))},
0, 0, 1
${d3.pointRadial(angle(d.end), y(d.where))}`
}))*/
.call(g => g.append('path')
.attr('stroke', d => colorScale(d.where))
.attr('stroke-opacity', .9)
.attr('d', d => `
M${d3.pointRadial(angle(d.start), y(d.where))}
L${d3.pointRadial(angle(d.start), y(d.where) + 80)}
`))
/*.call(g => g.append('path')
.attr('stroke', 'black')
.attr('stroke-dasharray', 1)
.attr('d', d => `
M${d3.pointRadial(angle(d.start) + 0.01, y(d.where))}
L${d3.pointRadial(angle(d.start) + 0.01, y(d.where) + 80)}
`))*/);
Insert cell
yAxis = g => g
.attr('class', 'y-axis')
.call(g => g.selectAll('g')
.data(y.domain())
.join('g')
.attr('fill', 'none')
.call(g => g.append('circle')
.attr('stroke', d => colorScale(d))
.attr('stroke-width', 1)
.attr('stroke-opacity', .6)
.attr('r', d => y(d))));
Insert cell
innerRadius = 50
Insert cell
outerRadius = 185
Insert cell
Insert cell
innerHeight = height - margin.top - margin.bottom
Insert cell
innerWidth = width - margin.left - margin.right
Insert cell
height = 490
Insert cell
margin = ({top: 30, right: 30, left: 40, bottom: 30})
Insert cell
Insert cell
text = FileAttachment('holidays@3.CSV').text()
Insert cell
psv = d3.dsvFormat(",")
Insert cell
parseTime = d3.timeParse("%d.%m.%Y")
Insert cell
holidays = psv.parse(text, d => {
return {
where: d.where,
start: parseTime(d.start),
end: parseTime(d.end)
} ;
})
Insert cell
d3 = require('d3@5')
Insert cell
import {swatches} from '@d3/color-legend'
Insert cell
moment = require('moment')
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