回答編集履歴

5

ツールチップのundefined表示について

2017/11/08 03:53

投稿

shimitei
shimitei

スコア799

test CHANGED
@@ -216,7 +216,7 @@
216
216
 
217
217
 
218
218
 
219
- ```
219
+ ```js
220
220
 
221
221
  tooltips: {
222
222
 
@@ -233,3 +233,35 @@
233
233
  },
234
234
 
235
235
  ```
236
+
237
+
238
+
239
+ # ツールチップのundefined表示について
240
+
241
+
242
+
243
+ これは色分けを実現するためにデータセットを分割したので、ツールチップのラベル表示のコードも変更しないといけません。
244
+
245
+ Start以前と以降で取得するデータを変える必要があります。
246
+
247
+ とりあえずundefinedだったら2番目のデータセットの値を表示に使うコードにしてみました。
248
+
249
+
250
+
251
+ ```js
252
+
253
+ tooltips: {
254
+
255
+ callbacks: {
256
+
257
+ label: function (tooltipItem, data) {
258
+
259
+ return (data.datasets[0].data[tooltipItem.index] || data.datasets[1].data[tooltipItem.index]) + " %";
260
+
261
+ }
262
+
263
+ }
264
+
265
+ },
266
+
267
+ ```

4

ツールチップ内容のカスタマイズ

2017/11/08 03:53

投稿

shimitei
shimitei

スコア799

test CHANGED
@@ -165,3 +165,71 @@
165
165
  ```
166
166
 
167
167
  ![イメージ説明](db0286a8304021e8c8ce93724be70e58.png)
168
+
169
+
170
+
171
+ # ツールチップ内容のカスタマイズ
172
+
173
+
174
+
175
+ ## タイトルなし、ラベルのみ
176
+
177
+
178
+
179
+ ![イメージ説明](c6d2529e210ae7be9e10a111d48539f6.png)
180
+
181
+
182
+
183
+ ```js
184
+
185
+ tooltips: {
186
+
187
+ callbacks: {
188
+
189
+ title: function(tooltipItems, data) {return ""},
190
+
191
+ label: function (tooltipItem, data) {
192
+
193
+ return data.labels[tooltipItem.index]
194
+
195
+ + ": "
196
+
197
+ + data.datasets[0].data[tooltipItem.index]
198
+
199
+ + " %";
200
+
201
+ }
202
+
203
+ }
204
+
205
+ },
206
+
207
+ ```
208
+
209
+
210
+
211
+ ## タイトルとラベルの2行
212
+
213
+
214
+
215
+ ![イメージ説明](648aac3e0a662fb9eb1679291e4aa0b3.png)
216
+
217
+
218
+
219
+ ```
220
+
221
+ tooltips: {
222
+
223
+ callbacks: {
224
+
225
+ label: function (tooltipItem, data) {
226
+
227
+ return data.datasets[0].data[tooltipItem.index] + " %";
228
+
229
+ }
230
+
231
+ }
232
+
233
+ },
234
+
235
+ ```

3

MarkdownのコードブロックにJavaScript言語指定

2017/11/08 02:59

投稿

shimitei
shimitei

スコア799

test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
 
56
56
 
57
- ```
57
+ ```js
58
58
 
59
59
  Chart.pluginService.register({
60
60
 

2

MarkdownのコードブロックにJavaScript言語指定

2017/11/07 06:58

投稿

shimitei
shimitei

スコア799

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- ```
9
+ ```js
10
10
 
11
11
  datasets: [
12
12
 
@@ -154,7 +154,7 @@
154
154
 
155
155
 
156
156
 
157
- ```
157
+ ```js
158
158
 
159
159
  options: {
160
160
 

1

ツールチップの常時表示追加

2017/11/07 06:57

投稿

shimitei
shimitei

スコア799

test CHANGED
@@ -37,3 +37,131 @@
37
37
 
38
38
 
39
39
  ![イメージ説明](6501502e1ff97c01508b72a9927afe7d.png)
40
+
41
+
42
+
43
+ # ツールチップの常時表示
44
+
45
+
46
+
47
+ Chart.js 2.3.0あたりのバージョンならば[Chart.js v2: How to make tooltips always appear on pie chart?](https://stackoverflow.com/a/37989832)のコードで実現できます。
48
+
49
+
50
+
51
+ stackoverflowのコードを引用します。
52
+
53
+ これはnew Chart(...)よりも前に記述しておかないと効果ありません。
54
+
55
+
56
+
57
+ ```
58
+
59
+ Chart.pluginService.register({
60
+
61
+ beforeRender: function (chart) {
62
+
63
+ if (chart.config.options.showAllTooltips) {
64
+
65
+ // create an array of tooltips
66
+
67
+ // we can't use the chart tooltip because there is only one tooltip per chart
68
+
69
+ chart.pluginTooltips = [];
70
+
71
+ chart.config.data.datasets.forEach(function (dataset, i) {
72
+
73
+ chart.getDatasetMeta(i).data.forEach(function (sector, j) {
74
+
75
+ chart.pluginTooltips.push(new Chart.Tooltip({
76
+
77
+ _chart: chart.chart,
78
+
79
+ _chartInstance: chart,
80
+
81
+ _data: chart.data,
82
+
83
+ _options: chart.options.tooltips,
84
+
85
+ _active: [sector]
86
+
87
+ }, chart));
88
+
89
+ });
90
+
91
+ });
92
+
93
+
94
+
95
+ // turn off normal tooltips
96
+
97
+ chart.options.tooltips.enabled = false;
98
+
99
+ }
100
+
101
+ },
102
+
103
+ afterDraw: function (chart, easing) {
104
+
105
+ if (chart.config.options.showAllTooltips) {
106
+
107
+ // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
108
+
109
+ if (!chart.allTooltipsOnce) {
110
+
111
+ if (easing !== 1)
112
+
113
+ return;
114
+
115
+ chart.allTooltipsOnce = true;
116
+
117
+ }
118
+
119
+
120
+
121
+ // turn on tooltips
122
+
123
+ chart.options.tooltips.enabled = true;
124
+
125
+ Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
126
+
127
+ tooltip.initialize();
128
+
129
+ tooltip.update();
130
+
131
+ // we don't actually need this since we are not animating tooltips
132
+
133
+ tooltip.pivot();
134
+
135
+ tooltip.transition(easing).draw();
136
+
137
+ });
138
+
139
+ chart.options.tooltips.enabled = false;
140
+
141
+ }
142
+
143
+ }
144
+
145
+ });
146
+
147
+ ```
148
+
149
+
150
+
151
+ ツールチップを常に出しておきたいチャートでは、optionsにshowAllTooltips: trueを設定します。
152
+
153
+
154
+
155
+
156
+
157
+ ```
158
+
159
+ options: {
160
+
161
+ showAllTooltips: true,
162
+
163
+ },
164
+
165
+ ```
166
+
167
+ ![イメージ説明](db0286a8304021e8c8ce93724be70e58.png)