I got this chart

I am implementing admin dashboard that will show information about CPU usage

因此,我需要显示y轴上0-100(百分比)的值范围,但当我try 实现这一点时,图表会动态更改y轴值,而不是静态的0-100

javascript

new Chart(document.querySelector('#lineChart'), {
          type: 'line',
          data: {
            labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
            datasets: [{
              label: 'Line Chart',
              data: [20, 30, 80, 15, 5, 4, 55],
              fill: true,
              borderColor: 'rgb(75, 192, 192)',
              tension: 0.1
            }]
          },
          options: {
            scales: {
              y: {
                type: 'linear',
                ticks: {
                  beginAtZero: true,
                  stepSize: 20, // Customize the step size if needed
                  min: 0,
                  max:100,
                  suggestedMax: Math.max(maxValue,100), // Set the maximum value of the y-axis to 100
                },
              }
            }
          }
        });

html

          <canvas id="lineChart" style="max-height: 400px; display: block; box-sizing: border-box; height: 234px; width: 469px;" width="469" height="234"></canvas>


推荐答案

这并不是因为选项值发生了变化,而是因为其中一些值在您的配置对象中放错了位置,所以它们被忽略了.

minmaxbeginAtZerosuggestedMax选项应该是scales.y对象的键,也就是说,它们应该在ticks对象之外.但是,如果设置了min: 0max:100,则不需要另外两个. 你的选项中唯一一个很好地放在ticks对象中的是stepSize.

new Chart(document.querySelector('#lineChart'), {
    type: 'line',
    data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'Line Chart',
            data: [20, 30, 80, 15, 5, 4, 55],
            fill: true,
            borderColor: 'rgb(75, 192, 192)',
            tension: 0.1
        }]
    },
    options: {
        scales: {
            y: {
                type: 'linear',
                min: 0,
                max:100,
                //beginAtZero: true,
                //suggestedMax: Math.max(maxValue,100), 
                ticks: {
                    stepSize: 20, // Customize the step size if needed
                },
            }
        }
    }
});
<canvas id="lineChart" style="max-height: 400px; display: block; box-sizing: border-box; height: 234px; width: 469px;" width="469" height="234"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.js" integrity="sha512-ZwR1/gSZM3ai6vCdI+LVF1zSq/5HznD3ZSTk7kajkaj4D292NLuduDCO1c/NT8Id+jE58KYLKT7hXnbtryGmMg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Javascript相关问答推荐

当在select中 Select 选项时,我必须禁用不匹配的 Select

node TS:JWT令牌签名以验证客户端和后台问题之间的身份验证

yarn安装一个本地npm包,以便本地包使用main项目的node_modules(ckeditor-duplicated-modules错误)

Mongoose post hook在使用await保存时不返回Postman响应

分层树视图

如何在每次单击按钮时重新加载HighChart/设置HighChart动画?

使用Java脚本导入gltf场景并创建边界框

JS:XML insertBefore插入元素

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

在画布中调整边上反弹框的大小失败

无法避免UV:flat的插值:非法使用保留字"

MongoDB受困于太多的数据

为什么在函数中添加粒子的速率大于删除粒子的速率?

输入的值的类型脚本array.排序()

使用API调用的VUE 3键盘输入同步问题

更新文本区域行号

将以前缓存的 Select 器与querySelector()一起使用

如何调整下拉内容,使其不与其他元素重叠?

MAT-TREE更多文本边框对齐问题

将匿名函数附加到链接的onClick属性