简体   繁体   English

如何使用 Chart.js 以 Excel 和 PDF 格式导出图表

[英]How to export a chart in Excel and PDF format using Chart.js

I have tried to create a sample to demo the Charts using Chart.js .我尝试使用Chart.js创建一个示例来演示图表。 I am able to do it very well.我能够做得很好。 Now I want to export this Chart to Excel and PDF.现在我想将此图表导出为 Excel 和 PDF。

Or is it possible to do so using Chart.js或者是否可以使用 Chart.js 这样做

Here's my JavaScript code:这是我的 JavaScript 代码:

function TyLyReport(){
    var barChartData = {
        labels : ["Jan","Feb","Mar"],
        datasets : [
            {
                fillColor : "#616161",
                highlightFill: "#616161",
                data : [1000,2000,2500]
            },
            {
                fillColor : "#FF4848",
                highlightFill: "#FF4848",
                data : [1016,1800,2900]
            }
        ]
    }
    window.onload = function(){
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myBar = new Chart(ctx).Bar(barChartData, {
            responsive : true,
            scaleOverride: true,
            scaleSteps: 3,
            scaleStepWidth: 1000,
            scaleStartValue: 0,
            scaleShowLabels: true,
            barValueSpacing : 30
        });
    }
}

Here's my HTML code:这是我的 HTML 代码:

<div style="width: 80%">
    <canvas id="canvas" class="reportbox"></canvas>
</div>
<script>
    TyLyReport();
</script>

Can I do so using this Chart.js?我可以使用这个 Chart.js 这样做吗?

If yes, please suggest the probable solution or if no, please tell me if there's any open source tool to do so.如果是,请提出可能的解决方案,如果否,请告诉我是否有任何开源工具可以这样做。

Thanks a lot in advance.非常感谢。

Chart.js doesnt support conversion to pdf but you can convert it to image by using following code Chart.js 不支持转换为 pdf,但您可以使用以下代码将其转换为图像

function done() {
    console.log('done');
    var url=document.getElementById("page").toDataURL();
    document.getElementById("url").src=url;
}

In options you have to set the following :在选项中,您必须设置以下内容:

onAnimationComplete: done

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

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