Highcharts版本7.1.1如何使用amd模块离线导出PDF

时间:2019-04-30 12:10:31

标签: highcharts requirejs amd export-to-pdf offline-mode

如何使用amd模块通过脱机导出到pdf的图表(Highcharts)? Require jsPDF模块“有时”抛出requirejs不匹配的错误或弄乱了其他require的回调引用。

问题高级图表论坛:https://www.highcharts.com/forum/viewtopic.php?f=9&t=41929&sid=5178a78a6547d8fb14769a85392b276c

var chart;
require(['highcharts', 'highcharts/modules/exporting', 'highcharts/modules/offline-exporting', 'jsPDF', 'svg2pdf'], function(Highcharts, a, b, jsPDF, svg2pdf) {
    window.jsPDF = jsPDF.default;
    window.svg2pdf = svg2pdf;
    chart = Highcharts.chart('container', {

    exporting: {
      libURL: 'https://code.highcharts.com/7.1.1/lib'
    },

    series: [{
      data: [3, 4, 5, 3, 2]
    }]

  });
});
<html>

  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.15/require.js"></script>

    <script>
      require.config({
        packages: [{
          name: 'highcharts',
          main: 'highcharts',
          location: 'https://code.highcharts.com/7.1.1'
        }],
        paths: {
          'jsPDF': 'https://code.highcharts.com/7.1.1/lib/jspdf',
          'svg2pdf': 'https://code.highcharts.com/7.1.1/lib/svg2pdf'
        }
      });

    </script>

  </head>

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

</html>

JsFiddle:https://jsfiddle.net/zbc149wh/2/

谢谢

2 个答案:

答案 0 :(得分:1)

问题来自jsPDF: https://github.com/yWorks/jsPDF

我为所有define语句设置了一个名称,因此不再有匿名模块定义。 现在可以了。

第3行示例

之前:

typeof define === 'function' && define.amd ? define(['exports'], factory) :

之后:

typeof define === 'function' && define.amd ? define('jsPDF', ['exports'], factory) :

答案 1 :(得分:0)

您可以像这样设置离线导出

exporting: {
        chartOptions: {
            plotOptions: {
                series: {
                    dataLabels: {
                        enabled: true
                    }
                }
            }
        },
        fallbackToExportServer: false
    }

您的代码分叉了:https://jsfiddle.net/viethien/fs03ou4k/7/

API参考:https://api.highcharts.com/highcharts/exporting.fallbackToExportServer

文档:https://www.highcharts.com/docs/export-module/client-side-export