JSPM

  • Created
  • Published
  • Downloads 16773
  • Score
    100M100P100Q140456F
  • 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.

Example

See an example of using recharts-to-png with FileSaver.

Install

npm i -S recharts-to-png

Usage

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);
    // 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;