Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding inside the canvas with a line chart without scale #2872

Closed
gagagast opened this issue Jun 29, 2016 · 19 comments
Closed

Padding inside the canvas with a line chart without scale #2872

gagagast opened this issue Jun 29, 2016 · 19 comments

Comments

@gagagast
Copy link

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
                }

              }]
            }

          }
        });
@Davy-F
Copy link

Davy-F commented Jun 29, 2016

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.

@Davy-F
Copy link

Davy-F commented Jul 5, 2016

Anything?

@etimberg
Copy link
Member

etimberg commented Jul 5, 2016

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

@Davy-F
Copy link

Davy-F commented Jul 5, 2016

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

@etimberg
Copy link
Member

etimberg commented Jul 5, 2016

Possibly. But it might be a duplicate of #2768

@gagagast
Copy link
Author

gagagast commented Jul 6, 2016

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

@etimberg
Copy link
Member

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
      }
    }]
  }
}

@carlesnunez
Copy link

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
          }
        }],
      },

@ryana
Copy link

ryana commented Dec 1, 2016

@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.

@ryana
Copy link

ryana commented Dec 1, 2016

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.

@rishabhjain
Copy link

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

@ezekg
Copy link

ezekg commented Mar 7, 2017

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

@groenroos
Copy link

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

@kadnan
Copy link

kadnan commented Jan 24, 2018

Anyone able to sort this out?

@js1m
Copy link

js1m commented Feb 1, 2018

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.

@tkarpenko
Copy link

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
            }
        }
    }
});

@miry
Copy link

miry commented Jul 17, 2020

I have similar problem (at least I think): The top grid line is cut. Version: [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?

@anthonylan
Copy link

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

@himynameisubik
Copy link

himynameisubik commented Feb 22, 2021

Sorry for the grave-digging but for me none of the above solutions worked.
I needed a combination of removing/hiding all the elements (gridLines, legends, tooltips etc) and setting the max & min ticks on the yAxis to the max and min values of the data array.

scales: {
	yAxes: [{
		ticks: {
			beginAtZero: false,
			min: Math.min.apply(null, this.chartData.datasets[0].data),
			max: Math.max.apply(null, this.chartData.datasets[0].data)
		}
	}]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests