質問編集履歴

2

追記

2018/10/24 05:33

投稿

MORIZOU
MORIZOU

スコア12

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,12 @@
30
30
 
31
31
  <script src="palette.js" type="text/javascript"></script>
32
32
 
33
+ <body>
34
+
35
+ <canvas id="mybarChart2" height="245" width="550"></canvas>
36
+
37
+ </body>
38
+
33
39
  <script>
34
40
 
35
41
  var myChart = "mybarChart2";

1

ソースコードの追加

2018/10/24 05:33

投稿

MORIZOU
MORIZOU

スコア12

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,321 @@
21
21
  Chart.jsは2.7.1を使用しています。
22
22
 
23
23
  ![イメージ説明](6ea43e69ee299f0e05154e2fa5b6996e.jpeg)
24
+
25
+
26
+
27
+ ```html
28
+
29
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
30
+
31
+ <script src="palette.js" type="text/javascript"></script>
32
+
33
+ <script>
34
+
35
+ var myChart = "mybarChart2";
36
+
37
+ var xlabel = ["111", "222", "333", "444", "555", "666", "777"];
38
+
39
+ var ylabel = ["あいうえお", "かきくけこ", "さしすせそ", "たちつてと", "なにぬねの", "はひふへほ", "まみむめも", "やゆよ", "らりるれろ", "わを", "ん", "ん"];
40
+
41
+ var hlxlabel = [];
42
+
43
+ for(count in xlabel){
44
+
45
+ var string ="";
46
+
47
+ if(xlabel[count].length >= 7){
48
+
49
+ string = xlabel[count].substr(0,6) + '…';
50
+
51
+ }else{
52
+
53
+ string = xlabel[count]
54
+
55
+ }
56
+
57
+ hlxlabel.push(string);
58
+
59
+ }
60
+
61
+ var hlylabel = [];
62
+
63
+ for(count in ylabel){
64
+
65
+ var string ="";
66
+
67
+ if(ylabel[count].length >= 7){
68
+
69
+ string = ylabel[count].substr(0,6) + '…';
70
+
71
+ }else{
72
+
73
+ string = ylabel[count]
74
+
75
+ }
76
+
77
+ hlylabel.push(string);
78
+
79
+ }
80
+
81
+ var colors = palette('rainbow', ylabel.length, 0, .5).map(function(hex) {return '#' + hex;})
82
+
83
+ var dataLabelPlugin = {
84
+
85
+ afterDatasetsDraw: function (chart, easing) {
86
+
87
+ var ctx = chart.ctx;
88
+
89
+ var sums = [];
90
+
91
+ chart.data.datasets.forEach(function (dataset, i) {
92
+
93
+ var meta = chart.getDatasetMeta(i);
94
+
95
+ if (!meta.hidden) {
96
+
97
+ meta.data.forEach(function (element, index) {
98
+
99
+ // 積み上げ総計の初期化
100
+
101
+ if(i === 0){
102
+
103
+ sums[index] = 0;
104
+
105
+ }
106
+
107
+ // 総計ラベルの設定
108
+
109
+ ctx.fillStyle = 'rgb(0, 0, 0)';
110
+
111
+ var fontSize = 11;
112
+
113
+ var fontStyle = 'normal';
114
+
115
+ var fontFamily = 'Helvetica Neue';
116
+
117
+ ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
118
+
119
+ ctx.textAlign = 'center';
120
+
121
+ ctx.textBaseline = 'middle';
122
+
123
+ var padding = 0;
124
+
125
+ var position = element.tooltipPosition();
126
+
127
+ // 総計出力
128
+
129
+ sums[index] = sums[index] + dataset.data[index];
130
+
131
+ if(i === (chart.data.datasets.length -1)){
132
+
133
+ ctx.fillText(sums[index].toString(), position.x, position.y - (fontSize / 2) - padding);
134
+
135
+ }
136
+
137
+ });
138
+
139
+ }
140
+
141
+ });
142
+
143
+ }
144
+
145
+ }
146
+
147
+ var chart = new Chart(myChart, {
148
+
149
+ type: 'bar',
150
+
151
+ data: {
152
+
153
+ labels: hlxlabel,
154
+
155
+ datasets: [
156
+
157
+ {label: ylabel[0],backgroundColor: colors[0],data: [1, 4, 3, 2, 5, 5, 8]},
158
+
159
+ {label: ylabel[1],backgroundColor: colors[1],data: [1, 4, 3, 2, 5, 9, 8]},
160
+
161
+ {label: ylabel[2],backgroundColor: colors[2],data: [1, 4, 3, 2, 5, 9, 8]},
162
+
163
+ {label: ylabel[3],backgroundColor: colors[3],data: [1, 4, 3, 2, 5, 9, 8]},
164
+
165
+ {label: ylabel[4],backgroundColor: colors[4],data: [1, 4, 3, 2, 5, 9, 8]},
166
+
167
+ {label: ylabel[5],backgroundColor: colors[5],data: [1, 4, 3, 2, 5, 9, 8]},
168
+
169
+ {label: ylabel[6],backgroundColor: colors[6],data: [1, 4, 3, 2, 5, 9, 8]},
170
+
171
+ {label: ylabel[7],backgroundColor: colors[7],data: [1, 4, 3, 2, 5, 9, 8]},
172
+
173
+ {label: ylabel[8],backgroundColor: colors[8],data: [1, 4, 3, 2, 5, 9, 8]},
174
+
175
+ {label: ylabel[9],backgroundColor: colors[9],data: [1, 4, 3, 2, 5, 9, 8]},
176
+
177
+ {label: ylabel[10],backgroundColor: colors[10],data: [1, 4, 3, 2, 5, 9, 8]},
178
+
179
+ {label: ylabel[11],backgroundColor: colors[11],data: [1, 4, 3, 2, 5, 9, 8]},
180
+
181
+ ]
182
+
183
+ },
184
+
185
+ options: {
186
+
187
+ title:{
188
+
189
+ text:"数字 × ひらがな",
190
+
191
+ display:true,
192
+
193
+ fontSize:17,
194
+
195
+ fontStyle:'normal',
196
+
197
+ padding:10,
198
+
199
+ },
200
+
201
+ scales : {
202
+
203
+ xAxes : [ {
204
+
205
+ stacked : true,
206
+
207
+ categoryPercentage : 0.5,
208
+
209
+ ticks : {
210
+
211
+ autoSkip : false,
212
+
213
+ maxRotation : 30,
214
+
215
+ },
216
+
217
+ } ],
218
+
219
+ yAxes : [ {
220
+
221
+ stacked : true
222
+
223
+ } ],
224
+
225
+ },
226
+
227
+ legend:{
228
+
229
+ labels:{
230
+
231
+ boxWidth:10,
232
+
233
+ fontSize:10,
234
+
235
+ padding:6,
236
+
237
+ generateLabels: function(chart){
238
+
239
+ return chart.data.datasets.map( function( dataset, i){
240
+
241
+ return {
242
+
243
+ text: hlylabel[i],
244
+
245
+ fillStyle: dataset.backgroundColor,
246
+
247
+
248
+
249
+ };
250
+
251
+ })
252
+
253
+ },
254
+
255
+ },
256
+
257
+ display: true,
258
+
259
+ position: "right",
260
+
261
+ },
262
+
263
+ maintainAspectRatio: false,
264
+
265
+ responsive: false,
266
+
267
+ tooltips:{
268
+
269
+ mode:'label',
270
+
271
+ position: 'customMode',//nearest
272
+
273
+ callbacks:{
274
+
275
+ title: function (tooltipItem, data){
276
+
277
+ return xlabel[tooltipItem[0].index];
278
+
279
+ },
280
+
281
+ },
282
+
283
+ },
284
+
285
+ },
286
+
287
+ plugins: [dataLabelPlugin]
288
+
289
+ });
290
+
291
+
292
+
293
+ Chart.Tooltip.positioners.customMode = function(elements, eventPosition){
294
+
295
+ var tooltip = this;
296
+
297
+ if(!elements.length){
298
+
299
+ return false;
300
+
301
+ }
302
+
303
+ var i, le;
304
+
305
+ var x = 0;
306
+
307
+ var y = 0;
308
+
309
+ var count = 0;
310
+
311
+ for(i = 0, len = elements.length; i < len; ++i){
312
+
313
+ var el = elements[i];
314
+
315
+ if(el && el.hasValue()){
316
+
317
+ var pos = el.tooltipPosition();
318
+
319
+ x += pos.x;
320
+
321
+ y += pos.y
322
+
323
+ ++count;
324
+
325
+ }
326
+
327
+ }
328
+
329
+ return{
330
+
331
+ x: Math.round(x / count),
332
+
333
+ y: 207//Math.round(y / count)
334
+
335
+ };
336
+
337
+ }
338
+
339
+ </script>
340
+
341
+ ```