Public
Edited
Nov 15, 2023
Fork of Simple D3
Insert cell
Insert cell
chart = {
const width = 928; // uncomment for responsive width
const height = 600;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");

let g = svg
.selectAll("g")
.data(dots)
.join("g")
.attr("fill-rule", "evenodd")
.attr("transform", (d, i) => `translate(${100 + i * 150} 400)`);

g.selectAll("path.house")
.data((d) => d.dots)
.join("path")
.classed("house", "true")
.attr(
"d",
"M 8 18 L 12 18 L 12 14 L 14 14 L 14 18 L 20 18 L 20 10 L 22 10 L 14 4 L 6 10 L 8 10 Z"
)
.attr("transform", (d) => `translate(${d.y * 22}, ${-d.x * 22})`)
.attr("fill", (d) => colours[d.colour]);

g.append("text")
.text((d) => d.place)
.style("fill", "black")
.attr("y", 40);

return svg.node();
}
Insert cell
tallChart = {
const width = 928; // uncomment for responsive width
const height = 900;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");

let g = svg
.selectAll("g")
.data(tallDots)
.join("g")
.attr("fill-rule", "evenodd")
.attr("transform", (d, i) => `translate(${100 + i * 150} 800)`);

g.selectAll("path.house")
.data((d) => d.dots)
.join("path")
.classed("house", "true")
.attr(
"d",
"M 8 18 L 12 18 L 12 14 L 14 14 L 14 18 L 20 18 L 20 10 L 22 10 L 14 4 L 6 10 L 8 10 Z"
)
.attr("transform", (d) => `translate(${d.y * 22}, ${-d.x * 22})`)
.attr("fill", (d) => colours[d.colour]);

g.append("text")
.text((d) => d.place)
.style("fill", "black")
.attr("y", 40);

return svg.node();
}
Insert cell
ourChart = {
const width = 928; // uncomment for responsive width
const height = 600;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");

let boxes = svg
.selectAll("path")
.data(ourMethodDots.boxes)
.join("path")
.attr("transform", `translate(100 420)`)
.attr("d", (d) => boxPath(d))
.style("fill", "none")
.style("stroke", "#444")
.attr("stroke-dasharray", "4 2");

let g = svg
.selectAll("g")
.data([ourMethodDots.dots])
.join("g")
.attr("fill-rule", "evenodd")
.attr("transform", (d, i) => `translate(${100 + i * 150} 400)`);

g.selectAll("path.house")
.data((d) => d)
.join("path")
.classed("house", "true")
.attr(
"d",
"M 8 18 L 12 18 L 12 14 L 14 14 L 14 18 L 20 18 L 20 10 L 22 10 L 14 4 L 6 10 L 8 10 Z"
)
.attr("transform", (d) => `translate(${d.y * 22}, ${-d.x * 22})`)
.attr("fill", (d) => colours[d.colour]);

g.append("text")
.text((d) => d.place)
.style("fill", "black")
.attr("y", 40);

return svg.node();
}
Insert cell
function boxPath({ start, end }) {
let x0 = start.row * 22 + 2,
y0 = -start.column * 22,
x1 = end.row * 22,
y1 = -end.column * 22 + 4;
let path = `M ${x0 + 4} ${y0} L ${x0 + 22} ${y0} L ${x0 + 22} 0 L ${
x1 + 22
} 0 L ${x1 + 22} ${y1} L ${x1} ${y1} L ${x1} -218 L ${x0 + 4} -218 Z`;
console.log(path);
return path;
}
Insert cell
PER_DOT = 10
Insert cell
d3.schemeSet3
Insert cell
colours = ({
red: d3.schemeTableau10[2],
yellow: d3.schemeTableau10[5],
blue: d3.schemeTableau10[0]
})
Insert cell
tallDots = counts.map((place) => {
let d = { place: place.place, dots: [] };
let row = 0;
for (let cat of cats) {
let column = 0;
for (let i = 0; i < place[cat]; i++) {
d.dots.push({ colour: cat, x: column, y: row });
++column;
}
row++;
}
return d;
})
Insert cell
dots = counts.map((place) => {
let d = { place: place.place, dots: [] };
let row = 0;
for (let cat of cats) {
let column = 0;
for (let i = 0; i < place[cat]; i++) {
if (column === PER_DOT) {
column = 0;
++row;
}
d.dots.push({ colour: cat, x: column, y: row });
++column;
}
row++;
}
return d;
})
Insert cell
ourMethodDots = {
let dots = [];
let boxes = [];
let row = 0;
let column = 0;
counts.forEach((place) => {
let start = { row, column };
for (let cat of cats) {
for (let i = 0; i < place[cat]; i++) {
if (column === PER_DOT) {
column = 0;
++row;
}
dots.push({ colour: cat, x: column, y: row });
++column;
}
}
let end = { row, column };
boxes.push({ start, end });
});
return { dots, boxes };
}
Insert cell
cats = ["red", "yellow", "blue"]
Insert cell
counts = FileAttachment("sample.csv").csv()
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