Cindy Burker
Cindy Burker

Reputation: 127

React react-chartjs-2 adding x and y axis labels to a line graph

import React from "react";
import { Line } from "react-chartjs-2";

// "chart.js": "^2.9.4",
// "react-chartjs-2": "^2.11.1"
const ext_data = [11, 19, 3, 5, 2, 3, 14, 19];
const ll = Array.from(Array(ext_data.length).keys());

const data = {
  labels: ll,
  datasets: [
    {
      label: "Profit$",
      data: ext_data,
      fill: false,
      backgroundColor: "rgb(255, 99, 132)",
      borderColor: "rgba(255, 99, 132, 0.4)"
    }
  ]
};

const options = {
  scales: {
    yAxes: [
      {
        ticks: {
          beginAtZero: true
        }
      }
    ]
  }
};

const LineChart = () => (
  <>
    <div className="header">
      <h1 className="title">Profit Trend</h1>
      <div className="links"></div>
    </div>
    <Line data={data} options={options} />
  </>
);

export default LineChart;

I am trying to add x-axis name : "time" and y-axis name: "number of cars". I went into the documentation : https://www.chartjs.org/docs/latest/axes/labelling.html However, I was not able to find something that was useful. Thank you for your time.

Upvotes: 0

Views: 3692

Answers (1)

Nightcoder
Nightcoder

Reputation: 76

In react-chartjs-2, to add your title to your axis, just add this to your yAxes array of options (options.scales.yAxes[]):

scaleLabel: {
   display: true,
   labelString: "number of cars",
   fontColor: "color you want",
 }

same for your xAxes.

Upvotes: 2

Related Questions