最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReCharts with multiple lines - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question edited Sep 27, 2019 at 11:48 sigmus 3,2373 gold badges24 silver badges31 bronze badges asked Sep 27, 2019 at 11:32 Kristoffer TølbøllKristoffer Tølbøll 3,3279 gold badges40 silver badges84 bronze badges 1
  • I'm not sure I understand what your issue is. If you want more than one line plotted, you need to add key in your data object (see jsfiddle.net/alidingling/xqjtetw0) – 7hibault Commented Sep 27, 2019 at 11:37
Add a comment  | 

2 Answers 2

Reset to default 9

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>

data array:

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

With the latest version now you can directly add your data at the component.

like this Recharts multiple Lines with different datasets

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

 <Line data={data} type="monotone" dataKey="uv" stroke="#8884d8"/>
 <Line data = {data2} type="monotone" dataKey="pv" stroke="#82ca9d"/> 

Example

发布评论

评论列表(0)

  1. 暂无评论