Public
Edited
Apr 30, 2019
18 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const svg = d3.select(DOM.svg(1000,6080))
svg.style('background-color', '#1d1b3d')
// score and sort each country
gdp.forEach((country) => {
let allValues = Object.keys(country)
.filter((key) => key !== 'score' && key !== 'Country Name' )
.map((key) =>
country[key] + wiggle >= target &&
country[key] - wiggle <= target ?
100 : 100 - Math.abs(country[key] - target)
)

country.score = allValues.reduce((a, b) => a + b, 0)
});
gdp.sort( (a, b) => b.score - a.score)
const sliceCount = gdp.columns.length -1;
const wedge = tau / sliceCount;
const padding = wedge/16;
let possition = { left:margin, top:margin }
gdp.forEach((data) => {
let i = 0;
let g = svg.append("g")
.attr("width", width)
.attr("height", height)
let left = possition.left + padding,
top = possition.top + padding;

heading(g, data['Country Name'], left, top)
possition.left += width;
if((possition.left + width) > (1000-margin) ) {
possition.left = margin;
possition.top += height;
}
g.selectAll("path")
.data( Object.keys(data)
.filter((key) => key !== 'score' && key !== 'Country Name')
.map((key) => data[key])
)
.enter()
.append("path")
.attr("transform", "translate(" + [(width/2)+left, (height/2)+top] + ")")
.style("fill", d => {
const num = parseFloat(d);
const inRange = (num+wiggle) > target && (num-wiggle) < target;
const onTheMark = num+(wiggle*2) > target && num-(wiggle*2) < target;
return inRange ? "#86e5ce" : onTheMark ? "#fec670" : "#fb7f82";
})
.attr("d", d3.arc()
.innerRadius(innerRadius)
.outerRadius(d => scale(d))
.startAngle(d => {
let start = (wedge * i) + padding;
i++;
return start;
})
.endAngle(() => (wedge * i) - padding)
)

donut(g, left, top)
});
return svg.node()
}
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
Insert cell
Insert cell
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