Using `Fragment` inside chart
See original GitHub issueHi,
I’m having an issue with dynamically rendering charts. Basically my data provides on each element information for 2 Line
or Bar
elements. So I would like to do a data.map
and then render 2 Bars or Lines on each iteration. However, React/JSX requires me to wrap these elements in another element so preferably I’d wrap the two elements in a Fragment
tag but if I do this nothing shows up in my chart.
My current code looks like this and this works fine:
<LineChart data={data}>
<XAxis dataKey="timestamp" tickFormatter={tickFormatter} />
<Tooltip content={renderLineTooltip} />
{dataKeys.map((dataKey, index) => (
<Line
key={`${dataKey}-submitted`}
strokeWidth={2}
dataKey={`${dataKey}.submittedCount`}
stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
connectNulls
/>
))}
{dataKeys.map((dataKey, index) => (
<Line
key={`${dataKey}-delivered`}
strokeWidth={2}
dataKey={`${dataKey}.deliveredCount`}
stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
connectNulls
/>
))}
</LineChart>
However, preferably I’d like my code to look like this:
<LineChart data={data}>
<XAxis dataKey="timestamp" tickFormatter={tickFormatter} />
<Tooltip content={renderLineTooltip} />
{dataKeys.map((dataKey, index) => (
<Fragment>
<Line
key={`${dataKey}-submitted`}
strokeWidth={2}
dataKey={`${dataKey}.submittedCount`}
stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
connectNulls
/>
<Line
key={`${dataKey}-delivered`}
strokeWidth={2}
dataKey={`${dataKey}.deliveredCount`}
stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
connectNulls
/>
</Fragment>
))}
</LineChart>
This would prevent me from having to loop over the same array twice. 😃
Am I doing something wrong or is this kind of behavior not supported for some reason?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Displaying chart inside Fragment Activity in Android
Fragment activity contains the references of achartengine libraries and return a view.
Read more >How to use chart inside fragment in Android ... - Stack Overflow
Use the View that you inflate in onCreateView as a starting point to "find" your other views while you're still in onCreateView ,...
Read more >Creating and Using Fragments | CodePath Android Cliffnotes
Refer to this detailed lifecycle chart to view the lifecycle of a fragment more visually. Looking Up a Fragment Instance. Often we need...
Read more >Fragments and the Navigation Component - Android Developers
In this codelab, you'll learn about fragments, the fragment lifecycle, ... Graph to navigate between fragments in the same host activity.
Read more >Add sample how to add map inside another fragment ...
I want to add map inside fragment with custom layout. Currently i found two ways: 1. I can do it using ChildFragmentManager that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve got this issue as well. It looks like Recharts is using React v16.0. It has not yet upgraded to 16.2 or higher, so it doesn’t support the updated Fragments syntax. It does support the older Fragment syntax:
Just be sure to 1) separate each element with a comma, and 2) give each component a proper ‘key’,
Sorry for not handling your issue in time, please try the latest version. If the problem persists, please open a new issue according to the issue guide.