简体   繁体   English

将Google Chart导出到Angular中的Excel工作表

[英]Export Google Chart to Excel Sheet in Angular

Im using ng-google-chart to create charts from data I receive from a database. 我使用ng-google-chart从我从数据库收到的数据中创建图表。 I store the data in a table. 我将数据存储在一个表中。 I need to export both the table and the chart. 我需要导出表格和图表。

I'm using the following technique to export tables (where "exportable" is the div the contains the table): 我正在使用以下技术来导出表(其中“可导出”是包含表的div):

 $scope.export = function () 
{

 var blob = new Blob([document.getElementById('exportable').innerHTML], {
                type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
            });
            saveAs(blob, "Record.xls");


            alert("export done"); 
        };

I cannot find any way to add the chart to this file. 我找不到将图表添加到此文件的任何方法。

This is the code to generate a chart 这是生成图表的代码

var chart1 = {};
                    chart1.type = "ColumnChart";
                    chart1.cssStyle = "height:400px; width:500px;";
                    chart1.data = {
                        "cols": [
                            { id: "gender", label: "Gender", type: "string" },
                            { id: "number", label: "number", type: "number" }

                        ], "rows": [
                            {
                                c: [
                                   { v: "male" },
                                   { v: $scope.male, f: $scope.male }

                                ]
                            },

                            {
                                c: [
                                   { v: "female" },
                                   { v: $scope.female }

                                ]
                            }
                        ]
                    };

                    chart1.options = {
                        "title": "",
                        "isStacked": "true",
                        "fill": 20,
                        "displayExactValues": true,
                        "vAxis": {
                            "title": "Number", "gridlines": { "count": 6 }
                        },
                        "hAxis": {
                            "title": "gender"
                        }
                    };

                    chart1.formatters = {};

                    $scope.chart = chart1;


                }

To getImageURI of the chart, wait for the ready event and call the function. 要获取图表的ImageImageURI ,请等待ready事件并调用该函数。
Then you can add the image somewhere on the page. 然后,您可以将图像添加到页面上的某处。
You can even hide the original chart if needed... 您甚至可以根据需要隐藏原始图表...
Following is an example of loading the image URI into another element. 以下是将图像URI加载到另一个元素中的示例。

 google.load('visualization', '1', {packages:['corechart'], callback: drawChart}); function drawChart() { var data = google.visualization.arrayToDataTable([ ["Element", "Density", { role: "style" } ], ["Copper", 8.94, "#b87333"], ["Silver", 10.49, "silver"], ["Gold", 19.30, "gold"], ["Platinum", 21.45, "color: #e5e4e2"] ]); var view = new google.visualization.DataView(data); view.setColumns([0, 1, { calc: "stringify", sourceColumn: 1, type: "string", role: "annotation" }, 2]); var options = { title: "Density of Precious Metals, in g/cm^3", width: 600, height: 400, bar: {groupWidth: "95%"}, legend: { position: "none" }, }; var chart = new google.visualization.ColumnChart(document.getElementById("chart_div")); google.visualization.events.addListener(chart, 'ready', function () { document.getElementById("chart_image").insertAdjacentHTML('beforeEnd', '<img alt="Chart Image" src="' + chart.getImageURI() + '">'); }); chart.draw(view, options); } 
 <script src="https://www.google.com/jsapi"></script> <span>CHART</span> <div id="chart_div"></div> <br/> <span>IMAGE</span> <div id="chart_image"></div> 

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

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