ChartJS在刻度线之间放置y轴标签

时间:2017-07-21 15:00:29

标签: javascript canvas graph charts chart.js

我有以下图表: enter image description here

但是,我想将y轴标签移动到以下位置:enter image description here

是否可以使用ChartJS在刻度线之间移动y轴标签,如上图所示?

功能类似于{ "first_name" = OOOOOOOOOOOOOOOOOO; id = 1OOOOOOOOOOOOOOOOOO; "last_name" = OOOOOOOOOOOOOOOOOO; name = "OOOOOOOOOOOOOOOOOO"; picture = { data = { "is_silhouette" = 0; url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/15871714_xxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxx_n.jpg?oh=xxxxxxxxxxxxxxxxxxxxxxxxxxx"; }; }; } 选项,但目前它被定义为options.scales.yAxes[].ticks.position,其中我需要y轴方向的y轴。更好的是,自动居中。

2 个答案:

答案 0 :(得分:3)

不幸的是,目前ChartJS中没有内置方法。

您宁愿创建图表插件来实现这一目标。

plugins: [{
   beforeDraw: function(chart) {
      var ctx = chart.ctx;
      var yAxis = chart.scales['y-axis-0'];
      var tickGap = yAxis.getPixelForTick(1) - yAxis.getPixelForTick(0);
      yAxis.options.ticks.fontColor = 'transparent'; // hide original tick
      // loop through ticks array
      Chart.helpers.each(yAxis.ticks, function(tick, index) {
         if (index === yAxis.ticks.length - 1) return;
         var xPos = yAxis.right;
         var yPos = yAxis.getPixelForTick(index);
         var xPadding = 10;
         // draw tick
         ctx.save();
         ctx.textBaseline = 'middle';
         ctx.textAlign = 'right';
         ctx.fillStyle = 'rgba(0, 0, 0, 0.8)';
         ctx.fillText(tick, xPos - xPadding, yPos + tickGap / 2);
         ctx.restore();
      });
   }
}]

注意:这会隐藏第一个标记(从0开始)

ᴡᴏʀᴋɪɴɢᴡᴏʀᴋɪɴɢxᴀᴍᴘʟᴇᴀᴍᴘʟᴇ

var barChart = new Chart(ctx, {
   type: 'bar',
   data: {
      labels: ['Jan', 'Feb', 'Mar'],
      datasets: [{
         label: 'BAR',
         data: [10, 20, 30],
         backgroundColor: 'rgba(0, 119, 204, 0.5)'
      }]
   },
   options: {
      responsive: false,
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      }
   },
   plugins: [{
      beforeDraw: function(chart) {
         var ctx = chart.ctx;
         var yAxis = chart.scales['y-axis-0'];
         var tickGap = yAxis.getPixelForTick(1) - yAxis.getPixelForTick(0);
         yAxis.options.ticks.fontColor = 'transparent'; // hide original tick
         // loop through ticks array
         Chart.helpers.each(yAxis.ticks, function(tick, index) {
            if (index === yAxis.ticks.length - 1) return;
            var xPos = yAxis.right;
            var yPos = yAxis.getPixelForTick(index);
            var xPadding = 10;
            // draw tick
            ctx.save();
            ctx.textBaseline = 'middle';
            ctx.textAlign = 'right';
            ctx.fillStyle = 'rgba(0, 0, 0, 0.8)';
            ctx.fillText(tick, xPos - xPadding, yPos + tickGap / 2);
            ctx.restore();
         });
      }
   }]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>

答案 1 :(得分:0)

我认为您可以使用:yAxes: [{gridLines: { offsetGridLines: true }}] 来执行此操作。标签值将是厚度之间的平均值。