|
| 1 | +/** |
| 2 | + * Simple Pie Chart for displaying distribution data |
| 3 | + * |
| 4 | + * //{literal} |
| 5 | + * Initialize with: |
| 6 | + * initPieChart({ |
| 7 | + * anchor: "#chart", |
| 8 | + * data: [{label: "Drop", count: 100, percentage: 25}, ...], |
| 9 | + * colorScheme: d3.schemeCategory10 |
| 10 | + * }); |
| 11 | + * //{/literal} |
| 12 | + */ |
| 13 | + |
| 14 | +// Translations |
| 15 | +const i18nPieChart = { |
| 16 | + missingConfig: "{t}Pie chart: missing required configuration{/t}", |
| 17 | + missingD3: "{t}Pie chart: d3 library not loaded{/t}", |
| 18 | + missingContainer: "{t}Pie chart: container not found{/t}", |
| 19 | + placeholderNoData: "{t}No data available{/t}", |
| 20 | +}; |
| 21 | + |
| 22 | +//{literal} |
| 23 | +window.initPieChart = function(config) { |
| 24 | + const { |
| 25 | + anchor, |
| 26 | + data = [], |
| 27 | + colorScheme = d3.schemeCategory10 |
| 28 | + } = config; |
| 29 | + |
| 30 | + if (!anchor) { |
| 31 | + console.error(i18nPieChart.missingConfig, config); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + function init() { |
| 36 | + if (typeof d3 === "undefined" || !d3) { |
| 37 | + console.error(i18nPieChart.missingD3); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const container = d3.select(anchor); |
| 42 | + if (container.empty()) { |
| 43 | + console.warn(i18nPieChart.missingContainer, anchor); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const margin = { top: 20, right: 120, bottom: 20, left: 20 }; |
| 48 | + |
| 49 | + container |
| 50 | + .attr("preserveAspectRatio", "xMidYMid meet") |
| 51 | + .style("overflow", "visible") |
| 52 | + .style("width", "100%") |
| 53 | + .style("height", "100%") |
| 54 | + .style("display", "block"); |
| 55 | + |
| 56 | + function computeSize() { |
| 57 | + const node = container.node(); |
| 58 | + const parent = node ? (node.parentElement || node) : null; |
| 59 | + const rect = parent ? parent.getBoundingClientRect() : { width: 400, height: 300 }; |
| 60 | + return { |
| 61 | + fullW: Math.max(300, Math.floor(rect.width || 400)), |
| 62 | + fullH: Math.max(200, Math.floor(rect.height || 300)), |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + function render() { |
| 67 | + if (!data || data.length === 0) { |
| 68 | + container.selectAll("*").remove(); |
| 69 | + container.append("text") |
| 70 | + .attr("x", "50%") |
| 71 | + .attr("y", "50%") |
| 72 | + .attr("text-anchor", "middle") |
| 73 | + .attr("fill", "#7a869a") |
| 74 | + .attr("font-size", 14) |
| 75 | + .text(i18nPieChart.placeholderNoData); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const { fullW, fullH } = computeSize(); |
| 80 | + const width = fullW - margin.left - margin.right; |
| 81 | + const height = fullH - margin.top - margin.bottom; |
| 82 | + const radius = Math.min(width - 120, height) / 2; |
| 83 | + |
| 84 | + container.attr("viewBox", `0 0 ${fullW} ${fullH}`); |
| 85 | + container.selectAll("*").remove(); |
| 86 | + |
| 87 | + const g = container.append("g") |
| 88 | + .attr("transform", `translate(${margin.left + radius},${margin.top + height / 2})`); |
| 89 | + |
| 90 | + const colorScale = d3.scaleOrdinal(colorScheme); |
| 91 | + |
| 92 | + // Sort data by count descending for consistent legend ordering |
| 93 | + const sortedData = [...data].sort((a, b) => b.count - a.count); |
| 94 | + |
| 95 | + const pie = d3.pie() |
| 96 | + .value(d => d.count) |
| 97 | + .sort(null); |
| 98 | + |
| 99 | + const arc = d3.arc() |
| 100 | + .innerRadius(0) |
| 101 | + .outerRadius(radius); |
| 102 | + |
| 103 | + const arcs = g.selectAll(".arc") |
| 104 | + .data(pie(sortedData)) |
| 105 | + .enter() |
| 106 | + .append("g") |
| 107 | + .attr("class", "arc"); |
| 108 | + |
| 109 | + arcs.append("path") |
| 110 | + .attr("d", arc) |
| 111 | + .attr("fill", (d, i) => colorScale(i)) |
| 112 | + .attr("stroke", "white") |
| 113 | + .attr("stroke-width", 2) |
| 114 | + .style("opacity", 0.8) |
| 115 | + .on("mouseover", function() { |
| 116 | + d3.select(this).style("opacity", 1); |
| 117 | + }) |
| 118 | + .on("mouseout", function() { |
| 119 | + d3.select(this).style("opacity", 0.8); |
| 120 | + }); |
| 121 | + |
| 122 | + // Add legend |
| 123 | + const legend = container.append("g") |
| 124 | + .attr("transform", `translate(${margin.left + radius * 2 + 30},${margin.top + 20})`); |
| 125 | + |
| 126 | + const legendItems = legend.selectAll(".legend-item") |
| 127 | + .data(sortedData) |
| 128 | + .enter() |
| 129 | + .append("g") |
| 130 | + .attr("class", "legend-item") |
| 131 | + .attr("transform", (d, i) => `translate(0,${i * 25})`); |
| 132 | + |
| 133 | + legendItems.append("rect") |
| 134 | + .attr("width", 18) |
| 135 | + .attr("height", 18) |
| 136 | + .attr("fill", (d, i) => colorScale(i)) |
| 137 | + .style("opacity", 0.8); |
| 138 | + |
| 139 | + legendItems.append("text") |
| 140 | + .attr("x", 24) |
| 141 | + .attr("y", 9) |
| 142 | + .attr("dy", "0.35em") |
| 143 | + .style("font-size", "12px") |
| 144 | + .text(d => `${d.label}: ${d.count.toLocaleString()} (${d.percentage.toFixed(1)}%)`); |
| 145 | + } |
| 146 | + |
| 147 | + render(); |
| 148 | + |
| 149 | + // Handle resize |
| 150 | + let resizeTimeout; |
| 151 | + window.addEventListener("resize", function() { |
| 152 | + clearTimeout(resizeTimeout); |
| 153 | + resizeTimeout = setTimeout(render, 250); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + if (document.readyState === "loading") { |
| 158 | + document.addEventListener("DOMContentLoaded", init); |
| 159 | + } else { |
| 160 | + init(); |
| 161 | + } |
| 162 | +}; |
| 163 | +//{/literal} |
0 commit comments