Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

svg element can't by styled with chartStyle Prop #263

Open
3 tasks done
olehgowombat opened this issue Feb 25, 2024 · 3 comments
Open
3 tasks done

svg element can't by styled with chartStyle Prop #263

olehgowombat opened this issue Feb 25, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@olehgowombat
Copy link

olehgowombat commented Feb 25, 2024

Describe the bug

I'm using recharts BarChart and its svg element has .recharts-surface class, which I need to style through chartStyle prop. I looked into source code and it seems like svg tag doesn't consume getElementStyle(attribs, chartStyle, additionalStyle) function, which maps recharts classNames to a stylesheet.

Is there any workaround to that for now (because it kind of urgent) or it can be only resolved in the new version? (in case my assumption is correct)

Thanks in advance!

Reproduction

Try to apply any stylesheet to the ReactPDFChart that wraps BarChart (which has recharts-surface as className for svg and recharts-text for text and see that text one has worked and svg has not)

import { Document, PDFViewer, Page, StyleSheet } from '@react-pdf/renderer';
import ReactPDFChart from 'react-pdf-charts';
import { Bar, BarChart, CartesianGrid, XAxis, YAxis } from 'recharts';

const data = [
  { name: 'A', uv: 4000, pv: 2400, amt: 2400 },
  { name: 'B', uv: 3000, pv: 1398, amt: 2210 },
  { name: 'C', uv: 2000, pv: 9800, amt: 2290 },
  { name: 'D', uv: 2780, pv: 3908, amt: 2000 },
  { name: 'E', uv: 1890, pv: 4800, amt: 2181 },
  { name: 'F', uv: 2390, pv: 3800, amt: 2500 },
  { name: 'G', uv: 3490, pv: 4300, amt: 2100 },
];

const styles = StyleSheet.create({
  'recharts-surface': {
    marginTop: '300px',
    overflow: 'visible',
  },
  'recharts-text': {
    fontSize: '5px',
  },
});

const MyDocument = () => (
  <Document>
    <Page size="A4">
      <ReactPDFChart chartStyle={styles}>
        <BarChart data={data} width={500} height={300}>
          <XAxis dataKey="name" />
          <YAxis />
          <CartesianGrid stroke="#ccc" strokeDasharray="3 3" />
          <Bar dataKey="uv" fill="#8884d8" isAnimationActive={false} />
          <Bar dataKey="pv" fill="#82ca9d" isAnimationActive={false} />
        </BarChart>
      </ReactPDFChart>
    </Page>
  </Document>
);

const App = () => (
  <div>
    <h1>React PDF Sample</h1>
    <PDFViewer height="600" width="800">
      <MyDocument />
    </PDFViewer>
  </div>
);

export default App;

Validations

@olehgowombat olehgowombat added the bug Something isn't working label Feb 25, 2024
@EvHaus
Copy link
Owner

EvHaus commented Feb 27, 2024

Thanks for the bug report. This has been fixed. Can you try again in v0.2.5?

@EvHaus EvHaus closed this as completed Feb 27, 2024
@olehgowombat
Copy link
Author

olehgowombat commented Feb 27, 2024

Yes, thanks, now it works for <svg>!

Btw I think that it make sense to apply this fix to all svg elements (such as <g>, <line>, etc. ), because they usually have some className, that could be mapped and customised.

@EvHaus
Copy link
Owner

EvHaus commented Mar 1, 2024

Sure. I'll do a pass on some of the other elements and see what's missing.

@EvHaus EvHaus reopened this Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants