简体   繁体   中英

How to limit xAxes and yAxes in React Chart JS?

I'm trying to make the below graph using chart js in React 18:

在此处输入图像描述

So far I've written the below code and tried to limit the x and y axes through maxTicksLimit, but I suppose I'm missing something here:

import "./styles.css";
import { data } from "./data";
import { Line } from "react-chartjs-2";
import moment from "moment";

export default function App() {
  const series = data.cpu.values.map((item) => item[1]);
  const labels = data.cpu.values.map((item) =>
    moment(item[0] * 1000).format("hh:mm:ss")
  );

  const options = {
    responsive: true
  };

  const datax = {
    labels,
    datasets: [
      {
        label: "CPU USAGE",
        data: series,
        borderColor: "rgb(255, 99, 132)",
        backgroundColor: "rgba(255, 99, 132, 0.5)",
        pointRadius: 0,
        fill: true
      }
    ],
    scales: {
      xAxes: [
        {
          ticks: {
            maxTicksLimit: 9
          }
        }
      ],
      yAxes: [
        {
          ticks: {
            maxTicksLimit: 4
          }
        }
      ]
    }
  };

  return (
    <div className="App">
      <Line options={options} data={datax} />
    </div>
  );
}

I get the below output:

在此处输入图像描述

I would appreciate any help CodeSandBox

To solve your problem, define options as follows.

const options = {
  responsive: true,
  scales: {
    x: {
      ticks: {
        maxTicksLimit: 9
      }
    },
    y: {
      ticks: {
        maxTicksLimit: 4
      }
    }
  }
};

For further information, consult Tick Configuration Options from the Chart.js documentation.

Please take a look at your amended CodeSandbox and see how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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