Published
Edited
Jun 8, 2020
1 fork
Insert cell
Insert cell
x = d3.scalePoint(attributes, [margin.left, width - margin.right])
Insert cell
Insert cell
y = {
let scales = new Map();

// TODO: create a suitable scale for each attribute and add it to the map
attributes.forEach(function(attribute) {
if(attribute=="origin") {
scales.set(
attribute,
d3.scaleOrdinal().range([height - margin.bottom, // for 3 types => 3 values on axis
(height - margin.bottom) / 2,
margin.top])
.domain(d3.map(data, d => d[attribute])));
} else {
scales.set(
attribute,
d3.scaleLinear().range([height - margin.bottom, margin.top])
.domain(d3.extent(data, d => d[attribute]))); // set max and min of
// each attribute to domain
}
});

return scales;
}
Insert cell
Insert cell
Insert cell
Insert cell
// color scale for each attribute
z = {
if(colorAttribute === "origin") { // set color for each type of origin => ordinal scale
return d3.scaleOrdinal(y.get(colorAttribute).domain(), ["#4e79a7","#f28e2c","#e15759"]);
} else {
return d3.scaleSequential(y.get(colorAttribute).domain(), d3.interpolateBrBG);
}
}
Insert cell
Insert cell
Insert cell
paracoords = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

// set the style of hidden data items
svg
.append("style")
.text("path.hidden { stroke: #000; stroke-opacity: 0.01;}");

// a map that holds any active brush per attribute
let activeBrushes = new Map();

const polylines = svg
.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.attr("stroke-opacity", 0.4)
.selectAll("path")
.data(data)
.join("path")
// TODO: create the polylines
.attr("stroke", "black") // use color black for drawing polylines without the color scale (before task 3)
.attr("d", d => d3.line()
.defined(([, value]) => value != null) // check the input value
.x(([key]) => x(key)) // x is the attribute
.y(([key, value]) => y.get(key)(value)) // use y to get the value of the attribute
(d3.cross(attributes, [d], (key, d) => [key, d[key]])))

// TODO: apply the color scale from task 3
// .attr("stroke", d => d = d3.scaleSequential(y.get(colorAttribute).domain(), d3.interpolateBlues)(d[colorAttribute]))
.attr("stroke", d => z(d[colorAttribute])); // z was define above (task 3) for cleaner code here
// create the group nodes for the axes
const axes = svg
.append("g")
.selectAll("g")
.data(attributes)
.join("g")
.attr("transform", d => `translate(${x(d)},0)`);

// TODO: add the visual representation of the axes
axes.append("g")
// match axes to function y to get the scales
.each(function(d) {d3.select(this).call(d3.axisRight(y.get(d)));})
// add label for each axis with shortAttributeNames
.call(g => g.append("text")
.attr("x", margin.left - 15)
.attr("y", 7.5)
.attr("text-anchor", "end")
.attr("fill", "currentColor")
.text(d => shortAttributeNames.get(d)))
// add style for text for a better appearance (SOURCE: d3 example from above)
.call(g => g
.selectAll("text")
.clone(true)
.lower()
.attr("fill", "none")
.attr("stroke-with", 5)
.attr("stroke-linejoin", "round")
.attr("stroke", "gray"));

function updateBrushing(i) {
// TODO implement brushing & linking
if(d3.event.selection === null) {
polylines.classed("hidden", false);
}
polylines.classed("hidden", d => {
for(var i = 0; i< attributes.length; ++i) { // check if attribute is in
// activeBrushes
var range_y = activeBrushes.get(attributes[i]);
// get y_coordinate of polylines in current attribute
var lines_y = y.get(attributes[i])(d[attributes[i]]);
if(range_y != null && (lines_y < range_y[0] || lines_y > range_y[1])) {
return true;
}
}
return false;
});
}

function brushed(attribute) {
activeBrushes.set(attribute, d3.event.selection);
updateBrushing();
}

function brushEnd(attribute) {
if (d3.event.selection !== null) return;
activeBrushes.delete(attribute);
updateBrushing();
}

const brushes = axes.append("g").call(
d3
.brushY()
.extent([[-10, margin.top], [10, height - margin.bottom]])
.on("brush", brushed)
.on("end", brushEnd)
);

return svg.node();
}
Insert cell
md`## Appendix`
Insert cell
height = 500
Insert cell
margin = ({ top: 10, right: 30, bottom: 10, left: 20 })
Insert cell
data = d3.csvParse(await FileAttachment("cars.csv").text(), d3.autoType)
Insert cell
attributes = data.columns.filter(d => d !== "name")
Insert cell
shortAttributeNames = new Map(
Object.entries({
mpg: "MPG",
cylinders: "CYL",
displacement: "DPL",
horsepower: "HP",
weight: "WGT",
acceleration: "ACL",
year: "YEAR",
origin: "OGN"
})
)
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
d3 = require("d3@5")
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