简体   繁体   English

如何使用Chart.js删除不必要的覆盖网格线?

[英]How to remove the unnecessary overlaying gridlines using Chart.js?

I have drawn a chart using Chartjs. 我已经使用Chartjs绘制了图表。 I can explain the pixels I want to remove, but I think an image makes it a lot clearer: 我可以解释要删除的像素,但是我认为图像使它更清晰:

在此处输入图片说明

Underneath is the code which generates this graph: 下面是生成此图的代码:

var options = {
    type: 'bar',
    data: {
        labels: ["1", "2", "3", "4", "5"],
        datasets: [
            {
                borderWidth: 2,
                borderColor: "#5d5d5d",
                pointBorderColor: "#5d5d5d",
                pointBackgroundColor: "#5d5d5d",
                pointBorderWidth: 5,
                type: 'line',
                data: [26, 26, 33, 28, 30],
                fill: false,
                lineTension: 0
            },
            {
                borderWidth: 3,
                pointBorderColor: "#b8b8b8",
                pointBackgroundColor: "#b8b8b8",
                pointBorderWidth: 10,
                type: 'line',
                data: [26, 26, 29, 28, 29],
                fill: false,
                lineTension: 0
            },
            {
                data: [0, 0, 0, 0, 0],
                fill: false,
                lineTension: 0
            }
        ]
    },
    options: {
        hover: {mode: null},
        legend: {
            display: false
        },
        tooltips: {enabled: false},
        hover: {mode: null},
        scales: {
            xAxes: [{
                gridLines: {
                    // drawBorder: false,
                },
            }],
            yAxes: [{
                display: false,
                ticks: {
                    suggestedMin: 0,
                    max: 60,
                    beginAtZero: true
                }
            }]
        }
    }
}

var ctx = document.getElementById(elementID).getContext('2d');
new Chart(ctx, options);

Does anybody know how I can remove those unnecessary overlaying lines using Chart.js? 有人知道我如何使用Chart.js删除那些不必要的重叠线吗?

You can use the following chart plugin to remove those unnecessary overlaying lines : 您可以使用以下图表插件删除那些不必要的重叠线:

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) {
         var x = x_axis.getPixelForValue(label);
         ctx.save();
         ctx.beginPath();
         ctx.moveTo(x, topY);
         ctx.lineTo(x, bottomY);
         ctx.lineWidth = 1;
         ctx.strokeStyle = x_axis.options.gridLines.color;
         ctx.stroke();
         ctx.restore();
      });
   }
});

ᴡᴏʀᴋɪɴɢ ᴅᴇᴍᴏ ⧩ ᴡᴏʀᴋɪɴɢ

 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) { var x = x_axis.getPixelForValue(label); ctx.save(); ctx.beginPath(); ctx.moveTo(x, topY); ctx.lineTo(x, bottomY); ctx.lineWidth = 1; ctx.strokeStyle = x_axis.options.gridLines.color; ctx.stroke(); ctx.restore(); }); } }); var options = { type: 'bar', data: { labels: ["1", "2", "3", "4", "5"], datasets: [{ borderWidth: 2, borderColor: "#5d5d5d", pointBorderColor: "#5d5d5d", pointBackgroundColor: "#5d5d5d", pointBorderWidth: 5, type: 'line', data: [26, 26, 33, 28, 30], fill: false, lineTension: 0 }, { borderWidth: 3, pointBorderColor: "#b8b8b8", pointBackgroundColor: "#b8b8b8", pointBorderWidth: 10, type: 'line', data: [26, 26, 29, 28, 29], fill: false, lineTension: 0 }, { data: [0, 0, 0, 0, 0], fill: false, lineTension: 0 }] }, options: { hover: { mode: null }, legend: { display: false }, tooltips: { enabled: false }, hover: { mode: null }, scales: { xAxes: [{ gridLines: { // drawBorder: false, }, }], yAxes: [{ display: false, ticks: { suggestedMin: 0, max: 60, beginAtZero: true } }] } } } var ctx = document.getElementById('canvas').getContext('2d'); new Chart(ctx, options); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> <canvas id="canvas"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 chart.js 仅绘制图形和 Xaxis 网格线 - How to only draw the graph and the Xaxis gridlines using chart.js 如何使用 Chart.js 有条件地显示/隐藏 yAxis 上的网格线? - how can conditionally show/hide gridlines on the yAxis using Chart.js? 如何为未显示的刻度线保留网格线? [chart.js之] - How do I persist the gridlines for ticks that are not displayed? [Chart.js] 如何对齐垂直网格线上的 X 轴刻度(Chart.js、条形图) - How to align X-Axis ticks on vertical gridlines (Chart.js, Bar-Chart) CHART.JS如何才能将y轴上的标签偏移/移动/调整为位于网格线的中间,而不是居中于网格线? - CHART.JS How can I offset/move/adjust the labels on the y-axis to be in the middle of the gridlines instead of centered on the gridlines? 如何删除chart.js上的网格 - How to remove grid on chart.js Chart.js 版本 3:如何为刻度线和网格线设置不同的颜色 - Chart.js Version 3: how to set the different color for tick lines and gridlines 如何减少堆栈条形图chart.js中多条网格线的数量 - How to reduce number of multiple gridlines in stack bar graph chart.js 如何在Y轴上禁用标签文本但是在Chart.js 2.6中保留网格线? - How do I disable Label Text on Y-Axis but keep the gridlines in Chart.js 2.6? chart.js控制网格线之间的间距/大小 - chart.js control space between/size of gridlines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM