如何获得yAxes的建议最大数量?

时间:2018-08-06 13:37:25

标签: javascript canvas chart.js

如何获取yAxes的建议最大编号? 例如,在图像中,legend2为4000,legend1为400)

传奇标签1:

Legend Label 1

传奇标签2:

Legend Label 2

var canvas = document.getElementById('myChart');
var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    label: "My First dataset",
    backgroundColor: "rgba(255,99,132,0.2)",
    borderColor: "rgba(255,99,132,1)",
    borderWidth: 2,
    hoverBackgroundColor: "rgba(255,99,132,0.4)",
    hoverBorderColor: "rgba(255,99,132,1)",
    data: [65, 59, 20, 81, 56, 55, 400],
  }]
};
var option = {
  scales: {
    yAxes: [{
      stacked: true,
      gridLines: {
        display: true,
        color: "rgba(255,99,132,0.2)"
      }
    }],
    xAxes: [{
      gridLines: {
        display: false
      }
    }]
  }
};

var myBarChart = Chart.Bar(canvas, {
  data: data,
  options: option
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>

问题在于数据标签超出了计划。

从GitHub链接到Codepen:Link to GitHub discussion

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取最大值:

// Find the scale in the chart instance
var axis = chartInstance.scales.<scale id>;
var max = axis.max;
var min = axis.min;

更多信息here

这里是fiddle,以获取更多详细信息。