Skip to content

Commit

Permalink
feat: add initial basic support for Victory
Browse files Browse the repository at this point in the history
Fixes #41
  • Loading branch information
EvHaus committed Aug 8, 2023
1 parent a7ea913 commit e5ff5f2
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 9 deletions.
Binary file modified bun.lockb
Binary file not shown.
Binary file removed examples/basic.pdf
Binary file not shown.
Binary file removed examples/composed.pdf
Binary file not shown.
5 changes: 3 additions & 2 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ console.error = function (message) {
};

const main = async () => {
await import('./basic');
await import('./composed');
await import('./recharts-basic');
await import('./recharts-composed');
await import('./victory-basic');

// rome-ignore lint/nursery/noConsoleLog: <explanation>
return console.log('✅ /examples updated!');
Expand Down
Binary file added examples/recharts-basic.pdf
Binary file not shown.
File renamed without changes
2 changes: 1 addition & 1 deletion examples/basic.tsx → examples/recharts-basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ const MyDocument = () => (

const __dirname = fileURLToPath(new URL('.', import.meta.url));

ReactPDF.render(<MyDocument />, `${__dirname}/basic.pdf`);
ReactPDF.render(<MyDocument />, `${__dirname}/recharts-basic.pdf`);
Binary file added examples/recharts-composed.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/composed.tsx → examples/recharts-composed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ const MyDocument = () => (

const __dirname = fileURLToPath(new URL('.', import.meta.url));

ReactPDF.render(<MyDocument />, `${__dirname}/composed.pdf`);
ReactPDF.render(<MyDocument />, `${__dirname}/recharts-composed.pdf`);
Binary file added examples/victory-basic.pdf
Binary file not shown.
35 changes: 35 additions & 0 deletions examples/victory-basic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import ReactPDFChart from '../src';
import { Document, Page } from '@react-pdf/renderer';
import ReactPDF from '@react-pdf/renderer';
import { fileURLToPath } from 'node:url';
import React from 'react';
import { VictoryBar, VictoryChart } from 'victory';

const data = [
{ x: 1, y: 2, y0: 1 },
{ x: 2, y: 3, y0: 2 },
{ x: 3, y: 5, y0: 2 },
{ x: 4, y: 4, y0: 3 },
{ x: 5, y: 6, y0: 3 },
];

const MyDocument = () => (
<Document>
<Page size='A4' style={{ padding: 20 }}>
<ReactPDFChart>
<VictoryChart>
<VictoryBar
data={data}
height={100}
style={{ data: { fill: '#c43a31' } }}
width={100}
/>
</VictoryChart>
</ReactPDFChart>
</Page>
</Document>
);

const __dirname = fileURLToPath(new URL('.', import.meta.url));

ReactPDF.render(<MyDocument />, `${__dirname}/victory-basic.pdf`);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"recharts": "2.7.2",
"rome": "12.1.3",
"semantic-release": "21.0.7",
"typescript": "5.1.6"
"typescript": "5.1.6",
"victory": "36.6.11"
},
"engines": {
"node": ">=16"
Expand Down
68 changes: 64 additions & 4 deletions src/ReactPDFChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,56 @@ const getElementStyle = (
// Apply inline styles that react-pdf supports
if (attribs.style) {
attribs.style.split(';').forEach((styleString) => {
const [key, value] = styleString.split(':');
const [rawKey, value] = styleString.split(':');
const key = rawKey.toLowerCase();

if (['backgroundColor', 'color'].includes(key)) {
style.push({ [key]: value });
} else {
// This warning is super noisy, but can be helpful when debugging
// console.warn(
// `<ReactPDFChart /> detected that your chart has a node with an unsupported inline style. "${attribs.style}" mentions "${key}" which isn't supported in react-pdf yet.`,
// );
}
});
}
return style;
};

// For SVG elements this will process inline styles into something react-pdf
// can understand
const getSvgElementStyle = (attribs: TagElementType['attribs']) => {
const style: SVGPresentationAttributes = {};

// Apply inline styles that react-pdf supports
if (attribs.style) {
attribs.style.split(';').forEach((styleString) => {
const [rawKey, value] = styleString.split(':');
const key = rawKey.toLowerCase();

switch (key) {
case 'color':
case 'fill':
case 'opacity':
case 'stroke':
style[key] = value;
break;
case 'stroke-width':
style.strokeWidth = value;
break;
case 'stroke-linecap':
style.strokeLineCap =
value as SVGPresentationAttributes['strokeLineCap'];
break;
default:
// This warning is super noisy, but can be helpful when debugging
// console.warn(
// `<ReactPDFChart /> detected that your chart has a node with an unsupported inline style. "${attribs.style}" mentions "${key}" which isn't supported in react-pdf yet.`,
// );
}
});
}

return style;
};

Expand Down Expand Up @@ -231,6 +275,7 @@ const webSvgToPdfSvg = (children: React.ReactElement, chartStyle?: Style) => {
cx={attribs.cx}
cy={attribs.cy}
r={attribs.r}
style={getSvgElementStyle(attribs)}
>
{children}
</Circle>
Expand All @@ -252,6 +297,7 @@ const webSvgToPdfSvg = (children: React.ReactElement, chartStyle?: Style) => {
cy={attribs.cy}
rx={attribs.rx}
ry={attribs.ry}
style={getSvgElementStyle(attribs)}
>
{children}
</Ellipse>
Expand All @@ -266,6 +312,7 @@ const webSvgToPdfSvg = (children: React.ReactElement, chartStyle?: Style) => {
x2={attribs.x2}
y1={attribs.y1}
y2={attribs.y2}
style={getSvgElementStyle(attribs)}
>
{children}
</Line>
Expand All @@ -291,19 +338,31 @@ const webSvgToPdfSvg = (children: React.ReactElement, chartStyle?: Style) => {
);
case 'path':
return (
<Path {...baseProps} d={attribs.d}>
<Path
{...baseProps}
d={attribs.d}
style={getSvgElementStyle(attribs)}
>
{children}
</Path>
);
case 'polygon':
return (
<Polygon {...baseProps} points={attribs.points}>
<Polygon
{...baseProps}
points={attribs.points}
style={getSvgElementStyle(attribs)}
>
{children}
</Polygon>
);
case 'polyline':
return (
<Polyline {...baseProps} points={attribs.points}>
<Polyline
{...baseProps}
points={attribs.points}
style={getSvgElementStyle(attribs)}
>
{children}
</Polyline>
);
Expand All @@ -328,6 +387,7 @@ const webSvgToPdfSvg = (children: React.ReactElement, chartStyle?: Style) => {
height={attribs.height}
rx={attribs.rx}
ry={attribs.ry}
style={getSvgElementStyle(attribs)}
width={attribs.width}
x={attribs.x}
y={attribs.y}
Expand Down

0 comments on commit e5ff5f2

Please sign in to comment.