将组件重新映射到PNG

时间:2017-07-13 16:08:54

标签: javascript reactjs canvas recharts

我目前有一个Recharts组件,我想将其导出为PNG文件。

                    <LineChart
                        id="currentChart"
                        ref={(chart) => this.currentChart = chart}
                        width={this.state.width}
                        height={this.state.height}
                        data={this.testData}
                        margin={{top: 5, right: 30, left: 20, bottom: 5}}
                    >
                        <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>

但我不确定图书馆是否直接支持。

我有一个想法,包括使用画布和2D渲染上下文让我接近解决方案,如MDN所述

但是,我不确定将HTML元素(或React Component)呈现为画布以实现此解决方案的通用方法。

我可能会发现这一切都错了,我会很感激纠正!

5 个答案:

答案 0 :(得分:4)

我能够通过深入研究Recharts组件来解决我的问题。 Recharts在包装器下呈现为SVG,所以我所要做的就是正确转换为另存为HTML或SVG

// Exports the graph as embedded JS or PNG
exportChart(asSVG) {

    // A Recharts component is rendered as a div that contains namely an SVG
    // which holds the chart. We can access this SVG by calling upon the first child/
    let chartSVG = ReactDOM.findDOMNode(this.currentChart).children[0];

    if (asSVG) {
        let svgURL = new XMLSerializer().serializeToString(chartSVG);
        let svgBlob = new Blob([svgURL], {type: "image/svg+xml;charset=utf-8"});
        FileSaver.saveAs(svgBlob, this.state.uuid + ".svg");
    } else {
        let svgBlob = new Blob([chartSVG.outerHTML], {type: "text/html;charset=utf-8"});
        FileSaver.saveAs(svgBlob, this.state.uuid + ".html");
    }
}

我使用FileSaver.js作为保存提示。

答案 1 :(得分:2)

书面答复对我帮助很大。非常感谢。尽管如此,我还是缺少一个“开箱即用”的 png 下载解决方案,我想在这里弥补。即使为时已晚,也许它会帮助其他人。

handleExportChart = () => {

        let chartSVG = ReactDOM.findDOMNode(this.currentChart).children[0];
        const width = chartSVG.clientWidth;
        const height = chartSVG.clientHeight;
        let svgURL = new XMLSerializer().serializeToString(chartSVG);
        let svgBlob = new Blob([svgURL], { type: "image/svg+xml;charset=utf-8" });
        let URL = window.URL || window.webkitURL || window;
        let blobURL = URL.createObjectURL(svgBlob);

        let image = new Image();
        image.onload = () => {
            let canvas = document.createElement('canvas');
            canvas.width = width;
            canvas.height = height;
            let context = canvas.getContext('2d');
            context.drawImage(image, 0, 0, context.canvas.width, context.canvas.height);
            let png = canvas.toDataURL('image/png', 1.0);
            FileSaver.saveAs(png, "Test.png");
        };

        image.src = blobURL;
    };

答案 2 :(得分:1)

@brammitch为此创建了一个程序包(受此处答案的启发):

https://github.com/brammitch/recharts-to-png

答案 3 :(得分:0)

此函数将SVG元素作为输入并转换为image/png数据:

export const svgToPng = (svg, width, height) => {

    return new Promise((resolve, reject) => {

        let canvas = document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;
        let ctx = canvas.getContext('2d');

        // Set background to white
        ctx.fillStyle = '#ffffff';
        ctx.fillRect(0, 0, width, height);

        let xml = new XMLSerializer().serializeToString(svg);
        let dataUrl = 'data:image/svg+xml;utf8,' + encodeURIComponent(xml);
        let img = new Image(width, height);

        img.onload = () => {
            ctx.drawImage(img, 0, 0);
            let imageData = canvas.toDataURL('image/png', 1.0);
            resolve(imageData)
        }

        img.onerror = () => reject();

        img.src = dataUrl;
    });
};

以及如何访问Recharts SVG元素?此代码段可让您在当前可见的DOM之外呈现任何图表,并使用其SVG:

const exportChart = () => {

    // Output image size
    const WIDTH = 900;
    const HEIGHT = 250;

    const convertChart = async (ref) => {

        if (ref && ref.container) {
            let svg = ref.container.children[0];
            let pngData = await svgToPng(svg, WIDTH, HEIGHT);
            console.log('Do what you need with PNG', pngData);
        }
    };

    const chart = <LineChart data={...} width={WIDTH} height={HEIGHT}
        ref={ref => convertChart(ref)} />;

    // Render chart component into helper div
    const helperDiv = document.createElement('tmp');
    ReactDOM.render(chart, helperDiv);
}

答案 4 :(得分:0)

这是一个旧帖子,但这可能对某人有帮助

let pngData = await getPngData(this.ref);
FileSaver.saveAs(pngData, filename);

使用 recharts-to-png 和 file-saver npm 模块