Pep
Pep

Reputation: 97

How to display multiple arrays of objects in Recharts?

How can I display two arrays of objects in Recharts ?

I want to display data_Chart in one Line and the response from the subscription in a different Line element...

How can this be achieved?

Response from Subscription in Grapqhl

enter image description here

I have tried with no success, thank you for the help!

My code:

import React, { Fragment, useState } from 'react';
import { useSubscription } from '@apollo/react-hooks';
import Box from '@material-ui/core/Box';
import { CRYPTO_ADDED } from '../../subscriptions';
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';

const data_Chart = [
  { name: 'Page A', uv: 4000, pv: 2400, amt: 2400 },
  { name: 'Page B', uv: 3000, pv: 1398, amt: 2210 },
  { name: 'Page C', uv: 2000, pv: 9800, amt: 2290 },
  { name: 'Page D', uv: 2780, pv: 3908, amt: 2000 },
  { name: 'Page E', uv: 1890, pv: 4800, amt: 2181 },
  { name: 'Page F', uv: 2390, pv: 3800, amt: 2500 },
];

const Dashboard = () => {

  const [topCrypto, setTopCrypto] = useState([]);
  useSubscription(CRYPTO_ADDED, {
    onSubscriptionData: ({subscriptionData}) => {
      setTopCrypto(topCrypto.concat(subscriptionData.data.cryptoAdded));
      console.log('topCrypto',topCrypto)
    }
  });


  let currVal = '';
  Object.keys(topCrypto).map((obj, indx) => {
    currVal = topCrypto[obj].PRICE;

  });

  return (
    <Fragment>
      <Box color="text.primary">
        <h2 className="m-2"> BTC price </h2>
          <h2>$ {currVal} </h2>

      </Box>
      <LineChart width={1100} height={685} data={topCrypto} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="LASTUPDATE" />
        <YAxis />
        <Tooltip />
        <Legend />
        <Line
          type="monotone"
          label="LASTUPDATE"
          dataKey="PRICE"
          stroke="#8884d8"
          activeDot={{ r: 8 }}
          strokeDasharray="5 5"
        />
      </LineChart>
    </Fragment>
  );
};

export default Dashboard;

Something like this: enter image description here

Upvotes: 0

Views: 3422

Answers (1)

Alex
Alex

Reputation: 3991

In recharts, you can add multiple line with different dataKey

<LineChart
        width={400}
        height={300}
        data={sample}
        margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
      >
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="LASTUPDATE" />
        <YAxis  />
        <Tooltip />
        <Legend />
        <Line //first line
          type="monotone"
          dataKey="PRICE1"
          stroke="#ff84d8"
          activeDot={{ r: 8 }}
          strokeDasharray="5 5"
        />
        <Line //second line
          type="monotone"
          dataKey="PRICE2"
          stroke="#8884d8"
          activeDot={{ r: 8 }}
          strokeDasharray="5 5"
        />
      </LineChart> 

temporary sample codesandbox


You need to identify and decide the shape of comparable data
one option could be like this

[  {PRICE1: 8310.45,PRICE2: 7800.45},{PRICE1: 8302.45,PRICE2: 7050.45},....]

another one could be with two datasource

 PRICE1=  [ 8310.45,8302.45,....]
 PRICE2=  [7800.457050.45,....]

or somethings else...

Upvotes: 1

Related Questions