recharts: Unable to use number type in both axis

I am trying to plot a time series graph using line chart. X axis will have time and Y axis will have price of an item. Since there is not date/time type in this library, I’m using a number corresponding to the time. But when I render, I get a console warning “x-axis should be category axis when the layout is horizontal” and an error “Uncaught TypeError: Cannot read property ‘coordinate’ of undefined”

Here is the code I am trying

import React from 'react';
import ReactDOM from 'react-dom';
import { LineChart, Line, CartesianGrid, YAxis, XAxis, Tooltip } from 'recharts';

const data = [
    {date:10, price:50 },
    {date:11, price:60 },
    {date:12, price:54 },
    {date:15, price:55 },
    {date:18, price:58 },
    {date:20, price:53 }
]

const SimpleLineChart = React.createClass({
    render() {
        return (
            <LineChart width={600} height={300} data={data} margin={{ top: 5, right: 20, bottom: 5, left: 0 }} >
              <Line type="monotone" dataKey="price" stroke="#8884d8" xAxisId="dateAxis" yAxisId="priceAxis" unit="Kg" />
              <CartesianGrid stroke="#ccc" strokeDasharray="5 5" />
              <XAxis type="number" dataKey="date" xAxisId="dateAxis" />
              <YAxis yAxisId="priceAxis" />
              <Tooltip />
            </LineChart>
        );
    }
})

ReactDOM.render(<SimpleLineChart />, document.querySelector("#main"));

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

@manuviswam
OK! We will discuss how to implement your case. In my opinion a time series chart is also a line chart, the only difference is that the ticks of x-axis in a time series chart are time strings. So I’d like to implement your case in two ways:

  1. Support smooth curve in ScatterChart
  2. Support two options of breakpoints display method: one is disconnected, another is skip and connected

Thank you @xile611 Problem with the scatter charts is that it lacks the curves 😉 It would be great if you can include a time series chart in your future plans. Thanks a lot

@manuviswam If both XAxis and YAxis are numeric axis, you should use ScatterChart. In your case, you should set the type of XAxis to be category just like this demo.

But I wonder why your data isn’t continuous in date.