File size: 6,158 Bytes
5f5806d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Segoe UI", sans-serif;
text-align: center;
margin-top: 20px;
}
svg {
font: 10px sans-serif;
}
.label {
font-weight: bold;
font-size: 12px;
text-anchor: middle;
}
.legend {
font-size: 12px;
text-anchor: start;
}
.legend rect {
stroke-width: 1;
stroke: #000;
}
</style>
<body>
<h2>Sunburst Chart: Country → Profession</h2>
<button id="downloadBtn">Download SVG</button>
<svg width="800" height="800"></svg>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
const width = 800;
const radius = width / 3;
const colorMap = {
"Adult Performer": "#8A2BE2",
"Model": "#DC143C",
"Actor": "#FF7F50",
"Performer": "magenta",
"Singer, Musician": "wheat",
"TV Personality": "#708090",
"Sports Professional": "gold",
"Public Figure": "maroon",
"Voice Actor": "lightgreen",
"Online Personality": "#4682B4",
"Other": "#ccc"
};
// Count total values per gender (assuming gender is at depth 1)
const professionOrder = [
"Adult Performer", "Actor", "Singer, Musician", "Model", "Online Personality", "Public Figure", "Sports Professional", "Voice Actor", "TV Personality", "Other"
];
const manualOrder = [
"United States", "Japan", "South Korea", "United Kingdom",
"Russia", "China", "Canada", "India", "Australia", "France", "Germany", "Other"
];
function customSort(a, b, order) {
const aIndex = order.indexOf(a.data.name);
const bIndex = order.indexOf(b.data.name);
return (aIndex === -1 ? Infinity : aIndex) - (bIndex === -1 ? Infinity : bIndex);
}
function partition(data) {
const root = d3.hierarchy(data).sum(d => d.value);
// Sort countries by manual order before partitioning
root.children.sort((a, b) => customSort(a, b, manualOrder));
// Sort professions by value (descending), with "Other" always last
root.children.forEach(country => {
if (country.children) {
country.children.sort((a, b) => {
if (a.data.name === "Other") return 1;
if (b.data.name === "Other") return -1;
return b.value - a.value;
});
}
});
return d3.partition().size([2 * Math.PI, root.height + 1])(root);
}
d3.json("json/sunburst_gender.json").then(data => {
const root = partition(data);
root.each(d => d.current = d);
// Sort countries with 'Other' last
root.children.sort((a, b) => customSort(a, b, manualOrder));
// Sort professions within countries with 'Other' last
root.children.forEach(country => {
if (country.children) {
country.children.sort((a, b) => customSort(a, b, professionOrder));
}
});
const svg = d3.select("svg")
.attr("viewBox", [0, 0, width, width])
.style("font", "10px sans-serif");
const g = svg.append("g")
.attr("transform", `translate(${width / 2},${width / 2})`);
const angleOffset = -80 * Math.PI / 180;
const arc = d3.arc()
.startAngle(d => d.x0 + angleOffset)
.endAngle(d => d.x1 + angleOffset)
.innerRadius(d => {
if (d.depth === 1) return radius * 0.1;
if (d.depth === 2) return radius * 0.5;
})
.outerRadius(d => {
if (d.depth === 1) return radius * 0.5;
if (d.depth === 2) return radius * 0.65;
});
const path = g.append("g")
.selectAll("path")
.data(root.descendants().slice(1))
.join("path")
.attr("fill", d => {
if (d.depth === 2) return colorMap[d.data.name] || "#ccc";
if (d.depth === 1) return "#fff";
return "none";
})
.attr("stroke", d => d.depth === 1 ? "#000" : null)
.attr("stroke-width", d => d.depth > 0 ? 2 : null)
.attr("d", arc);
path.append("title")
.text(d => `${d.ancestors().map(d => d.data.name).reverse().join(" → ")}\n${d.value}`);
// Count total values per gender (assuming gender is at depth 1)
const genderCounts = {};
root.children.forEach(d => {
const gender = d.data.name;
genderCounts[gender] = d.value;
});
// Add bold labels to country segments
g.selectAll("text")
.data(root.children)
.join("text")
.attr("transform", function(d) {
const angle = ((d.x0 + d.x1) / 2) + angleOffset;
const x = Math.cos(angle - Math.PI / 2) * (radius / root.height + -50);
const y = Math.sin(angle - Math.PI / 2) * (radius / root.height + -50);
return `translate(${x},${y}) rotate(${(angle / Math.PI)})`;
})
.attr("dy", "0.35em")
.attr("class", "label")
.text(d => `${d.data.name}: ${genderCounts[d.data.name] || 0}`);
// Count total values per profession (depth 2)
const professionCounts = {};
root.descendants().forEach(d => {
if (d.depth === 2) {
const name = d.data.name;
professionCounts[name] = (professionCounts[name] || 0) + d.value;
}
});
// Legend for profession colors
const legend = svg.append("g")
.attr("class", "legend")
.attr("transform", `translate(20,20)`);
professionOrder.forEach((prof, i) => {
const row = legend.append("g")
.attr("transform", `translate(0,${i * 20})`);
row.append("rect")
.attr("width", 16)
.attr("height", 16)
.attr("fill", colorMap[prof]);
row.append("text")
.attr("x", 22)
.attr("y", 12)
.text(`${prof} (${professionCounts[prof] || 0})`);
});
});
document.getElementById("downloadBtn").addEventListener("click", () => {
const svgNode = document.querySelector("svg");
// Clone the SVG node to preserve original
const clonedSvg = svgNode.cloneNode(true);
const outer = document.createElement("div");
outer.appendChild(clonedSvg);
// Add xmlns so it can be saved properly
clonedSvg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
// Serialize the SVG
const svgData = new XMLSerializer().serializeToString(clonedSvg);
const svgBlob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" });
// Create download link
const url = URL.createObjectURL(svgBlob);
const a = document.createElement("a");
a.href = url;
a.download = "sunburst_chart.svg";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
</script>
</body>
|