如何在recharts图表中添加一条与先前线条相连的单独线?

huangapple go评论63阅读模式
英文:

How can I add separate line to my chart in recharts which will be connected with previous line?

问题

我有一个发现,您可以在屏幕截图上看到。但即使第一行的结束值与第二行的起始值相同,这两行仍然没有连接在一起请参见图表

我附上了我制作的codesandbox实现 https://codesandbox.io/s/two-separate-lines-wet8f9

我创建了两个独立的<Line />组件,使用不同的数据对象。第一个不包含第二行的数据。

英文:

I have a realization you may see on the screenshot. But this two separate lines is not connected to each other even if the end value of the first line is the same as start on the second line see the Chart

I've attached codesandbox realization I had made https://codesandbox.io/s/two-separate-lines-wet8f9

I have created two separate <Line /> components with different data objects. The first one doesn't contain the data from the second line

答案1

得分: 0

我在一些实验中找到的唯一答案是:

https://codesandbox.io/s/two-separate-lines-forked-lmjlzy?file=/src/App.tsx

如果您有更合适的解决方案,那将是非常好的。

英文:

The only answer for myself I figured out during some experiments
https://codesandbox.io/s/two-separate-lines-forked-lmjlzy?file=/src/App.tsx

If you have more apropriate solution, it would be great to see it

答案2

得分: 0

你没有使用正确的数据数组。data 中的每个对象都在图表中呈现一个点。然而,你的 mergedData 复制了“Page E”。

[...data, ...data2]

// ->
...
{name: "Page E", uv: 1890, pv: 4800, amt: 2181},
{name: "Page E", uv: 1890, pv2: 4800, amt: 2181},
...

因此,图表在 X 轴上呈现了具有相同名称的2个不同点。

你想要的是一个具有 pvpv2 值的单个点:

{
  name: "Page E",
  pv: 4800,
  pv2: 4800,
}

附注:为了清晰起见,你可以删除 uvamt,因为这些数据键没有分配给任何“Line”。

英文:

You're not using the correct data array. Each object in data renders a point in the chart. However, your mergedData duplicates Page E.

[...data, ...data2]

// -&gt;
...
{name: &quot;Page E&quot;, uv: 1890, pv: 4800, amt: 2181},
{name: &quot;Page E&quot;, uv: 1890, pv2: 4800, amt: 2181},
...

So the chart renders 2 different points in X with the same name.

What you want is a single point with both pv and pv2 values:

{
  name: &quot;Page E&quot;,
  pv: 4800,
  pv2: 4800,
}

如何在recharts图表中添加一条与先前线条相连的单独线?

Sidenote: for clarity you can remove uv and amt since there are no Line assigned for those data-key.

huangapple
  • 本文由 发表于 2023年2月16日 02:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定