Published
Edited
Mar 14, 2021
Importers
Insert cell
Insert cell
Insert cell
tooltip = d3
.select('body')
.append('div')
.attr('class', 'd3-tooltip')
.style('position', 'absolute')
.style('z-index', '10')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff')
.style('font-size', "18px")
.style('font-weight', 250)
.style('font-family', 'FreeMono, monospace')

.text('a simple tooltip')
Insert cell
hoverColor = '#eec42d'
Insert cell
data_raw = d3.csv(
"https://raw.githubusercontent.com/isaack8/final-project-cse412/main/data/EPA_percent_change.csv",
d3.autoType
)
Insert cell
sort = data => data.sort((a, b) => d3.descending(a.Value, b.Value))
Insert cell
data = sort(data_raw)
Insert cell
x = d3
.scaleBand()
.domain(d3.range(data.length))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = {
// Find the largest absolute value
const absMax = d3.max(data, d => Math.abs(d.Value))
// Use that for the scale
return d3.scaleLinear()
.domain([-absMax, absMax]).nice()
.range([height - margin.bottom, margin.top])
}
Insert cell
xAxisPos = g => g
.attr("transform", `translate(0,${height / 2})`)
.call(d3.axisBottom(x).tickFormat(i => data[i].Source).tickSizeOuter(0))
.selectAll('g.tick')
.attr('display', i => data[i].Value < 0 ? 'none' : '')
Insert cell
xAxisNeg = g => g
.attr("transform", `translate(0,${height / 2})`)
.call(d3.axisTop(x).tickFormat(i => data[i].Source).tickSizeOuter(0))
.selectAll('g.tick')
.attr('display', i => data[i].Value > 0 ? 'none' : '')
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, data.format).tickFormat(function(d){return d*100+ "%"}))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y))
Insert cell
color = "steelblue"
Insert cell
height = 300
Insert cell
width = 600
Insert cell
margin = ({ top: 30, right: 60, bottom: 30, left: 60 })
Insert cell
d3 = require("d3@5")
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