Published
Edited
Feb 3, 2021
2 stars
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
// Add an x title
svg.append("text")
.attr("transform", `translate(${margin.left + (x.range()[1] - x.range()[0])/2},${height})`)
.style("text-anchor", "middle")
.text("title")

return svg.node();
}
Insert cell
x = d3.scaleLinear()
.domain([0, 1])
.range([margin.left, width - margin.right])
Insert cell
x(1)
Insert cell
y = d3.scaleLinear()
.domain([0, 1])
.range([height - margin.bottom, margin.top])
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 },0)`)
.call(d3.axisLeft(y))
Insert cell
margin = ({top: 20, right: 30, bottom: 40, left: 100})
Insert cell
height = 500
Insert cell
Insert cell
Insert cell
data = _.range(500).map((d,i) => ({id:i, x:Math.random(), y:Math.random(), color: Math.random()}))
Insert cell
render_data_table(data)
Insert cell
// Create a scatterplot of the `data` array
scatter = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
// Add an x title
svg.append("text")
.attr("transform", `translate(${margin.left + (x.range()[1] - x.range()[0])/2},${height})`)
.style("text-anchor", "middle")
.text("title")

const circles = svg.selectAll("circle")
.data(data, d => d.id)
.join("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", 5)
.style("fill", "rgb(73, 141, 201)")
return svg.node();
}
Insert cell
Insert cell
// Create a scatterplot of the `data` array
animated_scatter = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg.append("g").call(xAxis);

svg.append("g").call(yAxis);

// Add an x title
svg
.append("text")
.attr(
"transform",
`translate(${margin.left + (x.range()[1] - x.range()[0]) / 2},${height})`
)
.style("text-anchor", "middle")
.text("title");

const circles = svg
.selectAll("circle")
.data(data, d => d.id)
.join("circle")
.attr("cy", height - margin.bottom)
.attr("cx", d => x(d.x))
.attr("r", 5)
.style("fill", "rgb(73, 141, 201)")
.transition()
.duration(1000)
.delay(d => x(d.x))
.attr("cy", d => y(d.y));

return svg.node();
}
Insert cell
Insert cell
Insert cell
// Create a chart here where you explicitly manage the updating of elements
// Create a scatterplot of the `data` array
custom_scatter = {
let svg, x_axis, y_axis, x_label;
if (!this) {
svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

x_axis = svg
.append("g")
.attr("class", "x_axis")
.call(xAxis);

y_axis = svg
.append("g")
.attr("class", "y_axis")
.call(yAxis);

// Add an x title
x_label = svg
.append("text")
.attr(
"transform",
`translate(${margin.left +
(x.range()[1] - x.range()[0]) / 2},${height})`
)
.style("text-anchor", "middle")
.text("title");
} else {
svg = d3.select(this);
x_axis = svg.select(".x_axis");
y_axis = svg.select(".y_axis");
}

const chart_data = data.filter(d => d.color < max_color);
const circles = svg
.selectAll("circle")
.data(chart_data, d => d.id)
.join(
enter =>
enter
.append("circle")
.attr("cy", height - margin.bottom)
.attr("cx", d => x(d.x))
.attr("r", 5)
.style("fill", "rgb(73, 141, 201)"),
update => update,
exit =>
exit
.style("fill", "red")
.transition()
.duration(1000)
.delay(d => x(1 - d.x))
.attr("cy", height - margin.bottom)
.remove()
)
.transition()
.duration(1000)
.delay(d => x(d.x))
.attr("cy", d => y(d.y));

return svg.node();
}
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
import {table_styles, render_data_table} from "@uw-info474/utilities"
Insert cell
table_styles
Insert cell
_ = require("lodash")
Insert cell
import {slider} from "@jashkenas/inputs"
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