Published
Edited
Jul 27, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
simpleData = new Array(10).fill(undefined)
.map(_ => ({
value: d3.randomInt(1,10)(),
name: randomWords()
}))
Insert cell
Insert cell
Insert cell
Insert cell
width
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart1 = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height])
return svg.node()
}
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top: 30, right: 0, bottom: 30, left: 40})
Insert cell
Insert cell
x = d3.scaleBand()
.domain(simpleData.map(d => d.name))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(simpleData, d => d.value)])
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
Insert cell
simpleData
Insert cell
xCoordinates = simpleData.map(d => d.value).map(d => y(d))
Insert cell
yCoordinates = simpleData.map(d => d.name).map(d => x(d))
Insert cell
md`For fun, we're going to define another scale for our colors.

https://observablehq.com/@d3/working-with-color`
Insert cell
color = d3.scaleOrdinal()
.domain(simpleData.map(d => d.name))
.range(d3.schemeSet1)
Insert cell
md`Like our coordinates, \`\`d3.schemeSet\`\` allows us to map each data point with a corresponding color.`
Insert cell
simpleData.map(d => d.name).map(d => color(d))
Insert cell
Insert cell
Insert cell
stageTwoSVG = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height])
svg.append('g')
.style('stroke', 'black')
.selectAll('rect')
.data(simpleData)
.join('rect')
.attr('fill', d => color(d.name))
.attr('x', d => x(d.name))
.attr('y', d => y(d.value))
.attr('height', d => y(0) - y(d.value))
.attr('width', x.bandwidth())
return svg.node()
}
Insert cell
Insert cell
Insert cell
Insert cell
xAxis = g => g
.attr('transform', `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
.selectAll('.domain').remove()
Insert cell
Insert cell
yAxis = g => g
.attr('transform', `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.selectAll('.domain').remove()
Insert cell
Insert cell
stageThreeSVG = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height])
svg.append('g')
.style('stroke', 'black')
.selectAll('rect')
.data(simpleData)
.join('rect')
.attr('fill', d => color(d.name))
.attr('x', d => x(d.name))
.attr('y', d => y(d.value))
.attr('height', d => y(0) - y(d.value))
.attr('width', x.bandwidth())
xAxis(svg.append('g'))
yAxis(svg.append('g'))
svg.append("text")
.attr("x", (width/2))
.attr("y", margin.top)
.attr("text-anchor", "middle")
.attr("font-size", "24px")
.attr("font-family", "sans-serif")
.text(`Bar Chart Example`)
svg.append("text")
.attr("x", (width/2))
.attr("y", height)
.attr("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-family", "sans-serif")
.text("x-axis label")
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("y", 20)
.attr("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-family", "sans-serif")
.text("y-axis label")
return svg.node()
}
Insert cell
Insert cell
Insert cell
d3 = require('d3@5', 'd3-random@2')
Insert cell
randomWords = require('https://bundle.run/random-words@1.1.1')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more