javascript - 如何使ChartJS中的Line不超过yAxes的最大值

标签 javascript chart.js

我希望此 ChartJS 中的线条不要超过最大 yAxes,或者换句话说,即使 datasets.data 超过 yAxes,线条仍保留在元素内,我自己搜索了文档但没有找到它

像这样

enter image description here

不是这样的 enter image description here

这是我的代码

// Chartjs
var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['SUN', 'MON', 'TUE', 'WED', 'THU', "FRI", 'SAT'],
        datasets: [{
            label: 'Daily Data',
            data: [730000, 1012000, 1220000, 1831000, 560000, 2012000, 890000],
            borderColor: '#3f89fb',
            borderWidth: 3,
            fill: false
        }]
    },
    options: {
      scales: {
        // X or Horizontal
        xAxes: [{
          gridLines: {
            color: "rgba(0, 0, 0, 0)",
          }
        }],
        // Y or Vertical
        yAxes: [{
            stacked: true,
            ticks: {
              beginAtZero:true,
              max: 1000000,
              min: 0,
              stepSize: 200000,
              callback: function(value, index, values) {
                return value / 1e6 + 'M';
              }
            },
            gridLines: {
                color: "rgba(0, 0, 0, 0)",
            }
        }]
      },
      // Tooltips
      tooltips: {
        callbacks: {
          label: function(tooltipItem, chart) {
            let datasetsData = chart.datasets[0].data[tooltipItem.index];
            if (datasetsData.toString().length <= 6) {
              return 'Income Rp. '+datasetsData.toLocaleString() + 'K';
            }else {
              return 'Income Rp. '+datasetsData.toLocaleString() + ' M';
            }
          }
        }
      }
    }
});

最佳答案

根据您的代码,您可以执行以下操作: 将您的数据移动到像

这样的 var
var myData = [730000, 1012000, 1220000, 1831000, 560000, 2012000, 890000];

然后将数据字段更改为 data: myData, 然后将您的刻度更改为

        ticks: {
          beginAtZero:true,
          max: Math.max.apply(Math, myData), //Sets the max to the max data you have
          min: 0,
          stepSize: 200000,
          callback: function(value, index, values) {
            return value / 1e6 + 'M';
          }
        },

您可以添加一些东西到最大值以使其更大一点。

关于javascript - 如何使ChartJS中的Line不超过yAxes的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53046450/

相关文章:

javascript - 如何使用chart.js在条形图中绘制多个颜色条

javascript - 我可以根据 Chart.js 中的给定阈值设置不同的填充颜色吗?

angularjs - 如何将文本或符号附加到 chart.js 的工具提示

javascript - Socket.IO 客户端 .js 文件位于何处?

javascript - 我如何使用 Node.js Express 压缩中间件?

javascript - 如何访问组件脚本中的私有(private)对象?

javascript - 停止 Google Maps API v3 中的空闲事件

javascript - 在 VBScript 中引用全局变量的方式与在 Javascript 中的 window ["var_name"] 相同

javascript - 如何设置Y轴上显示的最高值的下限?

javascript - Chartjs destroy 方法不影响图表