Published
Edited
Sep 29, 2021
Insert cell
Insert cell
Insert cell
Insert cell
viewof angle4 = Inputs.range([-90, -90], { label: 'angle' })
Insert cell
Insert cell
import {slider } from "@jashkenas/inputs"
Insert cell
chart = {
const current_data = filter_year === 2020 ? data : data_set_1;

const svg = d3
.select(DOM.svg(width, height))
.attr("viewBox", `${-width / 2} ${-height / 2} ${width} ${height}`)
.style("width", "90%")
.style("height", "auto")
.style("font", "14px sans-serif")
.attr('transform', `rotate(${angle1})`);

svg
.append("g")
.attr('transform', `rotate(${angle2})`)
.selectAll("g")
.data(d3.stack().keys(data.columns.slice(1))(current_data))
.join("g")
.attr("fill", d => z(d.key))
.selectAll("path")
.data(d => d)
.join("path")
.attr("d", arc);

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

svg
.append("g")

.call(yAxis);

svg.append("g").call(legend)
.attr('transform', `rotate(${angle4})`);
return svg.node();
}
Insert cell
data = d3.csvParse(`indicator, Impact Community,Impact Ecosystem,Impact Captial
Business acumen,4,0,0
Overall well-being,5,0,0
Consciousness,2,0,0
Diverse and meaningful connections,2,0,0

Information transpareny,0,4,0
Exposure,0,4.5,0
Collaborativeness,0,3,0
Stakeholder involvement,0,2,0
.
Knowledge in social captial,0,0,3
Investment opportunity and support,0,0,2
`, (d, _, columns) => {
let total = 0;
for (let i = 1; i < columns.length; ++i) total += d[columns[i]] = +d[columns[i]];
d.total = total;
return d;
})
Insert cell
data_set_1 = d3.csvParse(`indicator, Impact Community,Impact Ecosystem,Impact Captial
Business acumen,3,0,0
Overall well-being,4,0,0
Consciousness,4,0,0
Diverse and meaningful connections,1,0,0

Information transpareny,0,1,0
Exposure,0,1,0
Collaborativeness,0,1,0
Stakeholder involvement,0,2,0
.
Knowledge in social captial,0,0,3
Investment opportunity and support,0,0,2
`, (d, _, columns) => {
let total = 0;
for (let i = 1; i < columns.length; ++i) total += d[columns[i]] = +d[columns[i]];
d.total = total;
return d;
})
Insert cell
arc = d3.arc()
.innerRadius(d => y(d[0]))
.outerRadius(d => y(d[1]))
.startAngle(d => x(d.data.indicator))
.endAngle(d => x(d.data.indicator) + x.bandwidth())
.padRadius(innerRadius)
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.indicator))
.range([-0.5*Math.PI, 0.5*Math.PI])
.align(0)
Insert cell
// This scale maintains area proportionality of radial bars
y = d3.scaleRadial()
.domain([0, d3.max(data, d => d.total)])
.range([innerRadius, outerRadius])
Insert cell
z = d3.scaleOrdinal()
.domain(data.columns.slice(1))//.range(["#DB004C", "#8CCBFF", "#FF3700"])
.range(["#FF8C00","#e61", "#f20"])
Insert cell
xAxis = g => g
.attr("text-anchor", "start:end")
.call(g => g.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `
rotate(${((x(d.indicator) + x.bandwidth() / 2) * 180 / Math.PI - 90)})
translate(${innerRadius},0)
`)
.call(g => g.append("line")
.attr("x2", -5)
.attr("stroke", "#000"))
.call(g => g.append("text")
.attr("transform", d => (x(d.indicator) + x.bandwidth() / 2 + Math.PI / 2) % (Math.PI) < 0.5*Math.PI
? "rotate(0)translate(10,3)"
: "rotate(0)translate(10,3)")
.text(d => d.indicator)))
Insert cell
yAxis = g => g
.attr("text-anchor", "end")
.call(g => g.selectAll("g")
.data(y.ticks(5).slice(1))
.join("g")
.attr("fill", "none")
.call(g => g.append("path")
.attr("stroke", "#000")
.attr("stroke-opacity", 0.22)
.attr("d", d3.arc()
.innerRadius(y)
.outerRadius(y)
.startAngle(-0.5*Math.PI)
.endAngle(0.5*Math.PI) ))

.call(g => g.append("text")
.attr("x", d => -y(d))
.attr("dx", "0.8em")
.attr("dy", "0.8em")
.attr("stroke", "#fff")
.attr("stroke-width", 5)
.text(y.tickFormat())
.clone(true)
.attr("fill", "#000")
.attr("stroke", "none"))
.call(g => g.append("text")
.attr("x", d => y(d))
.attr("dx", "0.35em")
.attr("dy", "-0.35em")
.attr("stroke", "#fff")
.attr("stroke-width", 5)
.text(y.tickFormat())
.clone(true)
.attr("fill", "#000")
.attr("stroke", "none"))
.attr("transform", d => `
rotate(${((x(d.indicator) + x.bandwidth() / 2) * 180 / Math.PI - 0)})
translate(${innerRadius},0)
`)
)
Insert cell
legend = g => g.append("g")
.selectAll("g")
.data(data.columns.slice(1))
.join("g")

.attr("transform", (d, i) => `translate(-110,${(i - (data.columns.length - 2.5) / 2) * 50-40})`)


.call(g => g.append("rect")
.attr("width", 18)
.attr("height", 18)
.attr("fill", z))
.call(g => g.append("text")
.attr("x", 24)
.attr("y", 9)
.attr("dy", "0.8em")
.text(d => d))

Insert cell
width = 975
Insert cell
height = width
Insert cell
innerRadius = 200
Insert cell
outerRadius = Math.min(width, height) / 2
Insert cell
d3 = require("d3@6")
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