Chart.js(https://www.chartjs.org/)というものを使ってレーダーチャートを出力していて、背景色(グラフエリア内のみ)を変えたいと思い色々調べたのですが、レーダーチャートの内部だけの色を変更する方法がわかりません。
http://oboe2uran.hatenablog.com/entry/2018/01/27/181126 こちらを参考にしてやってみたのですが、canvasタグ領域内の背景色が全て変わってしまい、思う通りにはいきませんでした。
以下コードをChart.jsに追記
Chart.pluginService.register({ beforeDraw: function(c){ if (c.config.options.chartArea && c.config.options.chartArea.backgroundColor) { var ctx = c.chart.ctx; var chartArea = c.chartArea; ctx.save(); ctx.fillStyle = c.config.options.chartArea.backgroundColor; ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top); ctx.restore(); } } });
html
1<div class="canvas_outer"> 2 <canvas id="myRadarChart1" width="" height=""></canvas> 3 </div> 4 <script> 5 Chart.pluginService.register({ 6 beforeDraw: function (chart, easing) { 7 if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) { 8 var ctx = chart.chart.ctx; 9 var chartArea = chart.chartArea; 10 11 ctx.save(); 12 ctx.fillStyle = chart.config.options.chartArea.backgroundColor; 13 ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top); 14 ctx.restore(); 15 } 16 } 17 }); 18 var ctx = document.getElementById("myRadarChart1").getContext('2d'); 19 var myRadarChart1 = new Chart(ctx, { 20 type: 'radar', 21 data: { 22 labels: [ 23 'A', 24 'B', 25 'C', 26 'D', 27 'E' 28 ], 29 datasets: [{ 30 label: '', 31 data: [0,0,0,0,0,0], 32 backgroundColor: [ 33 'rgba(255, 107, 107, 0.678)', 34 ], 35 borderColor: [ 36 'rgba(255, 107, 107, 1)', 37 ], 38 borderWidth: 1 39 }] 40 }, 41 options: { 42 chartArea: { 43 backgroundColor: 'rgba(230, 238, 255, 0.6)' 44 }, 45 legend: { 46 display: false 47 }, 48 scale: { 49 pointLabels: { 50 fontSize: 12, //フォントサイズ 51 fontFamily: "'游ゴシック',YuGothic,NotoSans,'ヒラギノ角ゴ ProN W3','Hiragino Kaku Gothic ProN','メイリオ',Meiryo,sans-serif", 52 }, 53 ticks: { 54 fontSize: 10, 55 stepSize: 1, 56 max: 5, 57 beginAtZero: true, 58 }, 59 gridLines: { 60 pointLabels: { 61 fontSize: 18 62 } 63 } 64 }, 65 scales: { 66 yAxes: [{ 67 display: false, 68 gridLines: { 69 drawBorder: false 70 } 71 }], 72 xAxes: [{ 73 display: false, 74 gridLines: { 75 drawBorder: false 76 } 77 }] 78 } 79 } 80 }); 81 </script>
回答1件
あなたの回答
tips
プレビュー