简体   繁体   English

如何从chart.js 中的折线图底部删除多余的空格?

[英]How can I remove extra whitespace from the bottom of a line chart in chart.js?

As you can see below with the canvas element highlighted, there is considerable whitespace below the x axis label.正如您在下面突出显示的画布元素所见,x 轴标签下方有相当多的空白。 If I resize the canvas, the whitespace stays proportional to the height of it.如果我调整画布的大小,空白与它的高度成正比。 Are there any settings that control this whitespace?是否有任何设置可以控制此空白? I've ruled out padding and do not see any settings for margins.我已经排除了填充并且没有看到任何边距设置。

Thanks!谢谢!

x 轴下方带有额外空白的折线图

Here is the JavaScript that renders the chart:这是呈现图表的 JavaScript:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  options: {
    legend: {
      display: false
    },
    elements: {
      point: {
        radius: 0
      }
    },
    scales: {
      xAxes: [{
        gridLines: {
          display: false,
          drawBorder: true
        },
        ticks: {
          autoSkip: true,
          maxTicksLimit: 1
        }
      }],
      yAxes: [{
        gridLines: {
          display: true,
          drawBorder: false
        },
        ticks: {
          callback: function(value, index, values) {
            return '$' + addCommas(value);
          }
        }
      }]
    },
    layout: {
      padding: 5
    }
  },
  type: 'line',
  data: {
    labels: labels,
    datasets: [
      { 
        data: values,
        fill: false,
        borderColor: "blue"
      }
    ]
  }
});

And the complete jsfiddle: https://jsfiddle.net/rsn288fh/2/以及完整的 jsfiddle: https ://jsfiddle.net/rsn288fh/2/

I know you said you ruled out padding, but this is the only option I can see working:我知道你说你排除了填充,但这是我能看到的唯一选项:

options: {
    layout: {
        padding: {
            bottom: -20
        }
    }
}

Obviously you can play with the -20 to what works for you.显然,您可以使用-20来玩适合您的游戏。

Here is the reference for padding for chartjs, if you wanted to see more如果您想查看更多信息,请参阅chartjs 的填充参考

EDIT:编辑:

I've updated your jsfiddle , with a colored div below the chart.我已经更新了你的jsfiddle ,图表下方有一个彩色的 div 。 As you resize it seems to stay at the same spot below the chart.当您调整大小时,它似乎停留在图表下方的同一位置。

Changing the tickMarkLength to 0 results in no padding on the left or bottom of the chart (or wherever your axis happens to be).tickMarkLength更改为 0 会导致图表左侧或底部(或轴所在的任何位置)没有填充。

https://www.chartjs.org/docs/latest/axes/styling.html#grid-line-configuration https://www.chartjs.org/docs/latest/axes/styling.html#grid-line-configuration

tickMarkLength刻度线长度

Length in pixels that the grid lines will draw into the axis area.网格线将绘制到轴区域的长度(以像素为单位)。

const options = {
  scales: {
    xAxes: [
      {
        ticks: {
          display: false,
        },
        gridLines: {
          display: false,
          tickMarkLength: 0,
        },
      },
    ],
    yAxes: [
      {
        ticks: {
          display: false,
        },
        gridLines: {
          display: false,
          tickMarkLength: 0,
        },
      },
    ],
  },
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM