質問編集履歴

1

2017/04/10追記以降に現状とスクリプトを追記

2017/04/10 05:28

投稿

tera_ina
tera_ina

スコア13

test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,243 @@
29
29
  グラフサイズ変更時の座標がずれてしまい
30
30
 
31
31
  Chart.js自体(Tooltip等)で処理しようと考えました。
32
+
33
+
34
+
35
+ ###2017/04/10追記
36
+
37
+ <以下記載のScript表示結果>
38
+
39
+ ※Januaryにマウスオーバーしたとき
40
+
41
+ ![イメージ説明](7793ec1adf8abd89d329e873916bb030.png)
42
+
43
+ <解決できた点>
44
+
45
+ 特定のBarのTooltipsを常時表示する
46
+
47
+ <問題点>
48
+
49
+ 1.他のBarもマウスオーバーすると結局Tooltipsが表示されてしまう。
50
+
51
+ 2.Tooltipsに任意の文字列を表示することができない。
52
+
53
+ (不要なMarchや判例の■マークが残ってしまう)
54
+
55
+ 3.Barのセンター上部に表示したいが右斜め上に表示されてしまう。
56
+
57
+ (複数表示の際にTooltipsが重ならないようにするため)
58
+
59
+
60
+
61
+ ```JavaScript
62
+
63
+ <div class="chart_container"><canvas id="myChart" width="320" height="180"></canvas></div>
64
+
65
+ <script>
66
+
67
+ var ctx = document.getElementById('myChart').getContext('2d');
68
+
69
+ var data = {
70
+
71
+ labels: ["January", "February", "March", "April", "May", "June", "July"],
72
+
73
+ datasets: [
74
+
75
+ {
76
+
77
+ label: "My First dataset",
78
+
79
+ backgroundColor: [
80
+
81
+ 'rgba(255, 99, 132, 0.2)',
82
+
83
+ 'rgba(54, 162, 235, 0.2)',
84
+
85
+ 'rgba(255, 206, 86, 0.2)',
86
+
87
+ 'rgba(75, 192, 192, 0.2)',
88
+
89
+ 'rgba(153, 102, 255, 0.2)',
90
+
91
+ 'rgba(255, 159, 64, 0.2)'
92
+
93
+ ],
94
+
95
+ borderColor: [
96
+
97
+ 'rgba(255,99,132,1)',
98
+
99
+ 'rgba(54, 162, 235, 1)',
100
+
101
+ 'rgba(255, 206, 86, 1)',
102
+
103
+ 'rgba(75, 192, 192, 1)',
104
+
105
+ 'rgba(153, 102, 255, 1)',
106
+
107
+ 'rgba(255, 159, 64, 1)'
108
+
109
+ ],
110
+
111
+ borderWidth: 1,
112
+
113
+ data: [65, 59, 80, 81, 56, 55, 40],
114
+
115
+ }
116
+
117
+ ]
118
+
119
+ };
120
+
121
+
122
+
123
+ var cnt;
124
+
125
+ Chart.pluginService.register({
126
+
127
+ beforeRender: function (chart) {
128
+
129
+ if (chart.config.options.showAllTooltips) {
130
+
131
+ // create an array of tooltips
132
+
133
+ // we can't use the chart tooltip because there is only one tooltip per chart
134
+
135
+ chart.pluginTooltips = [];
136
+
137
+ chart.config.data.datasets.forEach(function (dataset, i) {
138
+
139
+ chart.getDatasetMeta(i).data.forEach(function (sector, j) {
140
+
141
+ chart.pluginTooltips.push(new Chart.Tooltip({
142
+
143
+ _chart: chart.chart,
144
+
145
+ _chartInstance: chart,
146
+
147
+ _data: chart.data,
148
+
149
+ _options: chart.options.tooltips,
150
+
151
+ _active: [sector]
152
+
153
+ }, chart));
154
+
155
+ });
156
+
157
+ });
158
+
159
+
160
+
161
+ // turn off normal tooltips
162
+
163
+ chart.options.tooltips.enabled = false;
164
+
165
+ }
166
+
167
+ },
168
+
169
+ afterDraw: function (chart, easing) {
170
+
171
+ if (chart.config.options.showAllTooltips) {
172
+
173
+ // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
174
+
175
+ if (!chart.allTooltipsOnce) {
176
+
177
+ if (easing !== 1)
178
+
179
+ return;
180
+
181
+ chart.allTooltipsOnce = true;
182
+
183
+ }
184
+
185
+
186
+
187
+ cnt = 1;
188
+
189
+ // turn on tooltips
190
+
191
+ chart.options.tooltips.enabled = true;
192
+
193
+ Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
194
+
195
+ if (cnt == 3){ //例として3つ目のBarのみ表示するように設定
196
+
197
+ tooltip.initialize();
198
+
199
+ tooltip.update();
200
+
201
+ // we don't actually need this since we are not animating tooltips
202
+
203
+ tooltip.pivot();
204
+
205
+ tooltip.transition(easing).draw();
206
+
207
+ chart.options.tooltips.enabled = true;
208
+
209
+ }
210
+
211
+ cnt = cnt +1;
212
+
213
+ });
214
+
215
+ }
216
+
217
+ }
218
+
219
+ })
220
+
221
+
222
+
223
+ var myChart = new Chart(ctx, {
224
+
225
+ type: 'bar',
226
+
227
+ data: data,
228
+
229
+ options: {
230
+
231
+ tooltips: {
232
+
233
+ callbacks: {
234
+
235
+ label: function(tooltipItems, data) {
236
+
237
+ return 'AAA';
238
+
239
+ }
240
+
241
+ }
242
+
243
+ },
244
+
245
+ showAllTooltips: true,
246
+
247
+ barPercentage:1, responsive: true,
248
+
249
+ legend: { display: false },
250
+
251
+ scales: {
252
+
253
+ xAxes: [{ display: true, stacked: false, gridLines: { display: false } }],
254
+
255
+ yAxes: [{ display: true,
256
+
257
+ scaleLabel: { display: true, labelString: 'Y LABEL', fontFamily: 'monospace', fontSize: 14 },
258
+
259
+ ticks: { max: 100, min: 0, stepSize: 20}
260
+
261
+ }]
262
+
263
+ }
264
+
265
+ }
266
+
267
+ });
268
+
269
+ </script>
270
+
271
+ ```