JQuery未習熟の者です。
Chart.js 2.3.0を利用して棒グラフと折れ線グラフを作成したのですが、何点か思い通りいかない点があって悩んでいます。
【やりたい事】
棒・折れ線グラフ共に、グラフ上部中央のラベルを非表示若しくは削除したい。(”年間売上比率”)(転換率)
単純にラベル部分を消しても四角と『undefined』が表示されてしまう。
棒グラフの『start』以前と以後の色分けをしているのですが、折れ線グラフも同様に『start』以前と以後の色分けをしたい。
backgroundColorを棒グラフと同様に指定しても最初のカラーが全てに適用されてしまう。
両グラフともツールチップを常時表示したままにしたい。
(可能であればアニメーション終了時に表示開始したい)
解決策をご存知の方がいらっしゃいましたら何卒ご教授お願い致します。
2017/11/08
・進捗状況
ツールチップ常時表示、線グラフの色分け、ラベルの非表示が解決いたしました。
ただ、線グラフ"After 1 year"以降のツールチップ内のデータが"undefined%"と表示されてしまいます。
あと、ツールチップ内のラベル名が重複してしまっているので1つに絞りたいと考えています。
重ねてのお願いで恐縮ですが、ご教示頂けますと幸いです。
<script> //ツールチップ常時表示 Chart.pluginService.register({ beforeRender: function (chart) { if (chart.config.options.showAllTooltips) { // create an array of tooltips // we can't use the chart tooltip because there is only one tooltip per chart chart.pluginTooltips = []; chart.config.data.datasets.forEach(function (dataset, i) { chart.getDatasetMeta(i).data.forEach(function (sector, j) { chart.pluginTooltips.push(new Chart.Tooltip({ _chart: chart.chart, _chartInstance: chart, _data: chart.data, _options: chart.options.tooltips, _active: [sector] }, chart)); }); }); // turn off normal tooltips chart.options.tooltips.enabled = false; } }, afterDraw: function (chart, easing) { if (chart.config.options.showAllTooltips) { // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once if (!chart.allTooltipsOnce) { if (easing !== 1) return; chart.allTooltipsOnce = true; } // turn on tooltips chart.options.tooltips.enabled = true; Chart.helpers.each(chart.pluginTooltips, function (tooltip) { tooltip.initialize(); tooltip.update(); // we don't actually need this since we are not animating tooltips tooltip.pivot(); tooltip.transition(easing).draw(); }); chart.options.tooltips.enabled = false; } } }); //棒グラフ var ctx = document.getElementById("myBarChart1"); var myBarChart = new Chart(ctx, { //グラフの種類 type: 'bar', //データの設定 data: { //データ項目のラベル labels: ["Before 3 years","Before 2 years","Before 1 year","Start", "After 1 year", "After 2 years", "After 3 years"], //データセット datasets: [{ label: "年間売上比率", backgroundColor: ["rgba(75,192,192,0.6)","rgba(75,192,192,0.6)","rgba(75,192,192,0.6)","rgba(75,192,192,0.6)", "rgba(75,192,192,1)", "rgba(75,192,192,1)", "rgba(75,192,192,1)"], //グラフのデータ data: [0.7,0.8,0.7,1, 2.1, 8.1, 10.1] } ] }, //オプションの設定 options: { showAllTooltips: true, animation: { duration: 8000, }, legend: { display: false, }, //軸の設定 scales: { //縦軸の設定 yAxes: [{ display: true, stacked: false, gridLines: { display: false } }], xAxes: [{ display: true, stacked: false, gridLines: { display: false } }] }, tooltips: { callbacks: { label: function (tooltipItem, data) { return data.labels[tooltipItem.index] + ": " + data.datasets[0].data[tooltipItem.index] + " 倍"; } } } } }); </script> </div> <canvas id="myLineChart" width="800" height="400"></canvas> <script> //折れ線グラフ var ctx = document.getElementById("myLineChart"); var myLineChart = new Chart(ctx, { //グラフの種類 type: 'line', //データの設定 data: { //データ項目のラベル labels: ["Before 3 years","Before 2 years","Before 1 year","Start", "After 1 year", "After 2 years", "After 3 years"], //データセット datasets: [{ // Start以前 label: "転換率", borderWidth: 3, backgroundColor: "rgba(75,192,192,0.4)", borderColor: "rgba(75,192,192,0.6)", pointBorderColor: "#fff", pointBackgroundColor: "rgba(75,192,192,0.6)", pointRadius: 8, pointBorderWidth: 5, pointHoverRadius: 10, pointHitRadius: 5, pointStyle: "circle", pointHoverBackgroundColor: "rgba(75,192,192,0.6)", pointHoverBorderColor: "#fff", pointHitRadius: 5, //グラフのデータ data: [1.06,1.37,1.23,1.49] }, { // Start以前 label: "転換率", borderWidth: 3, backgroundColor: "rgba(75,192,192,0.8)", borderColor: "rgba(75,192,192,1)", pointBorderColor: "#fff", pointBackgroundColor: "rgba(75,192,192,0.8)", pointRadius: 8, pointBorderWidth: 5, pointHoverRadius: 10, pointHitRadius: 5, pointStyle: "circle", pointHoverBackgroundColor: "rgba(75,192,192,1)", pointHoverBorderColor: "#fff", pointHitRadius: 5, //グラフのデータ data: [,,, 1.49, 2.09, 3.27, 3.31] } ] }, //オプションの設定 options: { showAllTooltips: true, animation: { duration: 8000 }, legend: { display: false }, tooltips: { callbacks: { label: function (tooltipItem, data) { return data.labels[tooltipItem.index] + ": " + data.datasets[0].data[tooltipItem.index] + " %"; //ここで単位を付けます } } }, scales: { //縦軸の設定 yAxes: [{ display: true, stacked: false, gridLines: { display: false } }], xAxes: [{ display: true, stacked: false, gridLines: { display: false } }], ticks: { //最小値を0にする beginAtZero: true } } } }); </script>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/08 01:45
2017/11/08 03:02
2017/11/08 03:35
2017/11/08 03:54
2017/11/08 04:00