如何在Excel中导出图表?

我试图将从数据库中获取的数据显示为条形图,然后将数据和图表图像一起导出到excel中。我可以成功显示条形图,但无法将数据与图表一起导出到excel中。

单击按钮是否可以将数据和图表导出到Excel?

这是我的JavaScript代码

var fetcheddata = '';
var proddata = '';
$( document ).ready(function (){
    $('#chart-container-1').hide();
     $('#chart-container').show();
     $.ajax({
        url:"index.php?r=jobready/get-data",
        method: "POST",
        success: function (data) {
                fetcheddata = data;
                console.log(data);
                var data_e = JSON.parse(fetcheddata);
                console.log(data_e);
                var rating = [];
                var variety = [];
                var quality = [];

                for (var i in data_e) {
                    rating.push(data_e[i].ratingid);
                    variety.push(data_e[i].variety);
                    quality.push(data_e[i].quality);
            }
            console.log("Rating:" +rating);
            console.log("Variety:" +variety);
            console.log("Quality:" +quality);

           var chartdata = {
                labels: rating,
                datasets: [{
                        label: 'Variety',
                        backgroundColor: 'rgb(255, 0, 0)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: variety
                    },
                    {
                        label: 'Quality',
                        backgroundColor: 'rgb(0, 0, 255)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: quality
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var barGraph = new Chart(ctx, {
                type: 'bar',
                data: chartdata,
                options: {
                    barValueSpacing: 20,
                    title: {
                        display: true,
                        text: 'Variety',
                        fontSize: 20
                    },
                    scales: {
                        xAxes: [{
                            scaleLabel: {
                                display: true,
                                labelString: 'Product Id'
                            }
                        }],
                        yAxes: [{
                            scaleLabel: {
                                display: true,
                                labelString: 'Amount'
                            },
                            ticks: {
                                beginAtZero: true
                            }
                        }]

                    }
                }
            });
        },
        error: function (data) {
            console.log(data);
        }
    });

    document.getElementById('download-pdf').addEventListener("click", downloadPDF);   
    $("#download-xls").click(function (e) {

});
    $('#product_sales').on("click",function(){
        $('#chart-container-1').show();
        $('#chart-container').hide();
        $.ajax({
        url:"index.php?r=jobready/get-product-data",
        method: "POST",
        success: function (data) {
                proddata = data;
                console.log(data);
                var data_e = JSON.parse(proddata);
                console.log(data_e);
                var flavour = [];
                var munch = [];
                var perk = [];
                var snickers = [];

                for (var i in data_e) {
                    flavour.push(data_e[i].flavour);
                    munch.push(data_e[i].munch);
                    perk.push(data_e[i].perk);
                    snickers.push(data_e[i].snickers);
            }
            console.log("flavour:" +flavour);
            console.log("munch:" +munch);
            console.log("perk:" +perk);
            console.log("snickers:" +snickers);

           var chartdata = {
                labels: flavour,
                datasets: [{
                        label: 'Munch',
                        backgroundColor: 'rgb(255, 0, 0)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: munch
                    },
                    {
                        label: 'Perk',
                        backgroundColor: 'rgb(0, 0, 255)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: perk
                    },
                    {
                        label: 'Snickers',
                        backgroundColor: 'rgb(0, 255, 0)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: snickers
                    }
                ]
            };

            var ctx = $("#mycanvas_1");

            var barGraph1 = new Chart(ctx, {
                type: 'bar',
                data: chartdata,
                options: {
                    barValueSpacing: 20,
                    title: {
                        display: true,
                        text: 'Products',
                        fontSize: 20
                    },
                    scales: {
                        xAxes: [{
                            scaleLabel: {
                                display: true,
                                labelString: 'Flavour'
                            }
                        }],
                        yAxes: [{
                            scaleLabel: {
                                display: true,
                                labelString: 'Sales'
                            },
                            ticks: {
                                beginAtZero: true,
                                max: 700,
                                min: 0
                            }
                        }]

                    }
                }
            });
        },
        error: function (data) {
            console.log(data);
        }
    });
    });
});
function downloadPDF() {
        var canvas = document.querySelector('#mycanvas');
    //creates image
    var canvasImg = canvas.toDataURL("image/jpeg", 1.0);

    //creates PDF from img
    var doc = new jsPDF('landscape');
    doc.setFontSize(20);
    doc.text(15, 15, "Cool Chart");
    doc.addImage(canvasImg, 'JPEG', 10, 10, 280, 150 );
    doc.save('canvas.pdf');
}

我被困在download-xls jquery函数的单击上。

评论