2017-12-13 13 views
0

縦軸の数値に基づいて、ChartJSの行に条件付き背景色を追加する際にちょっとしたことがあります。Chart JS - 条件付き水平行の背景色

例:

If the vertical axis is between 0 - 6, background colour for those rows is green. 
If the vertical axis is between 6 - 12 background colour for those rows is grey 
If the vertical axis is > 12 background colour for those rows is red 

誰もこれまでにこのようなことをしたことはありますか?

大まかに機能を説明した画像を添付しました。

enter image description here

乾杯!

答えて

0

chartjsでこれを行うオプションはありません。しかし、あなた自身でと書くことができ、例えばbeforeDrawフックであなた自身で背景を描くことができます。

var chart = new Chart(ctx, { 
    plugins: [{ 
     beforeDraw: function(chart) { 
      //.. 
     } 
    }] 
}); 

y軸セグメントの高さを計算するためのすべての情報をグラフパラメータから取得できます。 これを実装する方法の下にスニペットを含めました。

 var canvas = document.getElementById('myChart'); 
 
     window.chartColors = { 
 
      red: 'rgb(255, 99, 132)', 
 
      orange: 'rgb(255, 159, 64)', 
 
      yellow: 'rgb(255, 205, 86)', 
 
      green: 'rgb(51, 204, 51)', 
 
      blue: 'rgb(54, 162, 235)', 
 
      purple: 'rgb(153, 102, 255)', 
 
      grey: 'rgb(201, 203, 207)' 
 
     }; 
 

 
     var myLineChart = new Chart(canvas, 
 
      { 
 
       type: 'line', 
 
       data: { 
 
        labels: ['1', '2', '3', '4', '5'], 
 
        datasets: [ 
 
         { 
 
          label: '# of Votes', 
 
          fill: false, 
 
          backgroundColor: window.chartColors.blue, 
 
          borderColor: window.chartColors.blue, 
 
          data: [2, 5, 12.5, 9, 6.3] 
 
         } 
 
        ] 
 
       }, 
 
       options: { 
 
        responsive: true, 
 
        title: { 
 
         display: true, 
 
         text: 'Conditional Background' 
 
        }, 
 
        backgroundRules: [{ 
 
         backgroundColor: window.chartColors.green, 
 
         yAxisSegement: 6 
 
        }, { 
 
         backgroundColor: window.chartColors.grey, 
 
         yAxisSegement: 12 
 
        }, { 
 
         backgroundColor: window.chartColors.red, 
 
         yAxisSegement: Infinity 
 
        }], 
 
        scales: { 
 
         yAxes: [{ 
 
          ticks: { 
 
           beginAtZero: true, 
 
           stepSize: 1 
 
          } 
 
         }] 
 
        } 
 
       }, 
 
       plugins: [{ 
 
        beforeDraw: function (chart) { 
 
         var ctx = chart.chart.ctx; 
 
         var ruleIndex = 0; 
 
         var rules = chart.chart.options.backgroundRules; 
 
         var yaxis = chart.chart.scales["y-axis-0"]; 
 
         var xaxis = chart.chart.scales["x-axis-0"]; 
 
         var partPercentage = 1/(yaxis.ticksAsNumbers.length - 1); 
 
         for (var i = yaxis.ticksAsNumbers.length - 1; i > 0; i--) { 
 
          if (yaxis.ticksAsNumbers[i] < rules[ruleIndex].yAxisSegement) { 
 
           ctx.fillStyle = rules[ruleIndex].backgroundColor; 
 
           ctx.fillRect(xaxis.left, yaxis.top + ((i - 1) * (yaxis.height * partPercentage)), xaxis.width, yaxis.height * partPercentage); 
 
          } else { 
 
           ruleIndex++; 
 
           i++; 
 
          } 
 
         } 
 
        } 
 
       }] 
 
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script> 
 
<canvas id="myChart" width="400" height="250"></canvas>

:これは適切な実装よりも概念の多くの証拠であることに注意してください
関連する問題