Published
Edited
Oct 23, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@5");
Insert cell
Insert cell
Insert cell
carData = {
yield fetch("https://vega.github.io/editor/data/cars.json")
.then(result => result.json());
}
Insert cell
Insert cell
svg`
<svg id="canvas">
<g class="vis">
<g class="marks"></g>
<g class="axis x"></g>
<g class="axis y"></g>
</g>
</svg>
`
Insert cell
Insert cell
height = 750
Insert cell
margin = 20
Insert cell
Insert cell
canvas = d3.select("#canvas")
.attr("width", width)
.attr("height", height);
Insert cell
Insert cell
marks = canvas.select(".vis .marks");
Insert cell
Insert cell
Insert cell
scaleX = d3.scaleLinear()
.domain([0, d3.max(carData.map(d => d["Horsepower"]))])
.range([0, width - 2*margin]);
Insert cell
scaleY = d3.scaleLinear()
.domain([0, d3.max(carData.map(d => d["Miles_per_Gallon"]))])
.range([height - 2*margin, 0]);
Insert cell
Insert cell
Insert cell
Insert cell
axisX = d3.axisBottom(scaleX);
Insert cell
axisY = d3.axisLeft(scaleY);
Insert cell
Insert cell
/*
{
d3.select("g.axis.x")
.call(axisX)
.attr("transform", `translate(0, ${height - margin - margin})`);
d3.select("g.axis.y").call(axisY);
}
*/
Insert cell
Insert cell
Insert cell
/*
pointMark = {
d3.select(".vis").attr("transform", `translate(${margin}, ${margin})`);
yield marks.selectAll("circle.point").data(carData)
.join("circle")
.attr("class", "point")
.attr("fill", "steelblue")
.attr("fill-opacity", 0.73)
.attr("r", 5)
.attr("cx", d => scaleX(d["Horsepower"]))
.attr("cy", d => scaleY(d["Miles_per_Gallon"]));
}
*/
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
xEncoding = "Horsepower"
Insert cell
yEncoding = "Miles_per_Gallon"
Insert cell
Insert cell
Insert cell
Insert cell
// your code goes here:
// myEncoding =
Insert cell
Insert cell
// your code goes here:
// (make sure the scale is a notebook variable like the other scales above)
Insert cell
Insert cell
// your code goes here:
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/*
{
// zoom modifies the scales by applying a transformation to it
// this transform is however cumulative, so we need to maintain a copy
// of the original scales to not overemphasize interactions
// try replacing the scaleXCopy inside the zoom event listener to see
// how that influences the behavior of the zoom
const scaleXCopy = scaleX.copy();
const scaleYCopy = scaleY.copy();
// generator function for zoom
const zoom = d3.zoom()
.on("zoom", () => {
// applying the zoom transformation to the vertical and horizontal
// scales
const rescaledX = d3.event.transform.rescaleX(scaleXCopy);
const rescaledY = d3.event.transform.rescaleY(scaleYCopy);
// updating the point mark
marks.selectAll("circle.point")
.attr("cx", d => rescaledX(d.Horsepower))
.attr("cy", d => rescaledY(d.Miles_per_Gallon));
// reconfiguring the axis generators
axisX.scale(rescaledX);
axisY.scale(rescaledY);
// redrawing the axes
d3.select("g.axis.x").call(axisX)
d3.select("g.axis.y").call(axisY);
});
// calling the zoom generator on a rectangle selection. All zoom events on
// this rectangle receives all zoom events and is not influenced by the
// transformation, thus it always stays in place.
canvas.append("rect")
.attr("id", "zoomHelper")
.attr("fill", "transparent")
.attr("width", width)
.attr("height", height)
.call(zoom);
}
*/
Insert cell
/*
{
const tooltip = d3.select(".vis .tooltip");
let res = null;
pointMark.on("mouseenter", d => {
res = d;
console.log(d);
})
yield res;
}
*/
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