This repository was archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-chart.html
207 lines (162 loc) · 4.42 KB
/
multi-chart.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
width: 960px;
position: relative;
}
svg {
width: 100%;
height: 100%;
}
.arc text {
font: 14px sans-serif;
text-anchor: middle;
}
.arc path {
stroke: #fff;
}
#tooltip {
position: absolute;
width: 200px;
height: auto;
padding: 10px;
background-color: white;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: Futura;
font-size: 16px;
line-height: 20px;
}
</style>
<body>
<div id='wrapper'>
<div id="tooltip" class="hidden">
<p><strong id="charge">Charge</strong></p>
</div>
<div id='svg'></div>
<div id='svgindex'></div>
<!--table>
<tr>
<td>
<div id='svgindex'></div>
</td>
<td>
<div id='svg'></div>
</td>
</tr>
</table-->
<script src="http://d3js.org/d3.v4.min.js"></script>
<script>
var width = 960,
height = 450;
var columns = 6;
var radius = Math.floor(width / columns / 2 * 0.95);
var buffer = 2 * radius
var xScale = d3.scaleBand()
.domain(d3.range(0, columns))
.range([0, width], 0.05);
var color = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select("div#svg")
.append("svg")
.attr("width", width)
.attr("height", height)
var pie = d3.pie()
.sort(null)
.value(function(d) { return d.value; });
function addArcs(data, node, rad) {
var path = d3.arc()
.outerRadius(rad - 10)
.innerRadius(0);
var label = d3.arc()
.outerRadius(rad * 0.6)
.innerRadius(rad * 0.6);
var arc = node.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
arc.append("path")
.attr("d", path)
.attr("fill", function(d) { return color(d.data.key); });
// arc.append("text")
// .attr("transform", function(d) { return "translate(" + label.centroid(d) + ")"; })
// .attr("dy", "0.35em")
// .text(function(d) { return d.data.key; });
}
// Here jsonRecord is a JSON record
function addChart(jsonRecord, xIndex, yIndex) {
// Sum the counts over each gender
var acc = {};
["Male", "Female"].forEach(function(gender) {
var ethnicity = jsonRecord.dem[gender];
for (var key in ethnicity) acc[key] = ethnicity[key] + (typeof acc[key] !== 'undefined' ? acc[key] : 0)
});
var x = (width - columns * buffer) / 2 + radius + xIndex * buffer;
var y = radius + yIndex * buffer;
g = svg.append("g")
.attr("transform", "translate(" + x + "," + y + ")")
.on("mouseover", function(d1) {
var xPosition = x;
var yPosition = y;
var toolTip = d3.select("#tooltip");
toolTip
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#charge")
.text(jsonRecord.charge);
// Updates existing values
var payload = toolTip.selectAll("#payload").data(d3.entries(acc));
// Create elements as needed
payload.enter().append("p")
.attr("id", "payload")
.merge(payload)
.text(function (d) { return d.key + ": " + d.value; });
// remove old elements as needed
payload.exit().remove();
d3.select("#tooltip").classed("hidden", false);
})
.on("mouseout", function() {
d3.select("#tooltip").classed("hidden", true);
});
addArcs(d3.entries(acc), g, radius);
}
function parseData(jsonData) {
jsonData.forEach(function(d, i) {
var stride = columns;
var x = i % stride;
var y = Math.floor(i / stride);
addChart(d, x, y);
});
}
d3.json("lcs.json", parseData);
// Add a total demographics plot
var totalData = {
"White": 75.0,
"Black": 19.4,
"Native American": 0.4,
"Asian": 1.4,
"Native Hawaiian or Pacific Islander": 0.1,
"Two or more races": 1.9,
"Hispanic or Latino (of any race)": 3.6
};
var svgindex = d3.select("div#svgindex").append("svg")
.attr("width", width)
.attr("height", height);
var g2 = svgindex.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
addArcs(d3.entries(totalData), g2, 220);
</script>
</body>