简体   繁体   English

如何将JavaScript图表导出到Excel文件(HighCharts)

[英]How to Export JavaScript Chart to Excel file (HighCharts)

I have to Export a Javascript chart (HighCharts) into an excel file; 我必须将Javascript图表(HighCharts)导出到excel文件中; the chart was rendered in a div, but the excel doesn't render the html+css content the javascript generates, render only the text without style. 图表是在div中呈现的,但是excel不会渲染javascript生成的html + css内容,只渲染没有样式的文本。

A solution would be convert this where the chart was rendered into an image(jpeg) but I have no success... 一个解决方案是将图表呈现为图像(jpeg)的转换,但我没有成功...

Thanks ! 谢谢 !

HighCharts already supports the image exporting functionality via the Exporting Module which is packaged with it. HighCharts已经通过与其一起打包的导出模块支持图像导出功能。 Exporting After getting it you should be able to modify the script to save the image in any way you need. 导出获取后,您应该能够修改脚本以任何方式保存图像。 It's certainly not a beginners task and will require lots of tinkering. 这肯定不是初学者的任务,需要大量的修修补补。

If it were me I would modify the code that responds to the export button so that I can activate it with JavaScript and also pass in information so that the PHP file on the back-end could save the picture in the manner you want rather than returning it to the client. 如果是我,我会修改响应导出按钮的代码,以便我可以使用JavaScript激活它并传入信息,以便后端的PHP文件可以按照您想要的方式保存图片而不是返回它给客户。

After some searching, I found this recent answer in their forums , with a jsfiddle to tinker with. 经过一番搜索后,我在他们的论坛中找到了这个最近的答案 ,并带着一个jsfiddle来修补。

It describes how to export a CSV using a script on your server, which from past experience, is the only way this can get done because the HighChart graph itself doesn't contain nearly enough information to produce a usable spreadsheet. 它描述了如何使用服务器上的脚本导出CSV,从过去的经验来看,这是完成此操作的唯一方法,因为HighChart图本身不包含足够的信息来生成可用的电子表格。 We already do with a different charting library, and use phpExcel to actually create the spreadsheet. 我们已经使用了不同的图表库,并使用phpExcel实际创建电子表格。

If you are willing to try an Add-in, there is a way to use Javascript, HTML and css in Excel. 如果您愿意尝试加载项,可以使用Excel中的Javascript,HTML和css。 It's called Funfun and it hosts an online editor with an embedded spreadsheet so the transition isn't hard between the website to Excel. 它被称为Funfun,它拥有一个带有嵌入式电子表格的在线编辑器,因此网站与Excel之间的转换并不难。

Here is a chart I made with Highcharts: 这是我用Highcharts制作的图表:

https://www.funfun.io/1/#/edit/5a61c190404f66229bda3f0f https://www.funfun.io/1/#/edit/5a61c190404f66229bda3f0f

In this example I took the chart from a Highchart demo , and replaced the data with mine. 在这个例子中,我从Highchart演示中获取了图表,并用我的数据替换了数据。 I store my data in the embedded spreadsheet, and thanks to a json file I can use it in my javascript code. 我将我的数据存储在嵌入式电子表格中,并且由于json文件,我可以在我的javascript代码中使用它。

That is how I get my data from the spreadsheet with the json file: 这就是我使用json文件从电子表格中获取数据的方法:

{
    "data": "=A1:E16"
}

I store it in my script.js with the right format so I can directly load it in Highcharts (for numbers you must convert your data into floats or int): 我将它存储在我的script.js中,格式正确,因此我可以直接将其加载到Highcharts中(对于数字,您必须将数据转换为浮点数或int):

var data = [];

for (var i = 1; i < $internal.data.length; i++)
  data.push(
    {
      x: parseFloat($internal.data[i][2]),
      y: parseFloat($internal.data[i][3]),
      z: parseFloat($internal.data[i][4]),
      name: $internal.data[i][1],
      country: $internal.data[i][0]
    }
  );

After You've chosen all of you're options for your chart you can add your data: 在为图表选择所有选项后,您可以添加数据:

series: [{
        data: data
    }]

Once you are happy with your chart you can directly load it into Excel by pasting the URL in the Funfun add-in . 一旦您对图表感到满意,您可以通过粘贴Funfun加载项中的URL将其直接加载到Excel Here is how it looks like with my example: 以下是我的示例:

最后

Of course you can use another library than Highcharts, there are a lot of powerful libraries for data visualization like charts.js and D3.js. 当然你可以使用另一个库而不是Highcharts,有很多强大的数据库可视化库,如charts.js和D3.js.

I know this is an old post but I hope it helps people with the same problem. 我知道这是一个老帖子,但我希望它可以帮助有同样问题的人。

Disclosure : I'm a developer of Funfun. 披露:我是Funfun的开发者。

This might be a little late, but I stumbled across this via Google so it might help someone. 这可能有点晚了,但我偶然发现了谷歌,所以它可能对某人有所帮助。 Highchart's image is in SVG format: http://en.wikipedia.org/wiki/Svg , you need to convert that to a bitmap format image. Highchart的图像采用SVG格式: http//en.wikipedia.org/wiki/Svg ,您需要将其转换为位图格式图像。 You have to change the Highcharts exporting options URL to your own URL: 您必须将Highcharts导出选项URL更改为您自己的URL:

exporting : {
  url: 'http://mydomain.com/export.php'
}

In your export script you must convert the SVG image to a bitmap image (I used PHP & imagick) then export to whatever suits your needs, save the bitmap image to the server, send by e-mail, etc. , 在导出脚本中,您必须将SVG图像转换为位图图像(我使用PHP和imagick)然后导出到适合您需要的任何内容,将位图图像保存到服务器,通过电子邮件发送等,

I know it's too late but i have the same problem and this jsfiddle helped me to create excel from highchart. 我知道现在为时已晚,但我有同样的问题,这个jsfiddle帮助我从highchart创建excel。 The Download CSV option added to export menu works fine. 添加到导出菜单的下载CSV选项工作正常。 Here is the code: 这是代码:

/**
 * A small plugin for getting the CSV of a categorized chart
 */
(function (Highcharts) {

    // Options
    var itemDelimiter = ',',  // use ';' for direct import to Excel
        lineDelimiter = '\n';

    var each = Highcharts.each;
    Highcharts.Chart.prototype.getCSV = function () {
        var xAxis = this.xAxis[0],
            columns = [],
            line,
            csv = "", 
            row,
            col;

        if (xAxis.categories) {
            columns.push(xAxis.categories);
            columns[0].unshift("");
        }
        each (this.series, function (series) {
            columns.push(series.yData);
            columns[columns.length - 1].unshift(series.name);
        });

        // Transform the columns to CSV
        for (row = 0; row < columns[0].length; row++) {
            line = [];
            for (col = 0; col < columns.length; col++) {
                line.push(columns[col][row]);
            }
            csv += line.join(itemDelimiter) + lineDelimiter;
        }
        return csv;
    };    
}(Highcharts));

// Now we want to add "Download CSV" to the exporting menu. We post the CSV
// to a simple PHP script that returns it with a content-type header as a 
// downloadable file.
// The source code for the PHP script can be viewed at 
// https://raw.github.com/highslide-software/highcharts.com/master/studies/csv-export/csv.php

Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
    text: 'Download CSV',
    onclick: function () {
        Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', {
            csv: this.getCSV()
        });
    }
});



var chart = new Highcharts.Chart({

    chart: {
        renderTo: 'container'
    },

    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]

});

$('#getcsv').click(function () {
    alert(chart.getCSV());
});

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

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