简体   繁体   English

jchartfx将图表导出到图像

[英]jchartfx exporting a chart to image

I want to export my chart to an image which i have created using http://www.jchartfx.com . 我想将图表导出到使用http://www.jchartfx.com创建的图像。 I came across the export function in its documentation from the link - http://www.jchartfx.com/api/Chart/Export but the example as 我通过链接-http: //www.jchartfx.com/api/Chart/Export在其文档中遇到了导出功能,但示例如下:

chart1.export, "\\temp\\image.bmp"));

looks to be incorrect . 看起来是错误的。 Can anyone please help me with this . 谁能帮我这个忙。 How can i export a chart to an image using this export function . 如何使用此导出功能将图表导出到图像。 Thank you 谢谢

According to JChartFX guys it is not possible easily export charts using export function - 据JChartFX专家介绍,使用导出功能无法轻松导出图表-

"We are considering it, unfortunately when doing charts in HTML5 we chose SVG over Canvas. One of the things that would be very easy to do in Canvas would be to export the chart as an image. It is not as simple (AFAIK) when using SVG so we are trying to figure out how to support this in the future." “不幸的是,我们正在考虑使用HTML5绘制图表时,我们选择了SVG而不是Canvas。在Canvas中非常容易做的一件事情就是将图表导出为图像。使用SVG,因此我们正在尝试找出将来如何支持它。”

However there is some code posted on their forums that allows to export charts. 但是,在他们的论坛上发布了一些代码,可以导出图表。 However, it is not simple, or clean way - http://community.jchartfx.com/forums/t/103.aspx 但是,这不是简单或干净的方法-http://community.jchartfx.com/forums/t/103.aspx

Canvas has the ability to draw an image using SVG as source. Canvas可以使用SVG作为源绘制图像。 Using that feature and a modern browser, you can export any jChartFX control to an image. 使用该功能和现代的浏览器,您可以将任何jChartFX控件导出到图像。 This is supported with or without using css. 无论是否使用css,都支持此功能。 A jsfiddle at http://jsfiddle.net/h9DfR/ shows this functionality. http://jsfiddle.net/h9DfR/上的jsfiddle显示了此功能。

    $(document).ready(function(){
        var chart = new cfx.Chart();
        // Configure the chart here
        chart.setGallery(cfx.Gallery.Bar);
        //
        chart.create("currentChart");       
    });

    $("#save").on("click", function () {
        // Obtain the chart's div  tag
        var html1 = $("svg.jchartfx").parent().html();
        // Filter the SVG tag only
        var html = html1.substring(html1.indexOf("<svg"));
        // Since CSS is used, a reference to the external css must be added to the SVG. This is not needed if CSS is not used
        html = "<?xml-stylesheet href=\"http://www.jchartfx.com/libs/v7/current/styles/Attributes/jchartfx.attributes.css\" type=\"text/css\"?>" + html;
        html = "<?xml-stylesheet href=\"http://www.jchartfx.com/libs/v7/current/styles/Palettes/jchartfx.palette.css\" type=\"text/css\"?>" + html;

        var canvas = document.querySelector("canvas");
        context = canvas.getContext("2d");
        var imgsrc = 'data:image/svg+xml;base64,' + btoa(html);
        var image = new Image;
        image.src = imgsrc;
        // This function creates the PNG and saves it to disk
        image.onload = function() {
            context.drawImage(image, 0, 0); 
            var canvasdata = canvas.toDataURL("image/png"); 
            var a = document.createElement("a");
            a.download = "sample.png";
            a.href = canvasdata;
            a.click();
        };
    });         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM