[Chartjs]-Chart.js line chart is cut off at the top?

25๐Ÿ‘

Edit/Update: As mentioned in the comments, the 5px value can more accurately be just half of whatever your line width value is, I belive default is 2px so in that case you would just want padding: { top: 1 }

There a layout.padding attribute you can set either in options or global. I had this same problem and set

options: {
  layout: {
    padding: {
      top: 5
    }
  },
  ...
}

worked fine for me to not cut off the line
http://www.chartjs.org/docs/#chart-configuration-layout-configuration

14๐Ÿ‘

You can try clip:false option, worked for me.

Example:

options: {
    clip: false,
    responsive: true,
    interaction: {
      intersect: false,
      mode: 'index',
    }
 }

5๐Ÿ‘

I used hardcode, just wide draw area at top and bottom. This code based on original Chart.canvasHelpers.clipArea.

const WIDE_CLIP = {top: 2, bottom: 4};

Chart.canvasHelpers.clipArea = function(ctx, clipArea) {
    ctx.save();
    ctx.beginPath();
    ctx.rect(
      clipArea.left,
      clipArea.top - WIDE_CLIP.top,
      clipArea.right - clipArea.left,
      clipArea.bottom - clipArea.top + WIDE_CLIP.bottom
    );
    ctx.clip();
};

1๐Ÿ‘

I found this while making line graphs that had the legend displayed at the top. The only work around I found was to move the legend to the bottom

options: {
    legend: {
       position: 'bottom',
    },
}

fiddle

1๐Ÿ‘

I ran into this Chart.js bug, too.

A very recent fix is shown here.
Youโ€™d have to manually edit your Chart.js file src/controllers/controller.line.js
(For Angular 2, this file path will be located inside directory node_modules/chart.js/.)

Or just wait for the next Chart.js release which will most likely contain the fix.

An alternative workaround is described in comment 1 of this bug ticket:
https://github.com/chartjs/Chart.js/issues/4202

0๐Ÿ‘

The options.layout.padding.top solution didnโ€™t work for me (just added padding AFTER the line cut), so I extracted the high/low values from my data and used the suggestedMin and suggestedMax config params like this:

var suggestedMin = {MIN};
var suggestedMax = {MAX};

options: {
    scales: {
        yAxes: [{
            ticks: {
                suggestedMin: suggestedMin - 1,
                suggestedMax: suggestedMax + 1,
            }
        }]
    }
}

0๐Ÿ‘

I had my data values stored in array. So I set my max and min values like this:

var _array = [1,3,2];
var suggestedMax = Math.max.apply(Math,_array); // 3
var suggestedMin = Math.min.apply(Math,_array); // 1

And than you can set

    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: false,
                    suggestedMin: suggestedMin - 1,
                    suggestedMax: suggestedMax + 1,
                }
            }]
        }

    }

And it works, thank you.

0๐Ÿ‘

This issue has been fixed in the Chart.js library. Just update it and youโ€™ll be fine.

Leave a comment