Public
Edited
Feb 15, 2024
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 == "Alarma"||"Impacto"||"Impactados"||"Servicios"||"Tipo"||"Tiempo"||"IE"||"Ambiente"||"RMA"||"TotalRMA"||"AFM"||"Plataforma") {
scales.set(
attribute,
d3.scaleOrdinal().domain(d3.map(data, d => d[attribute])).range([height - margin.bottom, height/2, margin.top])
);
}
else {
scales.set(
attribute,
d3.scaleLinear().domain(d3.extent(data, d => d[attribute])).range([height - margin.bottom, margin.top])
);
}
});
return scales;
}
Insert cell
Insert cell
Insert cell
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("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
.attr("stroke", d => col(d[colorAttribute])); // col is a function created in appendix
// 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
.each(function(d) { d3.select(this).call(d3.axisRight(y.get(d))); })
.call(g => g.append("text")
.attr("y", margin.bottom - 5)
.attr("x", -5)
.attr("text-anchor", "start")
.attr("fill", "currentColor")
.text(d => shortAttributeNames.get(d)))
.call(g => g.selectAll("text")
.clone(true).lower()
.attr("fill", "none")
.attr("stroke-width", 5)
.attr("stroke-linejoin", "round")
.attr("stroke", "white"));
function updateBrushing() {
// TODO implement brushing & linking
polylines.classed("hidden", d => {
var hidden;
attributes.forEach(function(key) {
if (!activeBrushes.has(key)) return;
else if (activeBrushes.get(key) === null) return;
else {
const [y0,y1] = activeBrushes.get(key);
if (!hidden) {
hidden = y0 > y.get(key)(d[key]) ||
y1 < y.get(key)(d[key])
}
};
});
return hidden;
});
}
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: 20, right: 30, bottom: 20, left: 20 })
Insert cell
data = d3.csvParse(await FileAttachment("test27.csv").text(), d3.autoType)
Insert cell
attributes = data.columns.filter(d => d !== "")
Insert cell
shortAttributeNames = new Map(
Object.entries({
Hostname: "Htn",
Impacto: "CYL",
Cantidad: "DPL",
Servicios: "Servicios",
capa: "WGT",
acceleration: "ACL",
year: "YEAR",
origin: "OGN"
})
)
Insert cell
// function created for assigning colors
col = ((colorAttribute == "Alarma") ? d3.scaleOrdinal(d3.schemeCategory10).domain(y.get(colorAttribute).domain().reverse()) : d3.scaleSequential().domain(y.get(colorAttribute).domain().reverse()).interpolator(d3.interpolateInferno))
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more