使用canvasjs和jspdf生成图表和pdf

时间:2016-11-15 13:21:29

标签: pdf-generation jspdf canvasjs

使用canvasjs和jspdf生成图表并导出为pdf。图表正在生成,但导出为pdf不起作用。我在这里缺少,如果有人可以建议。你的时间将不胜感激。谢谢

。     

<head>

<title>Graphical Representation Of Stores</title>
<style type="/css">
</style>

<script type="text/javascript" src="canvasjs.min.js"> </script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.min.js"></script>

</head>

<body>
<form>

<div id="chartContainer" style="height: 360px; width: 100%;"></div>
<button id="exportButton" type="button">Export as PDF</button>

<script type="text/javascript">


var chart = new CanvasJS.Chart("chartContainer",
{
        title: {
            text: "Exporting chart using jsPDF & toDataurl"
        },
        data: [
        {
            type: "spline",
            dataPoints: [ 
                { x: 10, y: 4 }, 
                { x: 20, y: 7 },
                { x: 30, y: 2 },
                { x: 40, y: 3 },
                { x: 50, y: 5 }
            ]
        }
        ]
});
chart.render();

var canvas = $("#chartContainer.canvasjs-chart-canvas").get(0);
var dataURL = canvas.toDataURL();
//console.log(dataURL);

$("#exportButton").click(function(){
    var pdf = new jsPDF();
    pdf.addImage(dataURL, 'JPEG', 0, 0);
    pdf.save("download.pdf");
});
</script>
</form>
</body>
</head>
</html>

1 个答案:

答案 0 :(得分:4)

和Manish,

问题在于选择器var canvas = $("#chartContainer.canvasjs-chart-canvas").get(0);,理想情况下应该是var canvas = $("#chartContainer .canvasjs-chart-canvas").get(0);(需要空间)。之后它的工作非常精细。

选中jsfiddle