2

I have a page that contains some data and some Highcharts. Highcharts has some fantastic functionality for exporting the chart as an image. How do I include the contents of a table or div to be exported with the chart so that there is one big image or pdf that contains the chart and the data? What I have tried so far, none of which is ideal, is:

html2canvas, seems like the best solution but does not seem to include Highcarts. Maybe I'm doing something wrong as the documentation doesn't seem great and I have been unable to find any good examples from Goggling that include the charts.

The second best solution that I have come across is including the data in the Highcharts. Example of this is located: http://jsfiddle.net/highcharts/z9zXM/ This could work, but would destroy the current formatting on the page. In other words, it would be an ugly solution :).

The other solutions that I found are to do with screenshots, but this would not capture the full page.

The ideal solution that may or may not exist is, using JavaScript in someway to combine a div or table with the Highchart into a pdf or jpg, is this possible and if it is, where would I find an example?

4

2 回答 2

0

将本机 Highcharts 导出功能与 html2canvas 结合起来怎么样?

首先,您将 Highcharts 导出到文件中。我猜想知道你必须调用的 URL 来获取图像就足够了。

其次,您使用标准的 html2canvas 魔法来生成没有图表的文档。(您可能需要使用图表指定 div 的宽度和高度,以便它不会在导出的文件中折叠)。

最后,您使用 gd 或 imagemagick 将一个粘贴到另一个上并返回给客户端。

于 2012-08-03T17:33:36.397 回答
0

这很简单,您可以使用此代码来捕获您必须在 html2canvas 中定义 div id 的特定区域的屏幕截图。我在这里使用 2 div-:

div id="car"
div id ="chartContainer"
如果你只想捕获汽车然后使用car我在这里只捕获汽车 你可以更改chartContainer以捕获图形 html2canvas($( '#car') 复制并粘贴此代码

<html>
    <head>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<script>
    window.onload = function () {
    
    var chart = new CanvasJS.Chart("chartContainer", {
        animationEnabled: true,
        theme: "light2",
        title:{
            text: "Simple Line Chart"
        },
        axisY:{
            includeZero: false
        },
        data: [{        
            type: "line",       
            dataPoints: [
                { y: 450 },
                { y: 414},
                { y: 520, indexLabel: "highest",markerColor: "red", markerType: "triangle" },
                { y: 460 },
                { y: 450 },
                { y: 500 },
                { y: 480 },
                { y: 480 },
                { y: 410 , indexLabel: "lowest",markerColor: "DarkSlateGrey", markerType: "cross" },
                { y: 500 },
                { y: 480 },
                { y: 510 }

            ]
        }]
    });
    chart.render();
    
    }
</script>
</head>

<body bgcolor="black">
<div id="wholebody">  
<a href="javascript:genScreenshotgraph()"><button style="background:aqua; cursor:pointer">Get Screenshot of Cars onl </button> </a>

<div id="car" align="center">
    <i class="fa fa-car" style="font-size:70px;color:red;"></i>
    <i class="fa fa-car" style="font-size:60px;color:red;"></i>
    <i class="fa fa-car" style="font-size:50px;color:red;"></i>
    <i class="fa fa-car" style="font-size:20px;color:red;"></i>
    <i class="fa fa-car" style="font-size:50px;color:red;"></i>
    <i class="fa fa-car" style="font-size:60px;color:red;"></i>
    <i class="fa fa-car" style="font-size:70px;color:red;"></i>
</div>
<br>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>

<div id="box1">
</div>
</div>>
</body>

<script>

function genScreenshotgraph() 
{
    html2canvas($('#car'), {
        
      onrendered: function(canvas) {

        var imgData = canvas.toDataURL("image/jpeg");
        var pdf = new jsPDF();
        pdf.addImage(imgData, 'JPEG', 0, 0, -180, -180);
        pdf.save("download.pdf");
        
      
      
      }
     });

}

</script>

</html>

于 2018-08-19T12:33:01.133 回答