Public
Edited
Apr 9
7 forks
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
windrose = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif");
const g = svg.append("g")
.attr("transform", `translate(${width/2},${height/2})`);
const stackedData = d3.stack().keys(data.columns.slice(1))(data);
g.append("g").attr("class", "axes")
.selectAll(".axis")
.data(d3.range(0, 360, 360/data.length))
.join("g")
.attr("class", "axis")
.attr("transform", (d) => `rotate(${d - 90})`)
.append("line")
.attr("x1", innerRadius)
.attr("x2", y(y.ticks(5).reverse()[0])) // to the last tick circle
.attr("fill", "none")
.attr("stroke","gray")
.attr("stroke-dasharray", "1,4");
g.append("g")
.attr("class", "rings")
.selectAll(".ring")
.data(stackedData)
.join(
enter => enter.append("g")
.attr("fill", (d) => colorScale(d.key) )
.selectAll("path")
.data((d) => d)
.join("path")
.attr("d", makeArc)
.attr("transform", `rotate(${angleOffset})`)
)

const label = g.append("g")
.attr("class", "direction-labels")
.selectAll("g")
.data(data)
.join("g")
.attr("text-anchor", "middle")
.attr("transform", (d) => `rotate(${(x(d.angle) + x.bandwidth() / 2) * 180 / Math.PI - (90 - angleOffset)}) translate(${outerRadius + 20},0)`);

const inLowerHalf = (d) => (x(d.angle) + x.bandwidth() / 2 + Math.PI / 2) % (2 * Math.PI) < Math.PI
label.append("text")
.attr("transform", (d) => inLowerHalf(d) ? "rotate(90)translate(0,6)" : "rotate(-90)translate(0,6)") // flip bottom labels
.text((d) => d.angle )
.attr("font-weight", 500)
.attr("font-size", 14);
g.call(yAxis)
const legend = g.call(addLegend)
return svg.node()
}
Insert cell
stackedData = d3.stack().keys(data.columns.slice(1))(data);
Insert cell
// the xScale is for the bars, one full turn around circle
x = d3.scaleBand()
.range([0, 2 * Math.PI])
.domain(data.map((d) => d.angle ))
.align(0);
Insert cell
// yScale is for the height of the bars
y = d3.scaleLinear()
.range([innerRadius, outerRadius])
.domain([0, d3.max(data, (d) => d.total )]);
Insert cell
colorScale = d3.scaleOrdinal()
.domain(data.columns.slice(1))
.range(d3.schemeBlues[data.columns.length-1]); // nr of wind velocity 'bins'
Insert cell
makeArc = d3.arc()
.innerRadius((d) => y(d[0]))
.outerRadius((d) => y(d[1]))
.startAngle((d) => x(d.data.angle) )
.endAngle((d) => x(d.data.angle) + x.bandwidth() )
.padAngle(0.02)
.padRadius(innerRadius);
Insert cell
addLegend = (g) => g.append("g")
.selectAll("g")
.attr("class", "legend")
.data(data.columns.slice(1).reverse())
.join("g")
.attr("transform", (_, i) => `translate(${outerRadius+30},${-outerRadius + 40 + (i - (data.columns.length - 1) / 3) * 20})`)
.call(g => g.append("rect")
.attr("width", 18)
.attr("height", 18)
.attr("fill", colorScale)
.attr("stroke", "dimgray")
.attr("stroke-width", 0.5)
)
.call(g => g.append("text")
.attr("x", 24)
.attr("y", 9)
.attr("dy", "0.35em")
.text((d) => `${d} ${data.unit}`)
.style("font-size",13)
).append("text")

Insert cell
yAxis = g => g.append("g")
.attr("class", "yAxis")
.selectAll("g")
.data(y.ticks(5)) // .slice(1) to skip 0
.join("g")
.call(g => g.append("circle")
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-dasharray", "4,4")
.attr("r", y)
)
// .call(g => g.append("text")
// .attr("y", (d) => -y(d) )
// .attr("dy", "-0.35em")
// .attr("x", -10)
// .text(y.tickFormat(5, "s"))
// .attr("font-size", 15)
// .attr("paint-order", "stroke")
// .attr("stroke-width", "1.4px")
// .attr("stroke", "white")
// )
// .call(g => g.append("text")
// .attr("y", -outerRadius + 10 )
// .attr("x", 15)
// .text("Ocurrences")
// .attr("paint-order", "stroke")
// .attr("stroke-width", "1.4px")
// .attr("stroke", "white")
// )
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = Object.assign(d3.csvParse(selectedData, (d, i, columns) => {
let t = 0;
for (let i = 1; i < columns.length; ++i) {
const columnName = columns[i]
t += +d[columnName]
}

d.total = t;

return d;
}), { unit: "km/h"})
Insert cell
Insert cell
Insert cell
Insert cell
fourtyfiveDeg = `angle,0 to 2 km/h,2 to 4 km/h,4 to 6 km/h,6 to 8 km/h,8 to 10 km/h,10 to 12 km/h,12 to 14 km/h,14 to 16 km/h,16 to 18 km/h
N,0.0,0.0,5.0,14.0,2.0,0.0,0.0,0.0,0.0
NE,0.0,1.0,5.0,3.0,1.0,0.0,0.0,0.0,0.0
E,0.0,3.0,3.0,5.0,1.0,0.0,0.0,0.0,0.0
SE,1.0,1.0,3.0,1.0,1.0,5.0,1.0,1.0,0.0
S,0.0,1.0,7.0,6.0,2.0,2.0,4.0,4.0,1.0
SW,2.0,2.0,9.0,9.0,7.0,0.0,3.0,2.0,2.0
W,1.0,2.0,10.0,14.0,2.0,1.0,1.0,1.0,2.0
NW,0.0,0.0,1.0,9.0,4.0,0.0,0.0,0.0,0.0`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3", "d3-array")
Insert cell
import { freelanceBanner } from "@julesblm/freelance-banner"
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