简体   繁体   English

ChartJs。 如何将折线图拉伸到边缘?

[英]ChartJs. How to stretch up line graph to the edges?

I use ChartJs library for building graphs.我使用 ChartJs 库来构建图表。 For now my graphs have "side gaps".现在我的图表有“侧隙”。 I want stretch line graphics to the full width and height like this:我想像这样将线条图形拉伸到全宽和全高:

在此处输入图片说明

I tried to change canvas width and it works if canvas has not a full width, but I need to make it works also on full width:我试图改变画布宽度,如果画布没有全宽,它就可以工作,但我需要让它也能在全宽上工作:

My configurations:我的配置:

data: {
    datasets: [
      {
        data: bids,
        fill: true,
        pointHitRadius: 10,
        pointBorderWidth: 0,
        pointHoverRadius: 4,
        pointHoverBorderColor: 'white',
        pointHoverBorderWidth: 2,
        pointRadius: 0,
        backgroundColor: 'rgba(58, 196, 174, 0.4)',
        borderColor: '#3ac4ae',
        lineTension: 0.7,
        borderJoinStyle: 'miter',
      },
      {
        data: asks,
        pointRadius: 0,
        fill: true,
        pointHitRadius: 10,
        pointBorderWidth: 0,
        pointHoverRadius: 4,
        pointHoverBorderColor: 'white',
        pointHoverBorderWidth: 2,
        backgroundColor: 'rgba(255, 107, 66, 0.4)',
        borderColor: '#ff6b42',
        lineTension: 0.7,
        cubicInterpolationMode: 'default',
        borderJoinStyle: 'miter',
      }
    ]
  },
  options: {
    responsive: true,
    legend: {
      display: false
    },
    scales: {
      xAxes: [
        {
          display: true,
          type: 'linear',
          gridLines: {
            borderDashOffset: 0,
            display: true,
            color: 'rgba(255, 255, 255, .1)'
          },
          ticks: {
            autoSkip: false,
            callback: (value, index, values) => {
              console.log('valueskol', values)
              return value
            },
            beginAtZero: true,
            padding: 20,
          },
        }
      ],
      yAxes: [
        {
          display: true,
          type: 'linear',
          position: 'left',
          gridLines: {
            display: true,
            color: 'rgba(255, 255, 255, .1)',
            drawBorder: false,
            offsetGridLines: true,
            tickMarkLength: 0,
            drawOnChartArea: true
          },
          ticks: {
            min: 0.02,
            padding: 20,
            stepSize: 0.1,
          }
        }

Would be very grateful, if someone can help me.如果有人可以帮助我,将不胜感激。

Set xAxes.ticks.min to the minimum X value and .max to the maximum value, and set yAxes.ticks.padding to 0.xAxes.ticks.min设置为最小 X 值,将.max设置为最大值,并将yAxes.ticks.padding设置为 0。

Here's an example.这是一个例子。 I had to make up some numbers because the data is missing from your post:我不得不补一些数字,因为您的帖子中缺少数据:

没有左填充的图表

 const ctx = document.getElementById('myChart').getContext('2d'); const myChart = new Chart(ctx, { type: 'line', data: { datasets: [{ data: [{ x: 1, y: 12 }, { x: 2, y: 11 }, { x: 3, y: 10 }, { x: 4, y: 1 }, ], fill: true, pointHitRadius: 10, pointBorderWidth: 0, pointHoverRadius: 4, pointHoverBorderColor: 'white', pointHoverBorderWidth: 2, pointRadius: 0, backgroundColor: 'rgba(58, 196, 174, 0.4)', borderColor: '#3ac4ae', lineTension: 0.7, borderJoinStyle: 'miter', }, { data: [{ x: 6, y: 1 }, { x: 7, y: 3 }, { x: 8, y: 5 }, { x: 9, y: 11 }, ], pointRadius: 0, fill: true, pointHitRadius: 10, pointBorderWidth: 0, pointHoverRadius: 4, pointHoverBorderColor: 'white', pointHoverBorderWidth: 2, backgroundColor: 'rgba(255, 107, 66, 0.4)', borderColor: '#ff6b42', lineTension: 0.7, cubicInterpolationMode: 'default', borderJoinStyle: 'miter', } ] }, options: { responsive: true, legend: { display: false }, scales: { xAxes: [{ display: true, type: 'linear', gridLines: { borderDashOffset: 0, display: true, color: 'rgba(255, 255, 255, .1)' }, ticks: { autoSkip: false, callback: (value, index, values) => { console.log('valueskol', values) return value }, beginAtZero: true, padding: 20, min: 1, }, }], yAxes: [{ display: true, type: 'linear', position: 'left', gridLines: { display: true, color: 'rgba(255, 255, 255, .1)', drawBorder: false, offsetGridLines: true, tickMarkLength: 0, drawOnChartArea: true }, ticks: { padding: 0, } }] } } });
 <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script> <canvas id="myChart" width="400" height="400"></canvas>

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

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