paracoords = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("style")
.text("path.hidden { stroke: #000; stroke-opacity: 0.01;}");
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");
function path(d){
console.log("cross: "+ (d3.cross(attributes, [d], (key, d) => [key, d[key]])));
return d3.line()
.defined(([, value]) => value != null)
(attributes.map(function(p) { return [x(p), y.get(p)(d[p])]; }))
}
//retun a color for each path corresponding the selected color attribute and its scale
polylines.attr("stroke", d => colorScale.get(colorAttribute)(d[colorAttribute]))
.attr("stroke-opacity", 0.8)
//.attr("d", path);
.attr("d", path);
//var debug = attributes.map(d=>x(d))
// create the group nodes for the axes
const axes = svg
//SVG Group Element and D3.js
//https://www.dashingd3js.com/svg-group-element-and-d3js
.append("g")
//create many elements for each data point
//https://bost.ocks.org/mike/join/
.selectAll("g")
.data(attributes)
.join("g")
//${}: It is used to embed the value into the string
.attr("transform", d => `translate(${x(d)},0)`);
//debugger;
// TODO: add the visual representation of the axes
//Reference:
//https://www.tutorialspoint.com/d3js/d3js_axis_api.htm
//https://www.d3-graph-gallery.com/graph/parallel_basic.html
//https://observablehq.com/@d3/parallel-coordinates
//selection.each(function) to invoke the specified function for each selected element
//let test =
axes.each(function(d) {
console.log("axes argument: " + d);
console.log("axes argument: " + this);
//Add scales to the y-axis
//The map.get() function in D3.js is used to return the value for the specified key string.
//https://www.tutorialspoint.com/d3js/d3js_axis_api.htm
var y_axis = d3.axisRight(y.get(d));
//In d3, `this` refers to the current node, the current DOM element.
d3.select(this)
//Selection.call(): https://github.com/d3/d3-selection#selection_call
.call(y_axis.ticks());
})
//console.log(test)
.call(g => g.append("text")
.attr("y", 7)
.attr("x", -10)
.attr("text-anchor", "start")
.attr("fill", "currentColor")
.text(d => shortAttributeNames.get(d)))
//make stick not covered by the color
.call(g => g.selectAll("text")
.clone(true).lower()
.attr("fill", "none")
.attr("stroke-width", 5)
.attr("stroke-linejoin", "round")
.attr("stroke", "white"));
//
console.log(polylines);
function updateBrushing() {
// TODO implement brushing & linking
//The code structure and approach are taken from Assignment 2
polylines.classed("hidden", d => {
//let isHidden = false;
//The some() method checks if any of the elements in an array pass a test
//https://www.w3schools.com/jsref/jsref_some.asp
//Only check the attribute that activeBrushes store the key
//The some() method checks if any of the attribute in an array pass a test
//As long as it turn true, it will return true
let isHidden = attributes.filter(f => activeBrushes.has(f)).some ( attribute =>
activeBrushes.get(attribute)[0] > y.get(attribute)(d[attribute])
||
activeBrushes.get(attribute)[1] < y.get(attribute)(d[attribute])
);
console.log(isHidden);
return isHidden;
});
}
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();
}