Published
Edited
Jun 9, 2020
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) {
scales.set(
attribute,
d3.scaleLinear().range([height - margin.bottom, margin.top])
// .domain(d3.extent(data , function (d) {return +d[attributes];}))
.domain (d3.extent(data, function (d) {return d[attribute];}))// add the scale for each attribute
// .domain(d3.extent(data, d => d[attribute]))
);
});

return scales;
}

Insert cell
Insert cell
Insert cell
Insert cell
// z has been defined in appandex to create colore scale for each attribut and called in below code
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
// added the polylines by using below code, used the curveCatmullRomm. Ref// https://observablehq.com/@d3/parallel-coordinates
.attr("d", d => d3.line()
.curve(d3.curveCatmullRom)
.defined(([, value]) => value != null)
.y(([key, value]) => y.get(key)(value))
.x(([key]) => x(key))
(d3.cross(attributes, [d], (key, d) => [key, d[key]])))
// TODO: apply the color scale from task 3
// z was already defiend in appendix and in here I called it to apply the color scale
.attr("stroke", d => z(d[colorAttribute]));
// 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
//added label for axes and gived some style
.each(function(d) {
d3.select(this).call(d3.axisRight(y.get(d))); })
.call(g => g
.attr("stroke-width" , 1)
.append("text")
.attr("x", margin.right)
.attr("y", 9)
.attr("text-anchor", "middle")
.attr("fill", "green")
.attr("font-size",12)
.text(d => shortAttributeNames.get(d)))
;
function updateBrushing() {
// TODO implement brushing & linking
// In here the resutl will be fales first and then we check if the area is selected then hide the other parts and only highlight the selected area ( return the result).
svg.selectAll("path")
.classed("hidden", d => {
var result = false;
attributes.forEach(p => {
if(activeBrushes.get(p) != null){
var y1 = activeBrushes.get(p)[1];
var y0 = activeBrushes.get(p)[0];
if(y0 > y.get(p)(d[p])
|| y1< y.get(p)(d[p])){
result = true;
}
}
})
return result;
});
}


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
Insert cell
md`## Appendix`
Insert cell
height = 500
Insert cell
margin = ({ top: 10, right: 30, bottom: 10, left: 10 })
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"
})
)
Insert cell
z = d3.scaleSequential(y.get(colorAttribute).domain().reverse(), d3.interpolateBrBG)
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