首页 文章

将多个Highchart导出为Zip文件到我的项目目录

提问于
浏览
2
  • 我已在一个页面中成功创建了总共4个不同的图表 .

  • 我有一个按钮"Download Selected Chart"并点击此按钮 I need to create one ZIP file with selected chart PDF, Means if I select three charts then create a ZIP with three charts PDF.

  • 高脚椅提供下载图表的功能,如PDF,SVG,PNG等,但我不能为多个选定的图表做 .

  • 我检查了Highchart导出服务器文档 - http://www.highcharts.com/docs/export-module/setting-up-the-server但我不明白如何使用它 .

如果有人有想法,请帮助我,我该怎么做?

2 回答

  • 2

    一个示例解决方案是:

    • 创建图表
    var options1 = { 
        // ...
    };
    $('#chart1').highcharts(options1);
    
    • 要求Highcharts导出服务器生成图表的图像
    var exportUrl = 'http://export.highcharts.com/';
    var d1 = $.ajax({
        url: exportUrl,
        // ...
    });
    
    • 获取生成图像的内容
    $.when(d1).done(function(v1) {
        var p1 = new JSZip.external.Promise(function (resolve, reject) {
            JSZipUtils.getBinaryContent(exportUrl + v1[0], function(err, data) {
                // ...
            });
        });
        // ...
    
    • 使用JSZip构造并保存ZIP文件以及生成的图像的内容
    // ...
        Promise.all([p1]).then(function(values) {
            var zip = new JSZip();
            zip.file("chart1.png", values[0], {binary:true});
            zip.generateAsync({type:"blob"})
            .then(function(content) {
                saveAs(content, "charts.zip");
            });
        });
    });
    

    您可以看到this (very scrappy) JSFiddle demonstration如何获取ZIP文件 . 步骤如上所述,但不连接到任何按钮,而是在进入站点后立即执行 .

  • 1

    在这里,我发布了适合我的解决方案 .

    • 单击按钮,使用高图表导出服务器获取svg图像 .
    // get selected checkbox
         $('.selected_checkbox').each(function( index ){ 
             var obj = {},chart;
             chart = $('#each_chart').highcharts();
             obj.svg = chart.getSVG();
             obj.type = 'image/png';
             obj.async = true;
             obj.id = chart_id; 
            // ajax call for svg 
             $.ajax({
                    type: "POST",
                    url: url,// u need to save svg in your folder
                    data: obj, 
                    success: function(data)
                    { 
                     // redirect to php function and create zip 
                     window.location = 'php function call';
                    }
              )}
    
    • Ajax函数调用创建SVG ..
    ini_set('magic_quotes_gpc', 'off');
        $type = $_POST['type'];
        $svg = (string) $_POST['svg'];
        $filename = 'name';
        $id = $_POST['id'];
    
     if (get_magic_quotes_gpc()) {
            $svg = stripslashes($svg);  
    }
    // check for malicious attack in SVG
    if(strpos($svg,"<!ENTITY") !== false || strpos($svg,"<!DOCTYPE") !== false){
            exit("the posted SVG could contain code for a malicious attack");
    } 
    
    $tempName = $filename.'_'.$id;
    // allow no other than predefined types
    if ($type == 'image/png') {
            $typeString = '-m image/png';
            $ext = 'png';
    
    } elseif ($type == 'image/jpeg') {
            $typeString = '-m image/jpeg';
            $ext = 'jpg';
    } elseif ($type == 'application/pdf') {
            $typeString = '-m application/pdf';
            $ext = 'pdf';
    } elseif ($type == 'image/svg+xml') {
            $ext = 'svg';
    } else { // prevent fallthrough from global variables
            $ext = 'txt';
    }
     
    
       $outfile = APPPATH."tempp/$tempName.$ext";
       if (!file_put_contents(APPPATH."tempp/$tempName.svg", $svg)) { 
        die("Couldn't create temporary file.");  
    }}
    
    • Ajax Success函数重定向代码..
      在这里,您需要读取目录文件并从SVG创建PDF .
      将每个PDF添加到ZIP后 .

    This is Example solution.You have to change code as per your requirement

相关问题