Chart.js line chart is cut off at the top?

前端 未结 7 1487
死守一世寂寞
死守一世寂寞 2020-12-16 19:27

For some reasone all graphs are cut off at the highest value. How can i fix this? I can\'t use a fixed y-axis

相关标签:
7条回答
  • 2020-12-16 19:40

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

    var _array = [1,3,2];
    var suggestedMin = Math.max.apply(Math,_array); // 3
    var suggestedMax = 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 讨论(0)
  • 2020-12-16 19:43

    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 讨论(0)
  • 2020-12-16 19:47

    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

    0 讨论(0)
  • 2020-12-16 19:53

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

    0 讨论(0)
  • 2020-12-16 19:58

    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();
    };
    
    0 讨论(0)
  • 2020-12-16 19:59

    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 讨论(0)
提交回复
热议问题