質問編集履歴

2

コード修正

2018/10/22 06:46

投稿

jam912sh
jam912sh

スコア25

test CHANGED
File without changes
test CHANGED
File without changes

1

コード修正

2018/10/22 06:46

投稿

jam912sh
jam912sh

スコア25

test CHANGED
File without changes
test CHANGED
@@ -36,242 +36,234 @@
36
36
 
37
37
 
38
38
 
39
- .bar {
39
+ #stacked{
40
-
40
+
41
- fill: steelblue;
41
+ color: #000000;
42
+
43
+ background-color: #FAFAFA;
42
44
 
43
45
  }
44
46
 
45
-
47
+ </style>
48
+
46
-
49
+ <body>
50
+
51
+
52
+
53
+ <div id="stackedbars">
54
+
55
+ <svg id="stacked" width="750" height="750"></svg></div>
56
+
57
+ </body>
58
+
59
+ <script src="//d3js.org/d3.v4.min.js"></script>
60
+
61
+ <script>
62
+
63
+
64
+
65
+ var svg = d3.select("#stacked"),
66
+
67
+ margin = {top: 40, right: 150, bottom: 30, left: 40},
68
+
69
+ width = +svg.attr("width") - margin.left - margin.right,
70
+
71
+ height = +svg.attr("height") - margin.top - margin.bottom,
72
+
73
+ g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
74
+
75
+
76
+
77
+
78
+
79
+ var x = d3.scaleBand()
80
+
81
+ .rangeRound([0, width])
82
+
83
+ .padding(0.3)
84
+
85
+ .align(0.3);
86
+
87
+
88
+
89
+ var y = d3.scaleLinear()
90
+
91
+ .rangeRound([height, 0]);
92
+
93
+ //グラフの色
94
+
95
+ var z = d3.scaleOrdinal(["#ff7f7f", "#7fbfff", "#7fffff", "#7fffbf", "#7fff7f", "#bfff7f", "#ffff7f","#ffbf7f", "#ff7fbf", "#ff7fff", "#bf7fff", "#7f7fff"]);
96
+
97
+
98
+
99
+ var stack = d3.stack();
100
+
101
+ //CSV指定
102
+
103
+ d3.csv("index3.csv", type, function(error, data) {
104
+
105
+ if (error) throw error;
106
+
107
+
108
+
109
+ data.sort(function(a, b) { return b.total - a.total; });
110
+
111
+
112
+
113
+ x.domain(data.map(function(d) { return d.ethnicity; }));
114
+
115
+ y.domain([0, d3.max(data, function(d) { return d.total; })]).nice();
116
+
117
+ z.domain(data.columns.slice(1));
118
+
119
+
120
+
121
+ g.selectAll(".serie")
122
+
123
+ .data(stack.keys(data.columns.slice(1))(data))
124
+
125
+ .enter().append("g")
126
+
127
+ .attr("class", "serie")
128
+
129
+ .attr("fill", function(d) { return z(d.key); })
130
+
131
+ .selectAll("rect")
132
+
133
+ .data(function(d) { return d; })
134
+
135
+ .enter().append("rect")
136
+
137
+ .attr("x", function(d) { return x(d.data.ethnicity); })
138
+
139
+ .attr("y", function(d) { return y(d[1]); })
140
+
141
+ .attr("height", function(d) { return y(d[0]) - y(d[1]); })
142
+
143
+ .attr("width", x.bandwidth());
144
+
145
+
146
+
147
+ //横軸の設定
148
+
149
+ g.append("g")
150
+
151
+ .attr("class", "axis axis--x")
152
+
153
+ .attr("transform", "translate(0," + height + ")")
154
+
155
+ .call(d3.axisBottom(x));
156
+
157
+ //縦軸の設定
158
+
159
+ g.append("g")
160
+
161
+ .attr("class", "axis axis--y")
162
+
163
+ .call(d3.axisLeft(y))
164
+
165
+ .append("text")
166
+
167
+ .attr("x", 2)
168
+
169
+ .attr("y", y(y.ticks(10).pop()))
170
+
171
+ .attr("dy", "0.35em")
172
+
173
+ .attr("text-anchor", "start")
174
+
175
+ .attr("fill", "#000");
176
+
177
+ //.text("Population");
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+  //凡例
186
+
187
+ var legend = g.selectAll(".legend")
188
+
189
+ .data(data.columns.slice(1).reverse())
190
+
191
+ .enter().append("g")
192
+
193
+ .attr("class", "legend")
194
+
195
+ .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
196
+
197
+ .style("font", "10px sans-serif");
198
+
199
+
200
+
201
+ legend.append("rect")
202
+
203
+ .attr("x", width + 18)
204
+
205
+ .attr("width", 18)
206
+
207
+ .attr("height", 18)
208
+
209
+ .attr("fill", z);
210
+
211
+
212
+
213
+ legend.append("text")
214
+
215
+ .attr("x", width + 44)
216
+
217
+ .attr("y", 9)
218
+
219
+ .attr("dy", ".35em")
220
+
221
+ .attr("text-anchor", "start")
222
+
223
+ .text(function(d) { return d; });
224
+
225
+
226
+
227
+
228
+
229
+ });
230
+
231
+
232
+
233
+ //タイトルの設定
234
+
235
+ svg.append("g")
236
+
237
+ //文字位置
238
+
239
+ .attr("transform", "translate(" + (width / 2 - 0) + "," + 30 + ")")
240
+
241
+ .append("text")
242
+
243
+ .attr("class", "title")
244
+
245
+ .attr("font-size", "16px")
246
+
247
+
248
+
249
+ function type(d, i, columns) {
250
+
251
+ for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
252
+
253
+ d.total = t;
254
+
47
- #stacked{
255
+ return d;
48
-
49
- color: #000000;
50
-
51
- background-color: #FAFAFA;
52
256
 
53
257
  }
54
258
 
259
+
260
+
55
- </style>
261
+ </script>
56
-
57
- <body>
262
+
58
-
59
-
60
-
61
- <div id="stackedbars">
263
+
62
-
63
- <svg id="stacked" width="750" height="750"></svg></div>
64
264
 
65
265
  </body>
66
266
 
67
- <script src="//d3js.org/d3.v4.min.js"></script>
68
-
69
- <script>
70
-
71
-
72
-
73
- var svg = d3.select("#stacked"),
74
-
75
- margin = {top: 40, right: 150, bottom: 30, left: 40},
76
-
77
- width = +svg.attr("width") - margin.left - margin.right,
78
-
79
- height = +svg.attr("height") - margin.top - margin.bottom,
80
-
81
- g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
82
-
83
-
84
-
85
-
86
-
87
- var x = d3.scaleBand()
88
-
89
- .rangeRound([0, width])
90
-
91
- .padding(0.3)
92
-
93
- .align(0.3);
94
-
95
-
96
-
97
- var y = d3.scaleLinear()
98
-
99
- .rangeRound([height, 0]);
100
-
101
- //グラフの色
102
-
103
- var z = d3.scaleOrdinal(["#ff7f7f", "#7fbfff", "#7fffff", "#7fffbf", "#7fff7f", "#bfff7f", "#ffff7f","#ffbf7f", "#ff7fbf", "#ff7fff", "#bf7fff", "#7f7fff"]);
104
-
105
-
106
-
107
- var stack = d3.stack();
108
-
109
- //CSV指定
110
-
111
- d3.csv("index3.csv", type, function(error, data) {
112
-
113
- if (error) throw error;
114
-
115
-
116
-
117
- data.sort(function(a, b) { return b.total - a.total; });
118
-
119
-
120
-
121
- x.domain(data.map(function(d) { return d.ethnicity; }));
122
-
123
- y.domain([0, d3.max(data, function(d) { return d.total; })]).nice();
124
-
125
- z.domain(data.columns.slice(1));
126
-
127
-
128
-
129
- g.selectAll(".serie")
130
-
131
- .data(stack.keys(data.columns.slice(1))(data))
132
-
133
- .enter().append("g")
134
-
135
- .attr("class", "serie")
136
-
137
- .attr("fill", function(d) { return z(d.key); })
138
-
139
- .selectAll("rect")
140
-
141
- .data(function(d) { return d; })
142
-
143
- .enter().append("rect")
144
-
145
- .attr("x", function(d) { return x(d.data.ethnicity); })
146
-
147
- .attr("y", function(d) { return y(d[1]); })
148
-
149
- .attr("height", function(d) { return y(d[0]) - y(d[1]); })
150
-
151
- .attr("width", x.bandwidth());
152
-
153
-
154
-
155
- //横軸の設定
156
-
157
- g.append("g")
158
-
159
- .attr("class", "axis axis--x")
160
-
161
- .attr("transform", "translate(0," + height + ")")
162
-
163
- .call(d3.axisBottom(x));
164
-
165
- //縦軸の設定
166
-
167
- g.append("g")
168
-
169
- .attr("class", "axis axis--y")
170
-
171
- .call(d3.axisLeft(y))
172
-
173
- .append("text")
174
-
175
- .attr("x", 2)
176
-
177
- .attr("y", y(y.ticks(10).pop()))
178
-
179
- .attr("dy", "0.35em")
180
-
181
- .attr("text-anchor", "start")
182
-
183
- .attr("fill", "#000");
184
-
185
- //.text("Population");
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-  //凡例
194
-
195
- var legend = g.selectAll(".legend")
196
-
197
- .data(data.columns.slice(1).reverse())
198
-
199
- .enter().append("g")
200
-
201
- .attr("class", "legend")
202
-
203
- .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
204
-
205
- .style("font", "10px sans-serif");
206
-
207
-
208
-
209
- legend.append("rect")
210
-
211
- .attr("x", width + 18)
212
-
213
- .attr("width", 18)
214
-
215
- .attr("height", 18)
216
-
217
- .attr("fill", z);
218
-
219
-
220
-
221
- legend.append("text")
222
-
223
- .attr("x", width + 44)
224
-
225
- .attr("y", 9)
226
-
227
- .attr("dy", ".35em")
228
-
229
- .attr("text-anchor", "start")
230
-
231
- .text(function(d) { return d; });
232
-
233
-
234
-
235
-
236
-
237
- });
238
-
239
-
240
-
241
- //タイトルの設定
242
-
243
- svg.append("g")
244
-
245
- //文字位置
246
-
247
- .attr("transform", "translate(" + (width / 2 - 0) + "," + 30 + ")")
248
-
249
- .append("text")
250
-
251
- .attr("class", "title")
252
-
253
- .attr("font-size", "16px")
254
-
255
-
256
-
257
- function type(d, i, columns) {
258
-
259
- for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
260
-
261
- d.total = t;
262
-
263
- return d;
264
-
265
- }
266
-
267
-
268
-
269
- </script>
270
-
271
-
272
-
273
- </body>
274
-
275
267
  </html>
276
268
 
277
269
  ```