【问题标题】:Export of high chart to CSV using custom button使用自定义按钮将高图表导出到 CSV
【发布时间】:2020-04-01 10:16:41
【问题描述】:

我有一个 Angular 项目,我正在尝试将图表导出到不同的导出。当我尝试时,我能够将图表导出为 png、jpeg、pdf、SVG 等。但我无法将图表导出为 CSV 或 .我试过下面的代码:

this.lineChart.chart.downloadCSV(); //For CSV

@ViewChild("lineChart", { static: false }) lineChart: any;
Highcharts = Highcharts;
chartOptions = {
series: [
  {
    name: "Current - 2014",
    data: [
      {
        name: "1",
        y: 200030
      },
      {
        name: "2",
        y: 23300
      },
      {
        name: "3",
        y: 2300
      },
      {
        name: "4",
        y: 23002
      },
      {
        name: "5",
        y: 340300
      },
      {
        name: "6",
        y: 263030
      },
      {
        name: "7",
        y: 530300
      },
      {
        name: "8",
        y: 880300
      },
      {
        name: "9",
        y: 232300
      },
      {
        name: "10",
        y: 34300
      },
      {
        name: "11",
        y: 200030
      },
      {
        name: "12",
        y: 980300
      }
    ],
    color: "indigo"
  },
  {
    name: "Prior - 2013",
    data: [
      {
        name: "1",
        y: 90030
      },
      {
        name: "2",
        y: 23300
      },
      {
        name: "3",
        y: 672300
      },
      {
        name: "4",
        y: 230300
      },
      {
        name: "5",
        y: 230300
      },
      {
        name: "6",
        y: 200030
      },
      {
        name: "7",
        y: 230300
      },
      {
        name: "8",
        y: 230300
      },
      {
        name: "9",
        y: 230300
      },
      {
        name: "10",
        y: 230300
      },
      {
        name: "11",
        y: 200030
      },
      {
        name: "12",
        y: 230300
      }
    ],
    color: "green"
  }
  ],
chart: {
  type: "line",
  renderTo: "chart",
  events: {
    load: function(event) {
    }
  }
},
title: {
  text: "Net activity along fiscal period accross years"
},
xAxis: {
  title: {
    text: "Fiscal Period"
  },
  type: "category"
},
yAxis: {
  title: {
    text: "Functional Amount"
  },
  gridLineWidth: 1
},
legend: {
  enabled: true,
  align: "right",
  verticalAlign: "middle",
  layout: "vertical"
},
credits: {
  enabled: false
},
plotOptions: {
  series: {
    allowPointSelect: true,
    minPointLength: 3,
    point: {
      events: {
        select: e => {
          console.log("x-axis value: ", e.target.name);
          console.log("y-axis value: ", e.target.y);
          let selectedPoint = this.lineChart.chart.getSelectedPoints();
          selectedPoint.forEach((point, index) => {
            console.log(
              "Point " + index + " : ",
              point.name + " - " + point.y
            );
          });
        },
        load: e => {
          this.lineChart.chart.reflow();
        },
        click: function(e) {
        }
      }
    }
  }
},
tooltip: {
  formatter: function() {
    return (
      this.series.name + "<br/>" + this.x + " : " + "<b>" + this.y + "<b>"
    );
  }
},
exporting: {
  enabled: true,
  showTable: false,
  fileName: "line-chart"
}};

我在控制台中遇到错误:

AppComponent.html:16 ERROR TypeError: Cannot read property 'decimalPoint' of undefined
    at d.Chart.b.Chart.getCSV (export-data.src.js:760)
    at d.Chart.b.Chart.downloadCSV (export-data.src.js:978)
    at AppComponent.export (app.component.ts:230)
    at Object.eval [as handleEvent] (AppComponent.html:16)
    at handleEvent (core.umd.js:sourcemap:29354)
    at callWithDebugContext (core.umd.js:sourcemap:30424)
    at Object.debugHandleEvent [as handleEvent] (core.umd.js:sourcemap:30151)
    at dispatchEvent (core.umd.js:sourcemap:20002)
    at eval (core.umd.js:sourcemap:28563)
    at HTMLButtonElement.eval (dom_renderer.ts:52)

任何人都可以帮助我。我尝试的演示项目可在以下链接中找到:

https://stackblitz.com/edit/angular-chart-export?file=src%2Fapp%2Fapp.component.ts

https://angular-chart-export.stackblitz.io

提前感谢大家。

【问题讨论】:

    标签: angular typescript highcharts


    【解决方案1】:

    我认为您缺少 TS 文件中的导入。尝试在您的组件中添加以下导入并尝试:

    import HC_exporting from "highcharts/modules/exporting";
    import HC_Data from "highcharts/modules/export-data";
    HC_exporting(Highcharts);
    HC_Data(Highcharts);
    

    【讨论】:

    • 您好,非常感谢......导出到 svg 工作正常......但导出到 csv 仍然失败。我已经更新了 stackblitz 中的 repo,你能帮我导出到 csv 吗?回购 - stackblitz.com/edit/…
    • 这个答案几乎是正确的。唯一需要改变的是应用模块的顺序:exporting 模块应该在export-data 之前加载。像这样:HC_exporting(Highcharts); HC_Data(Highcharts);这里解释了脚本的顺序:highcharts.com/docs/export-module/export-module-overview
    【解决方案2】:

    首先导入脚本文件:

    <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/export-data.js"></script>
    <script src="https://code.highcharts.com/modules/accessibility.js"></script>
    

    在chatOptions 中,我们可以在高位图表菜单中写入customize options,我们可以自定义下拉选项。 在图表选项中,我们可以这样写:

    exporting: {
    buttons: {
      contextButton: {
        menuItems: ['downloadCSV', 'downloadSVG'],
      },
    },
    

    }

    示例:click here 参考:click here,Example2

    【讨论】:

    • 谢谢@Krishna Pvs。我正在尝试的导出是使用外部按钮。现在我要从 SVG 下载。但我仍然无法以 CSV 格式下载。我也试过你的例子,但没有用。 downloadCSV 选项未显示在按钮选项中。我不知道这是否可能。请对此发表评论。另外补充一下,我正在尝试在角度应用程序中使用 highcharts-angular。现在我添加了导入: import Accessbility from "highcharts/modules/accessibility";现在我没有收到错误。但它没有下载。
    • 在尝试导出 CSV 时出现错误 - “无法读取未定义的属性 'decimalPoint'”。
    • 我认为这是另一个问题。不在highcharts中,它来自您的应用程序检查后台功能一次。
    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    相关资源
    最近更新 更多