Published
Edited
Nov 28, 2019
Fork of Solar path
Insert cell
Insert cell
Insert cell
chart = {
const cx = width / 2;
const cy = height / 2;

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "middle")
.attr("fill", "currentColor")
.style("margin", "0 -14px")
.style("display", "block");

svg.append("path")
.attr("d", path(graticule))
.attr("fill", "none")
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.2);

svg.append("path")
.attr("d", path(outline))
.attr("fill", "none")
.attr("stroke", "currentColor");

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

const sunPath = svg.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 2);

const hour = svg.append("g")
.selectAll("g")
.data(d3.range(24))
.join("g");

hour.append("circle")
.attr("fill", "black")
.attr("r", 2);

hour.append("text")
.attr("dy", "-0.4em")
.attr("stroke", "white")
.attr("stroke-width", 4)
.attr("stroke-linejoin", "round")
.attr("fill", "none")
.clone(true)
.attr("stroke", null)
.attr("fill", "black");

function update(date) {
sunPath.attr("d", path({type: "LineString", coordinates: d3.utcMinutes(date, d3.utcDay.offset(date)).map(solar.position)}));
hour.data(d3.utcHours(date, d3.utcDay.offset(date)));
hour.attr("transform", d => `translate(${projection(solar.position(d))})`);
hour.select("text:first-of-type").text(formatHour);
hour.select("text:last-of-type").text(formatHour);
}

return Object.assign(svg.call(update).node(), {update});
}
Insert cell
update = chart.update(date)
Insert cell
xAxis = g => g
.call(g => g.append("g")
.attr("stroke", "currentColor")
.selectAll("line")
.data(d3.range(360))
.join("line")
.datum(d => [
projection([d, 0]),
projection([d, d % 10 ? -1 : -2])
])
.attr("x1", ([[x1]]) => x1)
.attr("x2", ([, [x2]]) => x2)
.attr("y1", ([[, y1]]) => y1)
.attr("y2", ([, [, y2]]) => y2))
.call(g => g.append("g")
.selectAll("text")
.data(d3.range(0, 360, 10))
.join("text")
.attr("dy", "0.35em")
.text(d => d === 0 ? "N" : d === 90 ? "E" : d === 180 ? "S" : d === 270 ? "W" : `${d}°`)
.attr("font-size", d => d % 90 ? null : 14)
.attr("font-weight", d => d % 90 ? null : "bold")
.datum(d => projection([d, -4]))
.attr("x", ([x]) => x)
.attr("y", ([, y]) => y))
Insert cell
yAxis = g => g
.call(g => g.append("g")
.selectAll("text")
.data(d3.range(10, 91, 10)) // every 10°
.join("text")
.attr("dy", "0.35em")
.text(d => `${d}°`)
.datum(d => projection([180, d]))
.attr("x", ([x]) => x)
.attr("y", ([, y]) => y))
Insert cell
path = d3.geoPath(projection)
Insert cell
projection = d3.geoProjection((x, y) => d3.geoStereographicRaw(x, -y))
.scale(scale)
.clipExtent([[0, 0], [width, height]])
.rotate([0, -90])
.translate([width / 2, height / 2])
.precision(0.1)
Insert cell
outline = d3.geoCircle().radius(90).center([0, 90])()
Insert cell
graticule = d3.geoGraticule().stepMinor([15, 10])()
Insert cell
scale = (width - 120) * 0.5
Insert cell
width = 954 + 28
Insert cell
height = width
Insert cell
formatHour = d => d.toLocaleString("de", {hour: "numeric", timeZone: "Europe/Berlin"})
Insert cell
formatMonth = d => d.toLocaleString("de", {month: "long", timeZone: "Europe/Berlin"})
Insert cell
location = [7, 50.848]
Insert cell
solar = (await require("solar-calculator@0.2"))(location)
Insert cell
d3 = require("d3@5")
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