使用 chartjs 删除 y 轴上的多余线条

Remove excess lines on y axis using chartjs

请问如何去除折线图上多余的线条。我试图将 drawborder 设置为 false 但当然它只是删除了轴上的所有线。我只是想摆脱指向 y 轴标签的不需要的垂直线,如下图带有红色标记。

模板:

<d-chartrecord 
     :chart-data="datacollection" 
     v-bind:options="options"
     :height="200"
></d-chartrecord>

脚本:

export default {
    data () {
        return {
            datacollection: {},
            options: {
                responsive: true,
                legend: {
                  display: false,
                },
                scales: {
                    xAxes: [{
                        gridLines: {
                            display: true,
                            color: '#D7D7D7'
                        },
                        ticks: {
                            fontSize: 8,
                            beginAtZero: true
                        },
                        gridLines: {
                            display: true,
                        }
                    }],
                    yAxes: [{
                        display: true,
                        ticks: {
                            fontSize: 8,
                            beginAtZero: true,
                            stepSize: 50,
                            maxTicksLimit: 3
                        }
                    }],
                }
            },

        }
    },
    mounted () {
        this.putData()
    },
    methods: {
        putData () {
            this.datacollection = {
                labels: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
                datasets: [{
                    lineTension: 0,
                    radius: 4,
                    borderWidth: 1,
                    borderColor: '#F2A727',
                    pointBackgroundColor:[ '#fff', '#fff', '#fff', '#fff', '#fff', '#F2A727'],
                    backgroundColor: 'transparent',
                    data: [this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt()]
                }]
            }
        },
        getRandomInt  () {
            return Math.floor(Math.random() * (95)) + 5
        }
    }
}

遗憾的是,目前 ChartJS 中没有针对此的任何本机功能。您宁愿需要创建一个图表插件来实现这一点。

ᴘʟᴜɢɪɴ (ᴅʀᴀᴡ x-ᴀxɪꜱ ɢʀɪᴅ-ʟɪɴᴇꜱ)

Chart.plugins.register({
   beforeDraw: function(chart) {
      var ctx = chart.chart.ctx,
          x_axis = chart.scales['x-axis-0'],
          topY = chart.scales['y-axis-0'].top,
          bottomY = chart.scales['y-axis-0'].bottom;
      x_axis.options.gridLines.display = false;
      x_axis.ticks.forEach(function(label, index) {
         if (index === 0) return;
         var x = x_axis.getPixelForValue(label);
         ctx.save();
         ctx.beginPath();
         ctx.strokeStyle = x_axis.options.gridLines.color;
         ctx.moveTo(x, topY);
         ctx.lineTo(x, bottomY);
         ctx.stroke();
         ctx.restore();
      });
   }
});

* 将它放在脚本的顶部

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

Chart.plugins.register({
   beforeDraw: function(chart) {
      var ctx = chart.chart.ctx,
          x_axis = chart.scales['x-axis-0'],
          topY = chart.scales['y-axis-0'].top,
          bottomY = chart.scales['y-axis-0'].bottom;
      x_axis.options.gridLines.display = false; // hide original grid-lines
      // loop through x-axis ticks
      x_axis.ticks.forEach(function(label, index) {
         if (index === 0) return;
         var x = x_axis.getPixelForValue(label);
         ctx.save();
         ctx.beginPath();
         ctx.strokeStyle = x_axis.options.gridLines.color;
         ctx.moveTo(x, topY);
         ctx.lineTo(x, bottomY);
         ctx.stroke();
         ctx.restore();
      });
   }
});

var chart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         label: 'LINE',
         data: [3, 1, 4, 2, 5],
         backgroundColor: 'rgba(0, 119, 290, 0.2)',
         borderColor: 'rgba(0, 119, 290, 0.6)',
         fill: false,
         tension: 0
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            },
            gridLines: {
               display: false
            }
         }]
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

在chart.js中,gridLines提供了一个选项tickMarkLength来禁用超出轴的长度,例如:

 yAxes: [{
          gridLines: {
            tickMarkLength: 0,
          },
        }] 
xAxes: [{
          gridLines: {
            tickMarkLength: 0,
          },
        }]

在Chart.js(我使用的是2.9版本),gridLines也提供了一个禁用刻度线的选项:drawTicks.

scales: {
  xAxes: [{
    gridLines:{
      drawTicks: false
    }
  }]
}