首页 文章

HighCharts导出文件名

提问于
浏览
2

我使用highcharts导出模块将图表导出为pdf . 在我的代码中,我创建了一个片段 chart 对象,可以在使用的不同gui控件上进行操作 . 它看起来像这样:

options = {
   ...
   ...
   exporting:{
      type: "application/pdf",
      filename: "default",

      buttons:{
        exportButton:{
            menuItems: null,
            onclick:function(){
                var fileName = "AAAA";//it's dynamic in reality.
                alert(options.exporting.filename);//alerts "default"
                options.exporting.filename = fileName;
                alert(options.exporting.filename);//alerts "AAAA"
                this.exportChart();
             }
         },
         printButton: {
             enabled: false
         }
      }
   }
}

现在,无论何时单击导出按钮,下载的文件都将命名为 default.pdf ,而警报显示该属性已更改 .

此外,由于第一个警报显示结果为 default (这不是默认值,实际上是 chart ),因此我明确表示我正在引用正确的属性,因此在错误的属性中设置文件名不会导致错误 .

任何人都可以解释这种情况或建议更改,让我下载动态名称的文件 . ?

2 回答

  • 7

    你能尝试一下吗?

    exportButton:{
                menuItems: null,
                onclick:function(){
                    var fileName = "AAAA";//it's dynamic in reality.                                    
                    this.exportChart({filename : fileName});
                 }
             }
    

    exportChart方法也接受选项参数..

  • 2

    您可以更改文件的文件名 . 这是关于如何实现这一点的example . 相关代码:

    exportButton: {
        menuItems: null,
        onclick: function() {
            chart.exportChart({filename: 'my-png'}, null);
        }
    },
    

    这是另一个example,它显示了在导出和打印过程中可以控制的内容 . 相关代码:

    $('#buttonExport').click(function() {
        var e = document.getElementById("ExportOption");
        var ExportAs = e.options[e.selectedIndex].value;   
    
        if(ExportAs == 'PNG')
        {
            chart.exportChart({type: 'image/png', filename: 'my-png'}, {subtitle: {text:''}});
        }
        if(ExportAs == 'JPEG')
        {
            chart.exportChart({type: 'image/jpeg', filename: 'my-jpg'}, {subtitle: {text:''}});
        }
        if(ExportAs == 'PDF')
        {
            chart.exportChart({type: 'application/pdf', filename: 'my-pdf'}, {subtitle: {text:''}});
        }
        if(ExportAs == 'SVG')
        {
            chart.exportChart({type: 'image/svg+xml', filename: 'my-svg'}, {subtitle: {text:''}});
        }
    }); 
    
    $('#buttonPrint').click(function() {
        chart.setTitle(null, { text: ' ' });
        chart.print();
        chart.setTitle(null, { text: 'Click and drag in the plot area to zoom in' });
    });
    

相关问题