Published
Edited
Apr 28, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
spanish_silver = d3.csvParse(await FileAttachment("spanish-silver.csv").text())
Insert cell
x_variable = "year"
Insert cell
y_variable = "silver_minted"
Insert cell
color_variable = "situados_paid"
Insert cell
chart_data = spanish_silver.map(d => {
return {
x: parseInt(d.year),
y: parseInt(d.silver_minted),
s: parseInt(d.situados_paid),
color: parseInt(d.was_american_revolution)
};
})
Insert cell
Insert cell
chart_domain_x = [d3.min(chart_data, d => d.x), d3.max(chart_data, d => d.x)]
Insert cell
chart_domain_y = [0, d3.max(chart_data, d => d.y)]
//chart_domain_y = [0, 1]
Insert cell
chart_domain_color = [0, 1, 2, 3, 4, 5]
Insert cell
x = d3
.scaleLinear()
.domain(chart_domain_x)
.range([margin.left + 20, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain(chart_domain_y)
.range([height - margin.bottom, margin.top]) //[margin.left, width - margin.right]
Insert cell
color = d3.scaleSequential()
.domain(chart_domain_color)
.interpolator(d3.interpolateBlues)
Insert cell
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
yAxis = g =>
g.attr("transform", `translate(${margin.left + 20}, 0)`).call(d3.axisLeft(y))
Insert cell
import { legend } from "@d3/color-legend"
Insert cell
Insert cell
mark1 = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(chart_data)
.join("rect")
.attr("x", d => x(d.x))
.attr("y", d => y(d.y))
.attr("height", d => y(0) - y(d.y))
.attr("width", 10);
return svg.node();
}
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);
svg
.append("g")
.selectAll("rect")
.data(chart_data)
.attr("fill", color(d => d.was_american_revolution))
.join("rect")
.attr("x", d => x(d.x))
.attr("y", d => y(d.y))
.attr("height", d => y(0) - y(d.y))
.attr("width", 10);
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
svg
.append("text")
.text("Spanish Silver Production Per Year")
.attr("transform", `translate(${width / 3},15)`);

// x-axis label
svg
.append("text")
.text("Year")
.attr("transform", `translate(${(width - margin.left) / 2}, 500)`);

// y-axis label
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -(height / 2))
.attr("y", 20)
.text("Silver count");
return svg.node();
}
Insert cell
line = d3
.line()
.x(d => x(d.x))
.y(d => y(d.y))
Insert cell
ourline = line(chart_data)
Insert cell
viewof t = Scrubber(d3.ticks(0, 1, 200), {
autoplay: false,
loop: false,
initial: 50,
format: x => `t = ${x.toFixed(3)}`
})
Insert cell
strokeDasharray = d3.interpolate(
`0,${lineLength}`,
`${lineLength},${lineLength}`
)
Insert cell
lineLength = svg`<path d="${line(chart_data)}">`.getTotalLength()
Insert cell
chart2 = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

// path with animation
const path = svg
.append("path")
.attr("d", ourline)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-miterlimit", 1)
.attr("stroke-dasharray", strokeDasharray(t));

const gx = svg.append("g").call(xAxis);
const gy = svg.append("g").call(yAxis);
svg
.append("text")
.text("Spanish Silver Production Per Year")
.attr("transform", `translate(${width / 2},15)`)
.style("text-anchor", "middle");

// x-axis label
svg
.append("text")
.text("Year")
.attr("transform", `translate(${(width - margin.left) / 2}, 500)`)
.style("text-anchor", "middle");

// y-axis label
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -(height / 2))
.attr("y", 20)
.text("Silver count")
.style("text-anchor", "middle");

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({ top: 20, right: 30, bottom: 30, left: 60 })
Insert cell
height = 500;

Insert cell
data = d3.csvParse(await FileAttachment("alphabet.csv").text())
Insert cell
import { Scrubber } from "@mbostock/scrubber"
Insert cell
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