Published
Edited
Jun 19, 2020
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("overflow", "visible");

svg.append("g")
.selectAll("g")
.data(series)
.enter().append("g")
.attr("fill", d => color(d.key))
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", d => x(d[0]))
.attr("y", (d, i) => y(d.data.name))
.attr("width", d => x(d[1]) - x(d[0]))
.attr("height", y.bandwidth())
.append("title")
.text(d => `${d.data.name} ${d.key}
${formatPercent(d[1] - d[0])} (${formatValue(d.data[d.key])})`);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

return svg.node();
}
Insert cell
data = d3.csvParse(await FileAttachment("지역별검색지유형1분포도.csv").text(), (d, i, columns) => (d3.autoType(d), d.total = d3.sum(columns, c => d[c]), d)).sort((a, b) => b["<10"] / b.total - a["<10"] / a.total)
Insert cell
series = d3.stack()
.keys(data.columns.slice(1))
.offset(d3.stackOffsetExpand)
(data)
.map(d => (d.forEach(v => v.key = d.key), d))
Insert cell
x = d3.scaleLinear()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.top, height - margin.bottom])
.padding(0.08)
Insert cell
count = 0;

Insert cell
color = d3.scaleOrdinal()
.domain(series.map(d => d.key))
.range( ["#9e0142", "#d53e4f", "#f46d43", "#fdae61", "#fee08b", "#ffffbf", "#e6f598", "#abdda4", "#66c2a5", "#3288bd", "#5e4fa2","#fc8d59", "#ffffbf", "#99d594"])
.unknown("#ccc")
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(width / 100, "%"))
.call(g => g.selectAll(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove())
Insert cell
formatPercent = d3.format(".1%")
Insert cell
formatValue = x => isNaN(x) ? "N/A" : x.toLocaleString("ko")
Insert cell
height = data.length * 40 + margin.top + margin.bottom
Insert cell
margin = ({top: 30, right: 10, bottom: 0, left: 30})
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
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