首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用js打印canvas.js图表

使用js打印canvas.js图表
EN

Stack Overflow用户
提问于 2014-10-20 14:56:06
回答 2查看 1.7K关注 0票数 2

我想用javascript打印canvas.js图表。打印代码。

当我单击“打印”按钮时,浏览器中的“打印”弹出将显示“打印预览无”。

我如何解决这个问题?

代码语言:javascript
运行
复制
var html = "<html>";
html += document.getElementById("chartContainerInPT").innerHTML;
html += "</html>";
var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status  =0');
printWin.document.write(html);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();

html代码

代码语言:javascript
运行
复制
<div id="chart_divInPT" class="chart">
    <div id="chartContainerInPT" style="height: 300px; width: 100%;"></div>
</div>
<button class="btn save" type="button" style="float: right;" id="printInPT">Print</button> 

js

代码语言:javascript
运行
复制
var chart = new CanvasJS.Chart(
    "chartContainerInPT",
    {
        theme : "theme1",
        title : {
        text : ""
    },
    data : [ {
        type : "column",
        dataPoints : [
        {
            label : "Attempted",
            y : chapterWiseResult['noOfAttempt']
        },
        {
            label : "Correct",
            y : chapterWiseResult['noOfCorrect']
        },
        {
            label : "Wrong",
            y : wrong
        },
        {
            label : "Unattempted",
            y : unattempted
        },
        ]
    } ]
});

如何在浏览器中打印图表?

EN

回答 2

Stack Overflow用户

发布于 2014-10-20 15:03:54

作为解决方案,您可以将画布保存为图像。

代码语言:javascript
运行
复制
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
window.location.href=image;
票数 0
EN

Stack Overflow用户

发布于 2021-02-22 05:52:59

更新2021年

Canvas.js图表现在添加了打印( pdf,png,jpg)格式功能。启用导出选项如下

代码语言:javascript
运行
复制
            title: {
                text: "Alarm Statistics",
              },
            **exportEnabled: true**,// enable print option for the chart 

            data: [{...},{...}]

代码语言:javascript
运行
复制
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
window.onload = function () {
    var chart = new CanvasJS.Chart("chartContainer",
    {
        title: {
            text: "Chart with export feature enabled",
        },
        exportEnabled: true,
        axisX:{
            minimum: -900, 
            interval: 100,
        },
        data: [
        {
            type: "area",
            fillOpacity: .4,
            lineThickness: 3,
            dataPoints: [
                { x: -100, y: 71 },
                { x: -200, y: 55 },
                { x: -300, y: 50 },
                { x: -400, y: 65 },
                { x: -500, y: 105, markerType: "circle", markerSize: 9, markerColor: "brown", indexLabel: "Highest" },
                { x: -600, y: 68 },
                { x: -700, y: 28 },
                { x: -800, y: 34 },
                { x: -900, y: 14}
            ]
        }
        ]
    });
    chart.render();
}
</script>
<script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
</body>
</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26468695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档