Public
Edited
Sep 19, 2022
Fork of D3 U.S. map
Insert cell
Insert cell
Insert cell
d3 = require("d3", "d3-geo-projection@4", "d3-geo@2")
Insert cell
topojson = require("topojson@3")
Insert cell
Insert cell
usa = FileAttachment("counties-albers-10m.json").json()
Insert cell
nation = topojson.feature(usa, usa.objects.nation)
Insert cell
states = topojson.feature(usa, usa.objects.states)
Insert cell
projection = d3.geoAlbersUsa().scale(1300).translate([487.5, 305])
Insert cell
path = d3.geoPath()
Insert cell
graticule = d3.geoGraticule();
Insert cell
Insert cell
<svg id="map" viewBox="0 0 975 610">
<g id="graticule"></g>
<g id="nation"></g>
<g id="states"></g>
</svg>
Insert cell
svg = d3.select('#map')
Insert cell
Insert cell
// svg
// .select('#graticule')
// .append('path')
// .attr('d', path(graticule()))
// .attr('fill', 'none')
// .attr('stroke', 'black')
// .style('opacity', 0.2);
Insert cell
Insert cell
colorScale = d3.scaleSequential(d3.interpolateReds).domain([0, 1])
Insert cell
{
svg
.select('#nation')
.append('path')
.attr('d', path(nation))
.attr('fill', 'none')
.attr('stroke', 'lightgrey')

const stateD3 = svg
.select('#states')
.selectAll('path')
.data(states.features)
.enter()
.append('path')
.attr('d', path)
.attr('fill', (d) => colorScale(Math.random()))
.attr('stroke', 'lightgrey')
stateD3.on('click', (d) => {
console.log('clicked', d)
})
}
Insert cell
Insert cell
Insert cell
<svg id="legend" viewBox="0 0 ${innerWidth} ${50}">
<defs>
<linearGradient id="color-gradient">
<stop offset="0%" style="stop-color:#1f77b4;stop-opacity:1"></stop>
<stop offset="30%" style="stop-color:#ff7f0e;stop-opacity:1"></stop>
<stop offset="60%" style="stop-color:#2ca02c;stop-opacity:1"></stop>
<stop offset="100%" style="stop-color:#d62728;stop-opacity:1"></stop>
</linearGradient>
</defs>
</svg>
Insert cell
legend = d3.select('#legend')
.append('rect')
.attr('width', innerWidth)
.attr('y', 0)
.attr('height', 50)
.attr('fill', 'url(#color-gradient)');
Insert cell
Insert cell
//DataSource: eMarketer, March 2018
marketingData = [
{ year: 2016, media: "Digital", spending: 72.2 },
{ year: 2017, media: "Digital", spending: 90.39 },
{ year: 2018, media: "Digital", spending: 107.3 },
{ year: 2019, media: "Digital", spending: 125.75 },
{ year: 2020, media: "Digital", spending: 142.23 },
{ year: 2021, media: "Digital", spending: 156.43 },
{ year: 2022, media: "Digital", spending: 170.48 },
{ year: 2016, media: "TV", spending: 71.29 },
{ year: 2017, media: "TV", spending: 70.22 },
{ year: 2018, media: "TV", spending: 69.87 },
{ year: 2019, media: "TV", spending: 69.17 },
{ year: 2020, media: "TV", spending: 69.52 },
{ year: 2021, media: "TV", spending: 68.82 },
{ year: 2022, media: "TV", spending: 68.13 },
{ year: 2016, media: "Print", spending: 25.49 },
{ year: 2017, media: "Print", spending: 22.81 },
{ year: 2018, media: "Print", spending: 20.05 },
{ year: 2019, media: "Print", spending: 17.29 },
{ year: 2020, media: "Print", spending: 15.19 },
{ year: 2021, media: "Print", spending: 13.56 },
{ year: 2022, media: "Print", spending: 12.38 },
{ year: 2016, media: "Radio", spending: 14.33 },
{ year: 2017, media: "Radio", spending: 14.33 },
{ year: 2018, media: "Radio", spending: 14.41 },
{ year: 2019, media: "Radio", spending: 14.43 },
{ year: 2020, media: "Radio", spending: 14.46 },
{ year: 2021, media: "Radio", spending: 14.49 },
{ year: 2022, media: "Radio", spending: 14.52 },
{ year: 2016, media: "Out-of-home", spending: 7.6 },
{ year: 2017, media: "Out-of-home", spending: 7.75 },
{ year: 2018, media: "Out-of-home", spending: 7.87 },
{ year: 2019, media: "Out-of-home", spending: 7.95 },
{ year: 2020, media: "Out-of-home", spending: 8.03 },
{ year: 2021, media: "Out-of-home", spending: 8.11 },
{ year: 2022, media: "Out-of-home", spending: 8.19 },
{ year: 2016, media: "Directories", spending: 2.35 },
{ year: 2017, media: "Directories", spending: 1.83 },
{ year: 2018, media: "Directories", spending: 1.47 },
{ year: 2019, media: "Directories", spending: 1.19 },
{ year: 2020, media: "Directories", spending: 0.99 },
{ year: 2021, media: "Directories", spending: 0.84 },
{ year: 2022, media: "Directories", spending: 0.74 }
];
Insert cell
groupedMarketingData = d3.group(marketingData, (d) => d.media)
Insert cell
Insert cell
<svg id="line-chart-svg" viewBox="0 0 ${innerWidth} ${600}">
<g id="axes">
<g id='x-axis'></g>
</g>
<g id="lines"></g>
<g id="overlay">
<line></line>
</g>
</svg>
Insert cell
lineChart = d3.select('#line-chart-svg')
Insert cell
Insert cell
padding = {
return {left: 50, bottom: 50, right:40}
}
Insert cell
lineColorScale = d3.scaleOrdinal(d3.schemeTableau10).domain(groupedMarketingData.keys());
Insert cell
// Add x axis --> it is a date format
xAxis = {
const xAxis = d3.scaleLinear()
.domain([2016, 2022])
.range([ padding.left, innerWidth - padding.right]);

lineChart
.select('#x-axis')
.attr('transform', `translate(0, ${600 - padding.bottom})`)
.call(d3.axisBottom(xAxis).ticks(7,'.2d'))

// Append x axis text
lineChart
.select('#axes')
.append('text')
.text('Year')
.attr('x', 550)
.attr('y', 600);

return xAxis
}
Insert cell
// Add x axis --> it is a date format
yAxis = {
// Add y axis
const yAxis = d3.scaleLinear()
.domain([0, Math.max(...marketingData.map((row) => row.spending))])
.range([ 600 - padding.bottom, 10 ])
.nice();

lineChart.select('#axes')
.append('g')
.attr('transform', `translate(${padding.left},0)`)
.call(d3.axisLeft(yAxis));

// Append y axis text
lineChart
.select('#axes')
.append('text')
.text('Spending')
.attr('x', -340)
.attr('y', 20)
.attr('transform', 'rotate(-90)');

return yAxis
}
Insert cell
Insert cell
lineChart
.select('#lines')
.selectAll('path')
.data(groupedMarketingData)
.join('path')
.attr('fill', 'none')
.attr('stroke', ([group, values]) => lineColorScale(group))
.attr('stroke-width', 1)
.attr('d', ([group, values]) => d3.line()
.x((d) => xAxis(d.year))
.y((d) => yAxis(d.spending))
(values))
Insert cell
Insert cell
lineChart.on('mousemove', (event) => {
if (event.clientX > padding.left && event.clientX < innerWidth - padding.right) {
// Set the line position
lineChart
.select('#overlay')
.select('line')
.attr('stroke', 'black')
.attr('x1', event.clientX)
.attr('x2', event.clientX)
.attr('y1', 600 - padding.bottom)
.attr('y2', 0);

// Find the relevant data (by date and location)
const yearHovered = Math.floor( xAxis.invert(event.clientX))
const filteredData = marketingData
.filter((row) => row.year === yearHovered)
.sort((rowA, rowB) => rowB.spending - rowA.spending)

lineChart.select('#overlay')
.selectAll('text')
.data(filteredData)
.join('text')
.text(d=>`${d.media}, ${d.spending}`)
.attr('x', condition ? event.clientX : event.clickX)
.attr('y', (d, i) => 20*i + 20)
.attr('alignment-baseline', 'hanging')
.attr('fill', (d) => lineColorScale(d.media));
}
});

globalState.selected.push(country)
globalState.lineChart.update()
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