如何通过弹出另存为窗口将kendoui dataviz图表导出为(.png)或(.jpg)图像格式?

发布于 2025-01-03 16:49:08 字数 3184 浏览 3 评论 0原文

我正在使用 kendoui dataviz 图表,我需要将这些图表导出为 (.png)(.jpg) 图像格式。 基本上 kendoui dataviz 图表有一个名为“svg()”的内置方法。

'svg()' 返回当前图表的 SVG 表示形式。返回的字符串是一个独立的 SVG 文档。

例子

var chart = $("#chart").data("kendoChart");
var svgText = chart.svg();
Now svgText contains details of chart image..can anybody tell me how to convert these data to actual image format and pop up a Save As prompt ???

代码示例:我尝试了这个,但它没有提示任何“另存为”弹出窗口

     <div id="example" class="k-content">
              <div class="chart-wrapper">
                  <div id="chart"></div>
                     <center>
                        <div>
                          <input type="button" value="click" onclick="disp();" />
                        </div>
                     </center>
                  <div>
      <canvas id="canvas"></canvas>
      </div>
        </div>
           </div>


          <script type="text/javascript">

            function disp() {
                var chart = $("#chart").data("kendoChart");
                var svgText = chart.svg();
                var c = document.getElementById('canvas');
                canvg(c,svgText);
                var img    = c.toDataURL("image/png");
                document.write('<img src="' + img + '"/>');
                window.win = open(imgOrURL);
                setTimeout('win.document.execCommand("SaveAs")', 100);
                }

              function createChart() {
                $("#chart").kendoChart({
                    theme: $(document).data("kendoSkin") || "default",
                    title: {
                        text: "Internet Users"
                    },
                    legend: {
                        position: "bottom"
                    },
                    chartArea: {
                        background: ""
                    },
                    seriesDefaults: {
                        type: "bar"
                    },
                    series: [{
                        name: "World",
                        data: [15.7, 16.7, 20, 23.5, 26.6]
                    }, {
                        name: "United States",
                        data: [67.96, 68.93, 75, 74, 78]
                    }],
                    valueAxis: {
                        labels: {
                            format: "{0}%"
                        }
                    },
                    categoryAxis: {
                        categories: [2005, 2006, 2007, 2008, 2009]
                    },
                    tooltip: {
                        visible: true,
                        format: "{0}%"
                    }
                });
            }

            $(document).ready(function () {
                setTimeout(function () {
                    createChart();

                },100);


                $(document).bind("kendo:skinChange", function (e) {
                    createChart();
                  });
             });
    <script>                     

I am using kendoui dataviz charts, and I need to export those charts into (.png) or (.jpg) image format.
Basically kendoui dataviz chart has a built-in method called 'svg()'.

'svg()' Returns the SVG representation of the current chart. The returned string is a self-contained SVG document.

Example

var chart = $("#chart").data("kendoChart");
var svgText = chart.svg();

Now svgText contains details of chart image..can anybody tell me how to convert these data to actual image format and pop up a Save As prompt ???

code example: I tried this, but it doesn't prompt any 'SaveAs' popup

     <div id="example" class="k-content">
              <div class="chart-wrapper">
                  <div id="chart"></div>
                     <center>
                        <div>
                          <input type="button" value="click" onclick="disp();" />
                        </div>
                     </center>
                  <div>
      <canvas id="canvas"></canvas>
      </div>
        </div>
           </div>


          <script type="text/javascript">

            function disp() {
                var chart = $("#chart").data("kendoChart");
                var svgText = chart.svg();
                var c = document.getElementById('canvas');
                canvg(c,svgText);
                var img    = c.toDataURL("image/png");
                document.write('<img src="' + img + '"/>');
                window.win = open(imgOrURL);
                setTimeout('win.document.execCommand("SaveAs")', 100);
                }

              function createChart() {
                $("#chart").kendoChart({
                    theme: $(document).data("kendoSkin") || "default",
                    title: {
                        text: "Internet Users"
                    },
                    legend: {
                        position: "bottom"
                    },
                    chartArea: {
                        background: ""
                    },
                    seriesDefaults: {
                        type: "bar"
                    },
                    series: [{
                        name: "World",
                        data: [15.7, 16.7, 20, 23.5, 26.6]
                    }, {
                        name: "United States",
                        data: [67.96, 68.93, 75, 74, 78]
                    }],
                    valueAxis: {
                        labels: {
                            format: "{0}%"
                        }
                    },
                    categoryAxis: {
                        categories: [2005, 2006, 2007, 2008, 2009]
                    },
                    tooltip: {
                        visible: true,
                        format: "{0}%"
                    }
                });
            }

            $(document).ready(function () {
                setTimeout(function () {
                    createChart();

                },100);


                $(document).bind("kendo:skinChange", function (e) {
                    createChart();
                  });
             });
    <script>                     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半枫 2025-01-10 16:49:08

更新 2

图表现在包含各种导出选项 - PNG、SVG 和矢量 PDF。请参阅导出演示以供参考。

更新

图表现在具有用于获取导出图像(base64 编码)的内置方法:

var img = chart.imageDataURL();

我不知道显示“另存为”对话框的跨浏览器方式。

另请参阅:API 参考

原始响应如下:

无法在浏览器中导出图表图像。我们准备了一个演示,展示如何使用 Inkscape 在服务器上将 SVG 文档转换为 PNG/PDF。

该演示应用程序是在 ASP.NET MVC 中实现的,但不依赖于其任何功能,并且可以移植到其他框架。

您可以在 GitHub 上找到该演示:

https: //github.com/telerik/kendo-examples-asp-net/tree/master/chart-inkscape-export

UPDATE 2

The Chart now includes various export options - PNG, SVG and a vector PDF. See the Export demo for reference.

UPDATE

The Chart now has built-in method for obtaining the exported image (base64 encoded):

var img = chart.imageDataURL();

I'm not aware of a cross-browser way to display a "Save As" dialog.

See also: API Reference

Original response follows:

Exporting the Chart image in-browser is not possible. We have prepared a demo that shows how to convert the SVG document to PNG/PDF on the server using Inkscape.

The demo application is implemented in ASP.NET MVC, but does not depend on any of its features and can be ported to other frameworks.

You can find the demo on GitHub:

https://github.com/telerik/kendo-examples-asp-net/tree/master/chart-inkscape-export

蓝咒 2025-01-10 16:49:08

你可以这样做。
对于这种方法,您需要 svg.dll
您可以从此链接下载。

Javascript:

var chart = $("#chart").data("kendoChart");
var svgString = escape(chart.svg());
    $.ajax({
        url: "/MyController/Sample",
        data: { svg: svgString },
        async: false,
        type: 'Post',
        success: function (data) {
            window.location = "/MyController/getPdf";
        }
    });

控制器:

    [HttpPost]
    [ValidateInput(false)]
    public void Sample(string svg)
    {
        var svgText = System.Web.HttpUtility.UrlDecode(svg);
        Session["chrtData"] = svgText;
    }

    public void getPdf()
    {
        string svgText = Session["chrtData"].ToString();

        var byteArray = Encoding.ASCII.GetBytes(svgText);
        using (var stream = new MemoryStream(byteArray))
        {
            var svgDocument = Svg.SvgDocument.Open(stream);
            //First Way

            var bitmap = svgDocument.Draw();
            MemoryStream docMemStream = new MemoryStream();
            bitmap.Save("D:\\hi.png", System.Drawing.Imaging.ImageFormat.Png);

        }
     }

U can do like this.
For this approach u need svg.dll
U can download from this link.

Javascript:

var chart = $("#chart").data("kendoChart");
var svgString = escape(chart.svg());
    $.ajax({
        url: "/MyController/Sample",
        data: { svg: svgString },
        async: false,
        type: 'Post',
        success: function (data) {
            window.location = "/MyController/getPdf";
        }
    });

Controller:

    [HttpPost]
    [ValidateInput(false)]
    public void Sample(string svg)
    {
        var svgText = System.Web.HttpUtility.UrlDecode(svg);
        Session["chrtData"] = svgText;
    }

    public void getPdf()
    {
        string svgText = Session["chrtData"].ToString();

        var byteArray = Encoding.ASCII.GetBytes(svgText);
        using (var stream = new MemoryStream(byteArray))
        {
            var svgDocument = Svg.SvgDocument.Open(stream);
            //First Way

            var bitmap = svgDocument.Draw();
            MemoryStream docMemStream = new MemoryStream();
            bitmap.Save("D:\\hi.png", System.Drawing.Imaging.ImageFormat.Png);

        }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文