简体   繁体   English

Chart.js 折线图 y 轴不从 0 开始

[英]Chart.js Line graph y-axis not starting from 0

线形图 This is my current line graph.这是我目前的折线图。 The x-axis is just a mock data so hope u guys can overlook the bad data used. x轴只是一个模拟数据,所以希望你们可以忽略使用的不良数据。 As you can see on the y-axis, it is starting from 60 which is not what i want.正如您在 y 轴上看到的,它从 60 开始,这不是我想要的。 I have tried beginAtZero = true but its not working.我试过beginAtZero = true但它不起作用。 I would also like to remove the label weight but by simply removing not putting the labels, it would say undefined .我还想删除 label weight ,但只需删除不放置标签,就会显示undefined

Below is the code for the line graph.下面是折线图的代码。

import React from 'react'
import {Line} from 'react-chartjs-2'

function LineChart(datasets) {
  const dataset = datasets.data
  return (
    <div>
      <Line
        data={{
          labels: dataset.map(data=> data.created_at),
          datasets: [{
            label: 'Weight',
            data: dataset.map(data => data.weight),
            backgroundColor: 'rgba(255, 99, 132, 0.2)',
            borderWidth: 1,
            borderColor: 'rgb(255, 99, 132)',
          }]
        }}
        height = {200}
        options = {{
          resposive: true,
          maintainAspectRatio: true,
          scales: {
            yAxes: [{
                ticks: {
                    min: 0,
                    beginAtZero: true,
                }
            }]
          }
        }}
      />
    </div>
  )
}

export default LineChart

It seems that you're using Chart.js version 3, where the scales option needs to be defined differently (see Chart.js documentation ).您似乎使用的是 Chart.js 版本 3,其中scales选项需要以不同方式定义(请参阅Chart.js 文档)。

options = {{ 
  resposive: true,
  maintainAspectRatio: true,
  scales: {
    y: {
      beginAtZero: true          
    }
  }      
}} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM