質問するログイン新規登録

質問編集履歴

1

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

2017/04/10 05:28

投稿

tera_ina
tera_ina

スコア13

title CHANGED
File without changes
body CHANGED
@@ -13,4 +13,124 @@
13
13
  別CANVASに吹き出しを書込み合成してみましたが
14
14
  レスポンシブにしているため画面サイズ変更に伴う
15
15
  グラフサイズ変更時の座標がずれてしまい
16
- Chart.js自体(Tooltip等)で処理しようと考えました。
16
+ Chart.js自体(Tooltip等)で処理しようと考えました。
17
+
18
+ ###2017/04/10追記
19
+ <以下記載のScript表示結果>
20
+ ※Januaryにマウスオーバーしたとき
21
+ ![イメージ説明](7793ec1adf8abd89d329e873916bb030.png)
22
+ <解決できた点>
23
+ 特定のBarのTooltipsを常時表示する
24
+ <問題点>
25
+ 1.他のBarもマウスオーバーすると結局Tooltipsが表示されてしまう。
26
+ 2.Tooltipsに任意の文字列を表示することができない。
27
+ (不要なMarchや判例の■マークが残ってしまう)
28
+ 3.Barのセンター上部に表示したいが右斜め上に表示されてしまう。
29
+ (複数表示の際にTooltipsが重ならないようにするため)
30
+
31
+ ```JavaScript
32
+ <div class="chart_container"><canvas id="myChart" width="320" height="180"></canvas></div>
33
+ <script>
34
+ var ctx = document.getElementById('myChart').getContext('2d');
35
+ var data = {
36
+ labels: ["January", "February", "March", "April", "May", "June", "July"],
37
+ datasets: [
38
+ {
39
+ label: "My First dataset",
40
+ backgroundColor: [
41
+ 'rgba(255, 99, 132, 0.2)',
42
+ 'rgba(54, 162, 235, 0.2)',
43
+ 'rgba(255, 206, 86, 0.2)',
44
+ 'rgba(75, 192, 192, 0.2)',
45
+ 'rgba(153, 102, 255, 0.2)',
46
+ 'rgba(255, 159, 64, 0.2)'
47
+ ],
48
+ borderColor: [
49
+ 'rgba(255,99,132,1)',
50
+ 'rgba(54, 162, 235, 1)',
51
+ 'rgba(255, 206, 86, 1)',
52
+ 'rgba(75, 192, 192, 1)',
53
+ 'rgba(153, 102, 255, 1)',
54
+ 'rgba(255, 159, 64, 1)'
55
+ ],
56
+ borderWidth: 1,
57
+ data: [65, 59, 80, 81, 56, 55, 40],
58
+ }
59
+ ]
60
+ };
61
+
62
+ var cnt;
63
+ Chart.pluginService.register({
64
+ beforeRender: function (chart) {
65
+ if (chart.config.options.showAllTooltips) {
66
+ // create an array of tooltips
67
+ // we can't use the chart tooltip because there is only one tooltip per chart
68
+ chart.pluginTooltips = [];
69
+ chart.config.data.datasets.forEach(function (dataset, i) {
70
+ chart.getDatasetMeta(i).data.forEach(function (sector, j) {
71
+ chart.pluginTooltips.push(new Chart.Tooltip({
72
+ _chart: chart.chart,
73
+ _chartInstance: chart,
74
+ _data: chart.data,
75
+ _options: chart.options.tooltips,
76
+ _active: [sector]
77
+ }, chart));
78
+ });
79
+ });
80
+
81
+ // turn off normal tooltips
82
+ chart.options.tooltips.enabled = false;
83
+ }
84
+ },
85
+ afterDraw: function (chart, easing) {
86
+ if (chart.config.options.showAllTooltips) {
87
+ // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
88
+ if (!chart.allTooltipsOnce) {
89
+ if (easing !== 1)
90
+ return;
91
+ chart.allTooltipsOnce = true;
92
+ }
93
+
94
+ cnt = 1;
95
+ // turn on tooltips
96
+ chart.options.tooltips.enabled = true;
97
+ Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
98
+ if (cnt == 3){ //例として3つ目のBarのみ表示するように設定
99
+ tooltip.initialize();
100
+ tooltip.update();
101
+ // we don't actually need this since we are not animating tooltips
102
+ tooltip.pivot();
103
+ tooltip.transition(easing).draw();
104
+ chart.options.tooltips.enabled = true;
105
+ }
106
+ cnt = cnt +1;
107
+ });
108
+ }
109
+ }
110
+ })
111
+
112
+ var myChart = new Chart(ctx, {
113
+ type: 'bar',
114
+ data: data,
115
+ options: {
116
+ tooltips: {
117
+ callbacks: {
118
+ label: function(tooltipItems, data) {
119
+ return 'AAA';
120
+ }
121
+ }
122
+ },
123
+ showAllTooltips: true,
124
+ barPercentage:1, responsive: true,
125
+ legend: { display: false },
126
+ scales: {
127
+ xAxes: [{ display: true, stacked: false, gridLines: { display: false } }],
128
+ yAxes: [{ display: true,
129
+ scaleLabel: { display: true, labelString: 'Y LABEL', fontFamily: 'monospace', fontSize: 14 },
130
+ ticks: { max: 100, min: 0, stepSize: 20}
131
+ }]
132
+ }
133
+ }
134
+ });
135
+ </script>
136
+ ```