ReCharts with multiple lines

Ating picture Ating · Sep 27, 2019 · Viewed 7k times · Source

I want to create A ReChart component, which has multiple lines.

SO far i have this dummy code.

<LineChart
  data={data}
  margin={{
    top: 16,
    right: 16,
    bottom: 0,
    left: 24
  }}
>
  <XAxis dataKey="time" />
  <YAxis>
    <Label angle={270} position="left" style={{ textAnchor: "middle" }}>
      Cyber assesment
    </Label>
  </YAxis>
  <Line type="monotone" dataKey="amount" stroke="#556CD6" dot={false} />
</LineChart>

with the data being an array, with a two value object:

function createData(time, amount) {
  return { time, amount };
}

const data = [
  createData('00:00', 0),
  createData('03:00', 300),
  createData('06:00', 600),
  createData('09:00', 800),
  createData('12:00', 1500),
  createData('15:00', 2000),
  createData('18:00', 2400),
  createData('21:00', 2400),
  createData('24:00', undefined),
];

i want to make it so i can have multiple lines plotted, where i tried creating a different function and adding other data, to the same array, but with no good result.

How can this be achived?

i tried to create a working example, but without any luck

function createData(amountA, amountB) {
  return {a: amountA, b: amountB  };
}

const data = [
  createData(100, 200),
  createData(200, 400),
  createData(400, 500),
  createData(600, 800),
  createData(650, 700),
  createData(900, 800)
];

export default function Chart() {
    console.log(data)
  return (
    <React.Fragment>
      <Title>Performance indication</Title>
      <ResponsiveContainer>
        <LineChart
          data={data}
          margin={{
            top: 16,
            right: 16,
            bottom: 0,
            left: 24,
          }}
        >
          <XAxis dataKey="time" />
          <YAxis>
            <Label angle={270} position="left" style={{ textAnchor: 'middle' }}>
              Cyber assesment
            </Label>
          </YAxis>
          <Line type="monotone" dataKey={data.a} stroke="#556CD6" dot={false} />
          <Line type="monotone" dataKey={data.b} stroke="#556CD6" dot={false} />
        </LineChart>
      </ResponsiveContainer>
    </React.Fragment>

Answer

Dupocas picture Dupocas · Sep 27, 2019

Just declare two Line and two YAxis components inside your LineChart and explicitly declare yAxisId. Here is an example of one of ours

<LineChart data={data} margin={{ left: -10, right: 10 }}>
    <XAxis height={40} dataKey="part" tick={{ fontSize: 10 }}>
        <Label
            value='Período'
            position='insideBottom'
            fontSize={14}
            fill='#676767'
        />
    </XAxis>
    <YAxis width={80} yAxisId="left" tick={{ fontSize: 10 }}>
        <Label
            value={selected.A}
            angle={-90}
            position='outside'
            fill='#676767'
            fontSize={14}
        />
    </YAxis>
    <YAxis width={80} yAxisId="right" orientation="right" tick={{ fontSize: 10, }}>
        <Label
            value={selected.B}
            angle={-90}
            position='outside'
            fill='#676767'
            fontSize={14}
        />
    </YAxis>
    <Tooltip formatter={(value) => floatParser(value)} />
    <Line yAxisId="left" type="monotone" dataKey={selected.A} stroke={colors[1]} />
    <Line yAxisId="right" type="monotone" dataKey={selected.B} stroke={colors[0]} />
</LineChart>

enter image description here

data array:

const data = [
    { part: 'foo', axisA: 21211, axisB: 1232 }
]