Public
Edited
Apr 19, 2023
Insert cell
datanew = FileAttachment("mvc_clean - Sheet8.csv").csv({typed:true})
Insert cell

function fetch_or_create_category(list_of_categories, target_name) {
for (let category of list_of_categories) {
if (category["name"] == target_name) { // if we find a match
return category; // ends the function
}
}
let result = { "name": target_name, "children": [] };
list_of_categories.push(result);
return result;
}
Insert cell
function hierarchicalize(csv_data) {
let all_types = [];
let result = { "name": "top", "children": all_types};
for (let row of csv_data) {
let l1 = row["Party Name"]; // "A"
let l2 = row["Big Affiliation"]; // "1"
let item = row["Donor Name"]; // "I"
let value = parseFloat(row["Value"]); // 12
// l1group will be something like:
// { "name": "A", "children": [] }
let l1group = fetch_or_create_category(all_types, l1);
let l2group = fetch_or_create_category(l1group["children"], l2);
let new_entry = { "name": item, "value": value };
l2group["children"].push(new_entry);
}
return result;
}
Insert cell
flare = hierarchicalize(datanew);
Insert cell
mvc_clean - Sheet8.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
words = function(text) {
const words = text.split(/\s+/g); // To hyphenate: /\s+|(?<=-)/
if (!words[words.length - 1]) words.pop();
if (!words[0]) words.shift();
return words;
}
Insert cell
measureWidth = {
const context = document.createElement("canvas").getContext("2d");
return text => context.measureText(text).width;
}
Insert cell
lineHeight = 18
Insert cell
targetWidth = text => Math.sqrt(measureWidth(text.trim()) * lineHeight)
Insert cell
lines = function(words) {
let line;
let lineWidth0 = Infinity;
const lines = [];
for (let i = 0, n = words.length; i < n; ++i) {
let lineText1 = (line ? line.text + " " : "") + words[i];
let lineWidth1 = measureWidth(lineText1);
if ((lineWidth0 + lineWidth1) / 2 < targetWidth(words.join(" ")) ) {
line.width = lineWidth0 = lineWidth1;
line.text = lineText1;
} else {
lineWidth0 = measureWidth(words[i]);
line = {width: lineWidth0, text: words[i]};
lines.push(line);
}
}
return lines;
}
Insert cell
textRadius = function(lines) {
let radius = 0;
for (let i = 0, n = lines.length; i < n; ++i) {
const dy = (Math.abs(i - n / 2 + 0.5) + 0.5) * lineHeight;
const dx = lines[i].width / 2;
radius = Math.max(radius, Math.sqrt(dx ** 2 + dy ** 2));
}
return radius;
}
Insert cell
Insert cell
Insert cell
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