Public
Edited
Nov 10, 2022
2 stars
Insert cell
Insert cell
coviddata = FileAttachment("covid-compressed.json").json()
Insert cell
Insert cell
visDimensions = ({ width: 500, height: 200 });
Insert cell
margins = ({top: 30, bottom: 20, left: 100, right: 20 })
Insert cell
## Slider
Insert cell
viewof dataIndex = Inputs.range([0, coviddata.length - 1], {step: 1})
Insert cell
svg.node()
Insert cell
Insert cell
selectedMonth = coviddata[dataIndex].sort( (a,b) => d3.descending( a.total_cases, b.total_cases )).slice(0, 10);
Insert cell
## Creating SVG
Insert cell
svg = {
const svg = d3.create("svg")
.attr('viewBox', [0, 0, visDimensions.width, visDimensions.height])
.attr('font-family', 'sans-serif')
.attr('font-size', 10)
.attr('display', 'block');
return svg;
}
Insert cell
chartGroup = {
const group = svg.append('g')
.attr('transform', `translate(${margins.left}, ${margins.top})`);
return group;
}
Insert cell
Insert cell
yAxisGroup = svg.append('g')
.attr('class', 'axis-group')
.attr('transform', `translate(${margins.left}, ${margins.top})`);
Insert cell
xAxisGroup = svg.append('g')
.attr('class', 'axis-group')
.attr('transform', `translate(${margins.left}, ${ margins.top })`);
Insert cell
{

// create scales
const yScale = d3.scaleBand()
.domain(Array.from( new Set(selectedMonth.map( element => element.location )) ))
.range([0, visDimensions.height - margins.top - margins.bottom ])
.padding(0.2);

const xScale = d3.scaleLinear()
.domain(d3.extent(selectedMonth, d => d['total_cases']))
.range([10, visDimensions.width - margins.left - margins.right ]);

const colorScale = d3.scaleOrdinal()
.domain(Array.from( new Set(selectedMonth.map( element => element.location )) ))
.range(['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a'])

// adding axes
xAxisGroup.call( d3.axisTop(xScale).tickFormat(d3.format('.2s')) );
yAxisGroup.call( d3.axisLeft(yScale) );
chartGroup
.selectAll('.bar')
.data(selectedMonth, d => d.location )
.join(
enter => enter
.append('rect')
.attr('class', 'bar')
.attr('x', 10)
.attr('y', d => yScale(d.location))
.attr('width', d => xScale(d.total_cases))
.attr('height', 10)
.attr('fill', d => colorScale(d.location)),
update => update.transition()
.duration(500)
.attr('width', d => xScale(d.total_cases))
.attr('y', d => yScale(d.location)),
exit => exit.transition().duration(2000).attr('x', visDimensions.width ).attr('y', visDimensions.height ).remove()
);
}
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