最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ChartJS place y-axis labels between ticks - Stack Overflow

programmeradmin3浏览0评论

I have the following graph:

However, i'd like to shift the y axis labels to be the following:

Is it possible to move the y axis labels in between the tick marks with ChartJS, like shown in the example image above?

Functionality is similar to the options.scales.yAxes[].ticks.position option, except currently it is defined as in the x direction for the y axis, where as I need it in the y direction for the y axis. And better yet, automatically centered.

I have the following graph:

However, i'd like to shift the y axis labels to be the following:

Is it possible to move the y axis labels in between the tick marks with ChartJS, like shown in the example image above?

Functionality is similar to the options.scales.yAxes[].ticks.position option, except currently it is defined as in the x direction for the y axis, where as I need it in the y direction for the y axis. And better yet, automatically centered.

Share Improve this question edited Jul 21, 2017 at 15:15 Ulad Kasach asked Jul 21, 2017 at 15:00 Ulad KasachUlad Kasach 12.9k13 gold badges69 silver badges90 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

Unfortunately, there is no built-in method in ChartJS for this, at the moment.

You would rather need to create a chart plugin to achieve that.

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();
      });
   }
}]

note: this will hide the first tick (which begins at 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./ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>

With current version of Chart.js you can do :

y : {
  type: "category",
  labels,
  offset: true,
  grid: { offset: true }
}

I think you can use: yAxes: [{gridLines: { offsetGridLines: true }}] to do this. The label value will be the avg between the thicks.

Not sure if this is what you want (as I'm not sure if your Y axis has numbered values), but if your y axis is using numbered values, you can use the ticks callback and modify the values array and add your ticks there, with value being the position on the y axis based on the value and label as what you want displayed.

options: {
  scales: {
    yAxis: {
      ...
      ticks: {
        ...
        callback(value, index, values) {
          values.push({ value: 12.5, label: 'Critical' })
          values.push({ value: 18.5, label: 'Needs Work' })
          values.push({ value: 22.5, label: 'Neutral' })
          ...
        },
      },
发布评论

评论列表(0)

  1. 暂无评论