Public
Edited
Feb 14, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dataMatrix = {
let matrix = JSON.parse(JSON.stringify(data.nodes)) //clone the array data.nodes

//Initialize the matrix connections
for(let i in matrix){
matrix[i].connections = new Array(matrix.length).fill(0);
}

//For all the links, update the corresponding cell in the matrix
for(let j in data.links){
let source = data.links[j].source;
let target = data.links[j].target;
matrix[source].connections[target] = data.links[j].score;
}

return matrix;
}
Insert cell
Insert cell
nodelinkData = {
let nldata;
//TODO
//Create the node array from dataMatrix
//Create the link array from dataMatrix
return nldata;
}
Insert cell
Insert cell
Insert cell
Insert cell
matrixChart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

var chart = svg.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

//Sort according to bloodTypes (and within bloodTypes to id)
var graph = dataMatrix.sort(function (a, b) {
if(a.bloodType !== b.bloodType){
if(a.bloodType > b.bloodType) return 1;
else return -1;
}else{
if(a.donor_bloodType >= b.donor_bloodType) return 1;
else return -1;
}
});

//Create a row on the matrix
var row = svg.selectAll('g.row')
.data(graph)
.join('g')
.attr('class', 'row')
.attr('transform', function (d, i) { return 'translate(0,' + x(i) + ')'; })
.each(makeRow)

//function that creates the rects of a row
function makeRow(d, i) {
const rowData = d;
var cell = d3.select(this).selectAll('rect')
.data(graph)
.join('rect')
.attr('x', (d, i) => x(i))
.attr('width', x.bandwidth())
.attr('height', x.bandwidth())
.style('fill-opacity', (d,i) => opacity(rowData.connections[graph[i].id]))
.style('fill', (d,i) => {
if (rowData.connections[graph[i].id] > 0 && rowData.bloodType === d.bloodType) {
return colorScale(rowData.bloodType);
} else if (rowData.connections[graph[i].id] > 0) {
return '#555';
} else {
return '#ccc';
}
})
}

//Add the label at the begining of the row
row.append('text')
.attr('class', 'label')
.attr('x', margin.left - 4)
.attr('y', x.bandwidth() / 2)
.attr('dy', '0.32em')
.attr('font-size', '10px')
.attr('text-anchor', 'end')
.text((d,i) => d.id + "," + graph[i].outdegree+ "," + graph[i].bloodType+ "," + graph[i].donor_bloodType);
//Add the label at the top of the column
var column = svg.selectAll('g.column')
.data(graph)
.join('g')
.attr('class', 'column')
.attr('transform', function(d, i) { return 'rotate(-90, '+margin.top+', '+margin.left+')'; })
.append('text')
.attr('class', 'label')
.attr('x', margin.top + 4)
.attr('y', (d,i) => x(i) + x.bandwidth() / 2)
.attr('dy', '0.32em')
.attr('font-size', '10px')
.text((d,i) => d.id + "," + graph[i].indegree+ "," + graph[i].bloodType+ "," + graph[i].donor_bloodType)
return svg.node();
}

Insert cell
colorScale = d3.scaleOrdinal(d3.schemeCategory10);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const links = data.links.map(d => Object.create(d)); //Create a copy of the original data to don't modify it
const nodes = data.nodes.map(d => Object.create(d)); //Create a copy of the original data to don't modify it
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).distance(d=>linkScale(data.links[d.index].score)))
.force("charge", d3.forceManyBody().strength(-100))
.force("center", d3.forceCenter(width / 2, height / 2).strength(0.8));

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const link = svg.append("g")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", (d,i)=> linkcolor(data.links[i].score))
.attr("stroke-width", d => Math.sqrt(d.score)*0.15);

const node = svg.append("g")
.attr("stroke", "#bbb")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", color)
.call(drag(simulation));

var texts = svg.selectAll(".texts")
.data(nodes)
.join("text")
.attr("dy", -4)
.attr("font-family", "sans-serif")
.attr('font-size', '10px')
.attr('text-anchor', 'middle')
.text( d => d.index );

simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
texts
.attr("x", d => d.x)
.attr("y", d => d.y);
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
drag = simulation => {
//drag manages the drag events
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
Insert cell
linkcolor = d3.scaleSequential(d3.interpolateViridis)
.domain(d3.extent(data.links.map(d=>d.score)));
Insert cell
linkScale = d3.scaleLinear()
.domain(d3.extent(data.links.map(d=>d.score)))
.range([150,50]);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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