Public
Edited
Sep 30, 2023
Insert cell
Insert cell
Insert cell
Insert cell
populationData
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
populationData
Insert cell
keys = ["<10", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "≥80"]
Insert cell
Insert cell
stackLayout = d3.stack()
.keys(keys)
.offset(d3.stackOffsetExpand)(populationData) // 割合に変換
Insert cell
{
const width = 900;
const height = 300;
const marginTop = 25;
const marginRight = 20;
const marginBottom = 35;
const marginLeft = 60;

const x = d3
.scaleBand()
.domain(Array.from(new Set(populationData.map((d) => d.name))))
.range([0, width]);

const y = d3
.scaleLinear()
.domain([0, d3.max(stackLayout, (d) => d3.max(d, (d) => d[1]))]) // stackLayoutは配列の配列なのでmaxを2回使う
.nice()
.range([height, 0]);


const color = d3
.scaleOrdinal()
.domain(keys)
.range(d3.schemeSpectral[keys.length]);

const svg = d3
.create("svg")
.attr("width", width + marginLeft + marginRight)
.attr("height", height + marginTop + marginBottom);

const g = svg
.append("g")
.attr("transform", `translate(${marginLeft}, ${marginTop})`);

// Append a group for each series, and a rect for each element in the series.
g.append("g")
.selectAll(".bucket") // 9つの配列に割り当てられたデータをすべて選択
.data(stackLayout)
.join("g")
.attr("class", "bucket") //
.attr("fill", (_, i) => color(keys[i]))
.selectAll("rect")
.data((d) => d)
.join("rect")
.attr("x", (d) => x(d.data.name))
.attr("y", (d) => y(d[1]))
.attr("height", (d) => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth());

g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));

g.append("g").call(d3.axisLeft(y));

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const width = 200;
const height = 200;
const margin = { top: 20, bottom: 20, left: 20, right: 20 };

const svg = d3
.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);

const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

const rectangle = g
.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "#ddd")
.style("stroke", "#000")
// mouseenter
.on("mouseenter", (event) => {
rectangle.style("fill", "tomato");
})
// mouseleave
.on("mouseleave", (event) => {
rectangle.style("fill", "blue")
})
// click
.on("click", (events) => {
rectangle
.transition()
.duration(2000)
.attr("width", 0)
.attr("height", 0)
.attr("x", width / 2)
.attr("y", height / 2);
});

const circle = g
.append("circle")
.style("pointer-events", "none")
.attr("r", 5);

// mousemove(マウスを離す)
svg.on("mousemove", (event) => {
// console.log(event);
circle.attr("cx", event.layerX - 20);
circle.attr("cy", event.layerY - 20);
});

return svg.node();
}
Insert cell
Insert cell
Insert cell
{
const width = 600;
const height = 400;
const margin = { top: 30, bottom: 20, left: 50, right: 50 };

const div = htl.html`<div></div>`;

const categories = d3.groups(iris, (d) => d.species);

const buttons = d3
.select(div)
.selectAll("button")
.data(categories)
.join("button");

buttons.text((d) => d[0]);

const svg = d3
.select(div)
.append("div")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);

const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

g.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "#eee");

// 表示するテキスト
const text = svg
.append("text")
.style("font", "12px sans-serif")
.attr("y", 15)
.text("Click on a circle to read the data!");

const x = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalLength"]))
.nice()
.range([0, width]);

const y = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalWidth"]))
.nice()
.range([height, 0]);

const color = d3
.scaleOrdinal()
.domain(categories.map((d) => d[0]))
.range(d3.schemeDark2);

const yAxis = g.append("g").call(d3.axisLeft(y).tickSize(-width));

const xAxis = g
.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickSize(-height));

yAxis.selectAll("line").style("stroke", "#fff");
xAxis.selectAll("line").style("stroke", "#fff");

const circles = g
.selectAll("circle")
.data(iris.filter((d) => d.species == "setosa"))
.join("circle")
.attr("r", 4)
.style("stroke", "#000")
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => color(d.species))
.style("cursor", "pointer") // ポインターに変換
// クリックするとデータを表示する
.on("click", (event, d) => {
text.text(JSON.stringify(d));
});


buttons.on("click", (event, d) => {
g.selectAll("circle")
.data(d[1], (d) => d)
.join(
(enter) =>
enter
.append("circle")
.attr("r", 4)
.style("stroke", "#000")
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => color(d.species))
.style("cursor", "pointer")
.on("click", (event, d) => {
text.text(JSON.stringify(d));
}),
(update) =>
update
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => color(d.species)),
(exit) =>
exit
.attr("fill", "red")
.call((exit) =>
exit.transition().duration(1000).attr("r", 0).remove()
)
);
});

return div;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
iris.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

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