【问题标题】:Try to offline exporting to Highcharts尝试离线导出到 Highcharts
【发布时间】:2019-04-27 07:03:06
【问题描述】:

我想使用 highcharts 进行离线导出。 这是我的代码

import {Chart} from 'highcharts-vue'
import Highcharts from 'highcharts'
import offlineExporting from "highcharts/modules/offline-exporting"
offlineExporting(Highcharts)

我尝试将exporting.js 和offline-exporting.js 一起使用,并且成功了。 这是问题的解决方案吗?如果不是,有没有其他解决方案?

【问题讨论】:

  • “我尝试同时使用exporting.js 和offline-exporting.js 并且成功了” 那么问题出在哪里?
  • 我只是在搜索“有什么方法可以只使用offline-exporting.js 吗?”
  • documentaion 之后:“要使用该模块,只需将其包含在导出模块之后。即使禁用回退,导出模块也是必需的依赖项。” I不认为这是可能的。
  • 好的,谢谢你的反馈。

标签: javascript vue.js highcharts export offline-mode


【解决方案1】:

要使用offline-exporting 模块,您还需要导入exporting 模块。

此外,您可以禁用 fallbackToExportServer 属性,以便导出永远不会使用 Highcharts 导出服务器。

exporting: {
    ...,
    fallbackToExportServer: false
},

API:

fallbackToExportServer:布尔值

如果离线导出模块无法在客户端导出图表,是否回退到导出服务器。

...

现场演示: https://jsfiddle.net/BlackLabel/92dbwLgx/

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

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

【讨论】:

    【解决方案2】:

    您需要将“exporting.fallbackToExportServer”设置为“false”,以便始终在本地导出图表。

    http://jsfiddle.net/viethien/rcmpbsL1/

    /* Automate testing of module somewhat */
    
    var nav = Highcharts.win.navigator,
        isMSBrowser = /Edge\/|Trident\/|MSIE /.test(nav.userAgent),
        isEdgeBrowser = /Edge\/\d+/.test(nav.userAgent),
        containerEl = document.getElementById('container'),
        parentEl = containerEl.parentNode,
        oldDownloadURL = Highcharts.downloadURL;
    
    function addText(text) {
        var heading = document.createElement('h2');
        heading.innerHTML = text;
        parentEl.appendChild(heading);
    }
    
    function addURLView(title, url) {
        var iframe = document.createElement('iframe');
        if (isMSBrowser && Highcharts.isObject(url)) {
            addText(title +
            ': Microsoft browsers do not support Blob iframe.src, test manually'
            );
            return;
        }
        iframe.src = url;
        iframe.width = 400;
        iframe.height = 300;
        iframe.title = title;
        iframe.style.display = 'inline-block';
        parentEl.appendChild(iframe);
    }
    
    function fallbackHandler(options) {
        if (options.type !== 'image/svg+xml' && isEdgeBrowser ||
            options.type === 'application/pdf' && isMSBrowser) {
            addText(options.type + ' fell back on purpose');
        } else {
            throw 'Should not have to fall back for this combination. ' +
                options.type;
        }
    }
    
    Highcharts.downloadURL = function (dataURL, filename) {
        // Emulate toBlob behavior for long URLs
        if (dataURL.length > 2000000) {
            dataURL = Highcharts.dataURLtoBlob(dataURL);
            if (!dataURL) {
                throw 'Data URL length limit reached';
            }
        }
        // Show result in an iframe instead of downloading
        addURLView(filename, dataURL);
    };
    
    Highcharts.Chart.prototype.exportTest = function (type) {
        this.exportChartLocal({
            type: type
        }, {
            title: {
                text: type
            },
            subtitle: {
                text: false
            }
        });
    };
    
    Highcharts.Chart.prototype.callbacks.push(function (chart) {
        if (!chart.options.chart.forExport) {
            var menu = chart.exportSVGElements && chart.exportSVGElements[0],
                oldHandler;
            chart.exportTest('image/png');
            chart.exportTest('image/jpeg');
            chart.exportTest('image/svg+xml');
            chart.exportTest('application/pdf');
    
            // Allow manual testing by resetting downloadURL handler when trying
            // to export manually
            if (menu) {
                oldHandler = menu.element.onclick;
                menu.element.onclick = function () {
                    Highcharts.downloadURL = oldDownloadURL;
                    oldHandler.call(this);
                };
            }
        }
    });
    
    /* End of automation code */
    
    
    Highcharts.chart('container', {
        exporting: {
            chartOptions: { // specific options for the exported image
                plotOptions: {
                    series: {
                        dataLabels: {
                            enabled: true
                        }
                    }
                }
            },
            sourceWidth: 400,
            sourceHeight: 300,
            scale: 1,
            fallbackToExportServer: false,
            error: fallbackHandler
        },
    
        title: {
            text: 'Offline export'
        },
    
        subtitle: {
            text: 'Click the button to download as PNG, JPEG, SVG or PDF'
        },
    
        chart: {
            type: 'area'
        },
    
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
    
        series: [{
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 126.0, 148.5, 216.4, 194.1, 95.6, 54.4]
        }]
    
    });
    #container {
        max-width: 800px;
        height: 400px;
        margin: 1em auto;
    }
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>
    <script src="https://code.highcharts.com/modules/offline-exporting.js"></script>
    
    <div id="container"></div>

    【讨论】:

      【解决方案3】:

      对于offline-exporting,您必须进行客户端导出实现

      import { withHighcharts } from 'react-jsx-highcharts';
      import Highcharts from 'highcharts';
      import exportingOption from 'highcharts/modules/exporting';
      import offlineOption from 'highcharts/modules/offline-exporting';
      
      exportingOption(Highcharts);
      offlineOption(Highcharts);
      
      withHighcharts(Component, Highcharts);
      

      此外,您必须将导出对象添加为 Highcharts 选项

      exporting: {
          buttons: {
              contextButton: {
                  menuItems: ['downloadPNG', 'downloadJPEG', 'downloadPDF', 'downloadSVG']
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-09
        • 1970-01-01
        • 2017-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        • 2021-05-14
        • 1970-01-01
        相关资源
        最近更新 更多