Chart.js: Padding inside the canvas with a line chart without scale

Created on 29 Jun 2016  路  18Comments  路  Source: chartjs/Chart.js

Hello,
How can i make the line graph stick to the exact border of the canvas ?
I 've made a line graph without scales and there are always a gap between the left and the bottom borders of the canvas and the beginning of the charts... I try to play with the responsive and the maintainAspectRatio options without success... here is my configuration .

Thx

new Chart(ctx, {
          // responsive: false,
          type: 'line',
          // maintainAspectRatio:false,
          data: {
            labels: labels,
            datasets: [{
              fill:true,
              backgroundColor: "#47afbd",
              data: data
            }]
          },
          options: {
            defaultFontSize:10,
            animation:{
              onProgress:function(){
                if(self.$('#engagement-chart-wrap').hasClass('isLoading')){
                  self.$('#engagement-chart-wrap').removeClass('isLoading');
                }
              }
            },
            legend: {
              display: false
            },
            tooltips:{
              enabled:false
            },
            elements:{
              point:{
                radius:0
              }
            },
            scales: {

              yAxes: [{

                ticks :{
                  display:false,
                  padding:0
                },
                gridLines:{
                  drawTicks:false,
                  drawBorder:false
                },
                scaleLabel:{
                  display:false
                }

              }],
              xAxes: [{
                ticks :{
                  display:false,
                  padding:0

                },
                gridLines:{
                  display:false,
                  drawTicks:false,
                  drawBorder:false
                }

              }]
            }

          }
        });
bug

Most helpful comment

Adding the layout padding works for me

let chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        layout: {
            padding: {
                left: 0,
                right: 0,
                top: 10,
                bottom: 10
            }
        }
    }
});

All 18 comments

I'm also getting padding issues without a scale. But mine are out of range.
How can we add padding to the canvas? (Chart v2)

With scale. e.g.

xAxes: [{
            display: true

image

Without e.g.

xAxes: [{
            display: false

image

Only seems to happen when both x and y axis display is equal to false.

Anything?

There are currently no options to do this. If someone wants to submit a PR, I can look at it

Thanks for the reply. Isn't this a bug, rather than an option though?

Possibly. But it might be a duplicate of #2768

Not Really... My chart appears always inside the limits but doesn't fill the canvas element.

I found the bug here. It's that the gridLines.tickMarkLength is still used even if drawTicks is false. Until this is fixed, this is a good work around

options: {
  scales: {
    yAxes: [{
      gridLines: {
        tickMarkLength: 0
      }
    }]
  }
}

I was not able to make work the chart with @etimberg configuration. Instead I set to display none all the lines of my chart:

      scales: {
        xAxes: [{
        gridLines: {
          display: false,
          drawBorder: false,
        },
        scaleLabel: {
          display: false
        },
        ticks: {
          display: false
        }
        }],
        yAxes: [{
          gridLines: {
            display: false,
            drawBorder: false
          },
          scaleLabel: {
            display: false
          },
          ticks: {
            display: false
          }
        }],
      },

@carlesnunez's solution partially worked for me. I'm still running into an issue on the top of the chart.

Sides and bottom working

^ Sides and bottom not cut off

Top cut off

^ Top cut off :(

I'll post back when we figure it out, but if anyone else could help, I'm all ears.

FYI I just realized that I can dynamically set yAxes.0.ticks.max to be a little greater than the max value in my dataset and it fixes this even with yAxes.0.ticks.display is false.

Adding negative left and bottom layout padding(http://www.chartjs.org/docs/#chart-configuration-layout-configuration) seems to be working.

I'm still hitting this issue. Padding does not fix it. I'm having to dynamically adjust the yAxes ticks min/max by ~5, as @ryana had mentioned earlier.

const min = Math.min(...data)
const max = Math.max(...data)

options.scales.yAxes[0].ticks.min = min - 5
options.scales.yAxes[0].ticks.max = max + 5

Before

image

After

image

I also still face this issue, and no padding setting seems to address it. I also continually modify the data in the chart, so keeping track of setting the minimum and maximum tick is a non-workable workaround for me.

options: {
    maintainAspectRatio: false,
    legend: {
        display: false
    },
    tooltips: {
        enabled: false
    },
    scales: {
        xAxes: [{
            display: false
        }],
        yAxes: [{
            display: false
        }]
    }
}

This configuration yields a chart where the top and bottom are partly cut off;

nayttokuva 2017-04-02 kello 16 16 34

Anyone able to sort this out?

So is it still not possible to make the graph completely fill the canvas element without losing the gridlines or using some other hack? I am using chart.js 2.6.0 and the graph does not scale to the edges of the canvas element, which makes it an aesthetical pain.

Adding the layout padding works for me

let chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        layout: {
            padding: {
                left: 0,
                right: 0,
                top: 10,
                bottom: 10
            }
        }
    }
});

I have similar problem (at least I think): The top grid line is cut. Version: chart.[email protected]

image

As you see the line on top with 100% value is cut by half and points have holes in the center.

My workaround:

options: {
      scales: {
        yAxes: [{
          ticks: {
            min: 0,
            max: 105,
            callback: function(value, index, values) {
              if (value == 105) {
                return '';
              }
              return value + '%';
            }
          }
        }]
      }
    }

It looks like:

image

To be perfect I would remove the last grid line for 105. Do you know if it is possible?

Adding the layout padding works for me

let chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        layout: {
            padding: {
                left: 0,
                right: 0,
                top: 10,
                bottom: 10
            }
        }
    }
});

It worked! Thanks alot

Was this page helpful?
0 / 5 - 0 ratings