chart.js如何显示最大值?

时间:2018-12-13 09:09:00

标签: javascript charts chart.js

我需要显示右上角图片中的最大值。 chart.js选项是否有可能?我认为它将与插件一起使用,但是我不知道如何制作它

new Chart(document.getElementById('chart'), {
type: 'line',
data: {
labels: [0, 1, 2, 3, 4, 5],
datasets: [{
  label: 'series 1',
  data: [0, 2, 4, 3, 1, 0]
}]
},
options: {
maintainAspectRatio: false,
scales: {
  xAxes: [{
    gridLines: {
      display: false, // must be false since we're going to draw our 
      own 'gridlines'!
      color: 'rgba(255, 0, 0, .2)', // can still set the colour.
      lineWidth: 5 // can still set the width.
    }
  }],
  yAxes: [{
    gridLines: {
      display: false
    },
    ticks: {
      beginAtZero: true
    }
  }]
}
},

});

enter image description here

1 个答案:

答案 0 :(得分:1)

JavaScript代码

var maxVal = Math.max.apply(null,data)// 4 document.getElementById(“ demo”)。innerHTML = maxVal;

HTML代码

最大值

您已经知道y轴数据,因此使用Math.max.apply函数将y轴数据数组传递给该函数,然后在该数组中找到最大值并将其存储在变量中,最后显示元素使用ID为innerhtml的DOM或canvas对象在上图中显示该元素。使用CSS对齐