Public
Edited
Jan 24, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
myData =[{group: 'mDem', value: '22',label: 'DEM'}, {group: 'wDem', value: '10',label: 'DEM'}, {group: 'spacer', value: '20', label: 'IND'},{group: 'wReb', value: '2',label: 'GOP'}, {group: 'mReb', value: '15',label: 'GOP'} ]
Insert cell
myLineData= [{group: 1, value: '1'}, {group: 2, value: '50'},{group: 3, value: '100'}, ]
Insert cell
myLegendData=[{group: 'DEM', value: 105},{group: 'DEM', value: 105},{group: 'IND', value: 105}, {group: 'GOP', value: 105}, {group: 'GOP', value: 105}]
Insert cell
myMax_value = 100
Insert cell
myMax_radius = 100
Insert cell
myScale = d3.scaleLinear().domain([0,100]).range([50,150])
Insert cell
myPolarData = myData.map((d,i) => {
return {
value: i !== 2 ? myScale(d.value): +d.value,
percent: i !== 2 ? d.value : 0,
sector: d.group,
startAngle: (i * Math.PI * 2) / 10,//because 5 sections in a half circle. If wanted whole circle, would be 10 sections
endAngle: ((i * Math.PI * 2) / 10) + (Math.PI * 2 / 10),
outerRadius:(myScale(d.value) / myMax_value) * myMax_radius
} } )
Insert cell
myPolarLineData = myLineData.map((d,i) => {
return {
value:myScale(d.value),
percent:d.value,
sector: d.group,
startAngle:0,
endAngle: (Math.PI * 2 / 2),
outerRadius:(myScale(d.value) / myMax_value) * myMax_radius
} } )
Insert cell
myPolarLegendData = myLegendData.map((d,i) => {
return {
value:myScale(d.value),
percent:d.value,
sector: d.group,
startAngle:i === 2 ? ((i * Math.PI * 2)/10) +.02 : ((i * Math.PI * 2)/10),//because 5 sections in a half circle. If wanted whole circle, would be 10 sections
endAngle: i === 2 ? ((i * Math.PI * 2) / 10) + (Math.PI * 2 / 10) - .02: ((i * Math.PI * 2) / 10) + (Math.PI * 2 / 10),
outerRadius:(myScale(d.value) / myMax_value) * myMax_radius
} } )
Insert cell
myArc = d3.arc()
.innerRadius(50)

Insert cell
myLineArc = d3.arc()
Insert cell
myChart = {
const arcs = myPolarData; // pie(data);
const lineArcs = myPolarLineData
const legendArcs = myPolarLegendData
const spacer = 50
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("transform", "rotate(-90) translate(-100)")

svg.append("g")
.selectAll("path")
.data(arcs)
.join("path")
.attr("fill", (d,i) => myColor[i])
.attr("d", myArc)

// Wrapper for the grid & axes
var legend = svg.append("g").attr("class", "legend");
legend
.selectAll("path")
.data(legendArcs)
.join("path")
.attr("stroke", (d,i) => myLegendColor[i])
.attr("stroke-width", 2)
//.attr('opacity', .5)
.attr('fill', 'transparent')
.attr("d", myLineArc)
var lines = svg.append("g").attr("class", "lines");
lines
.selectAll("path")
.data(lineArcs)
.join("path")
.attr("stroke", (d,i) => i?'gray': 'white')
.attr('stroke-dasharray', '4 4')
.attr('opacity', .5)
.attr('fill', 'transparent')
.attr("d", myLineArc)

// Draw the background circles
// axisGrid.selectAll(".levels")
// .data(d3.range(4).reverse())
// .enter()
// .append("circle")
// .attr("class", "gridCircle")
// .attr("r", d => d * spacer)
// .style("fill", 'transparent')
// .attr("stroke", "#cdcdcd")
//.style("fill-opacity", .5);

// svg.append("g")
// .attr("font-family", "sans-serif")
// .attr("font-size", 12)
// .attr("text-anchor", "middle")
// .selectAll("text")
// .data(arcs)
// .join("text")
// .attr("transform", d => `translate(${arcLabel.centroid(d)})`)
// .call(text => text.append("tspan")
// .attr("y", "-0.4em")
// .attr("font-weight", "bold")
// .text( d => d.sector ));

return svg.node();
}
Insert cell
myColor = ['#5f7dad', '#133061', 'transparent','#8f1626', '#db6272']
Insert cell
myLegendColor=['#133061', '#133061', 'grey','#8f1626', '#8f1626']
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