1

canvasjs 允许以简单的方式将图形导出为 pdf:http: //jsfiddle.net/canvasjs/cm1qyk2L/

我正在尝试与echarts相同,但我不知道如何正确采用上述 jsfiddle 中的第 21 行。什么是正确的 jquery 选择器?

    <div id="main" style="width: 750px;height:500px;"></div>
    <script type="text/javascript">
        // based on prepared DOM, initialize echarts instance
        var myChart = echarts.init(document.getElementById('main'));

        // specify chart configuration item and data
        var 

option = {
    color: ['#3398DB'],
    tooltip : {
        trigger: 'axis',
        axisPointer : {            
            type : 'shadow'        
        }
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis : [
        {
            type : 'category',
            data : ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
                alignWithLabel: true
            }
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'Salami',
            type:'bar',
            barWidth: '60%',
            data:[10, 52, 200, 334, 390, 330, 220]
        }
    ]
};
        // use configuration item and data specified to show chart
        myChart.setOption(option);

//console.log(dataURL);

var canvas = $("#main .").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>
<button id="exportButton" type="button">Export as PDF</button>
4

1 回答 1

0

这是我的解决方案:

var canvas = $("#main canvas").get(0);

$(document).ready(function(){
    $("#exportButton").click(function(){
        var dataURL = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new jsPDF();
        pdf.addImage(dataURL, 'JPEG', 0, 0);
        pdf.save("download.pdf");
    });
});

https://plnkr.co/edit/Wv8JNWeYNxsLH798wnx8?p=preview

于 2018-03-12T12:28:54.177 回答