question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering Arbitrary Shape in Charts

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

API docs mention arbitrary shapes such as Polygon, which leads me to believe that I should be able to include these shapes as part of a chart. For example, something like this:

const data = [
  {x: 100, y: 200}, {x: 120, y: 100}, {x: 170, y: 300},
  {x: 140, y: 250}, {x: 150, y: 400}, {x: 110, y: 280},
];
const polygon = [
  {x: 45, y:100}, {x: 45, y:200}, {x: 90, y:200}, {x: 90, y:100},
];
return (
  <ScatterChart width={400} height={400} margin={{top: 20, right: 20, bottom: 20, left: 20}}>
    <XAxis type={'number'} dataKey={'x'} name='stature' unit='cm'/>
    <YAxis dataKey={'y'} name='weight' unit='kg'/>
    <CartesianGrid />
    <Scatter name='A school' data={data} fill='#8884d8'/>
    <Tooltip cursor={{strokeDasharray: '3 3'}}/>
    <Polygon data={polygon} fill='#8884d8'/>
  </ScatterChart>
);

However, it seems that there is only a list of children expected of any particular chart, and anything else is ignored. So, Polygon inside any chart is not rendered at all.

What is the expected behavior?

Shapes inside chart can be added and rendered as a layer within the same coordinate system. The above code should render something like this:

image

Which versions of Recharts, and which browser / OS are affected by this issue? Did this work in previous versions of Recharts?

1.0.0-alpha.6

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jetzhoucommented, Nov 11, 2017

@xile611 I’m aware of that, but I will have to put in actual svg coordinates, rather than data points on the same scale as my normal data series. I’ve got a decent implementation using ReferenceArea working already, I can open a PR if you are interested. 😃

4reactions
shlanguscommented, Mar 18, 2019

For those who are looking for a way to draw own components and shapes inside of the chart. As mentioned above charts check the type of children and render only allowed types which include valid svg elements. If you pass <polygon> it should be rendered, but <Polygon> should not because it is a Rechart’s component used internally(?). So every direct child of chart should be either one of allowed types (see docs for it) or svg element.

If you try to put your own component that renders svg you would wonder that it is not rendered. That’s because the type of component is that you write in JSX. I found two options to get around this.

  1. Use an allowed Rechart component’s name for custom components. This way your component also would implicitly get some props supposed to be passed to the original component. Also, there is some chart behavior depended on presens of particular components (e.g. Chart adds margin to place a Brush under the chart).
  2. Wrap your component in a valid svg element. Don’t forget about <foreignObject> that is valid svg element allows to place non-svg stuff inside.
Read more comments on GitHub >

github_iconTop Results From Across the Web

(PDF) Improving Shape Depiction under Arbitrary Rendering
Based on the observation that shading conveys shape information through intensity gradients, we present a new technique called Radiance ...
Read more >
Chart.js — drawing an arbitrary vertical line - Stack Overflow
You extend the line chart and include logic for drawing the line in the draw ... const context = chartInstance.chart.ctx; // render vertical...
Read more >
Unable to draw 2D closed shapes - Nevron Software
But I need to draw irregular shapes - with arbitrary outlines. ... Gallery \ Custom \ Custom Series - using this approach you...
Read more >
XYAreaRenderer - JFree
Area item renderer for an XYPlot . This class can draw (a) shapes at each point, or (b) lines between points, or (c)...
Read more >
Multi-chart geometry images - JHU CS
... we use an atlas construction to map the surface piecewise onto charts of arbitrary shape. ... rendering involves a simple scan of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found