JSPM

  • Created
  • Published
  • Downloads 16101
  • Score
    100M100P100Q143144F
  • License MIT

This package converts a Recharts chart to a png.

Package Exports

  • recharts-to-png

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (recharts-to-png) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm GitHub

recharts-to-png

Converts a Recharts chart to PNG.

Inspired by these Stack Overflow questions and answers from peter.bartos and AlbertMunichMar.

Demo

See a demo using recharts-to-png alongside FileSaver.

Install

npm install recharts-to-png

Usage

await getPngData(chart, fill);

chart: RechartsChart - AreaChart | BarChart | PieChart | etc.

fill: background color (optional) - string | CanvasGradient | CanvasPattern

Example

import { getPngData } from "recharts-to-png";

...

export const App = () => {
  // The chart that we want to download the PNG for.
  const [chart, setChart] = React.useState();

  const handleDownload = React.useCallback(async () => {
    // Send the chart to getPngData
    const pngData = await getPngData(chart);
    // Can also pass an optional fill parameter for the background color
    // const pngData = await getPngData(chart, '#000000');
    // Use FileSaver to download the PNG
    FileSaver.saveAs(pngData, "test.png");
  }, [chart]);

  const data = [...];

  return (
    <LineChart
      ref={ref => setChart(ref)} // Save the ref of the chart
      data={data}
      height={300}
      width={600}
      margin={{ top: 5, right: 30, left: 20, bottom: 25 }}
    >
      <XAxis dataKey="name" />
      <YAxis />
      <CartesianGrid strokeDasharray="3 3" />
      <Tooltip />
      <Legend />
      <Line
        type="monotone"
        dataKey="pv"
        stroke="#8884d8"
        activeDot={{ r: 8 }}
      />
      <Line type="monotone" dataKey="uv" stroke="#82ca9d" />
    </LineChart>
  );
};

export default App;