在 Angular Highcharts 折线图中添加自定义按钮

Add custom buttons in Angular Highcharts Line Chart

我正在尝试在导出 属性

的 Angular Highcharts 折线图中添加 2 个自定义按钮
exporting: {
    enabled: true,
    buttons: {
        customButton: {
            text: 'Custom Button',
            click: () => {
                alert('You pressed the button!');
            },
        },
        anotherButton: {
            text: 'Another Button',
            click: () => {
                alert('You pressed another button!');
            },
        },
    },
}

但是这2个按钮没有显示。这里可能缺少什么逻辑? Stackblitz

exporting.buttons 是仅在导出菜单中编辑按钮的选项:https://api.highcharts.com/highcharts/exporting.buttons

要呈现自定义按钮,请使用 SVGRenderer 功能:https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#button

您可以在 render 回调中添加这些按钮 - 在初始加载后和每次重绘后调用:https://api.highcharts.com/highcharts/chart.events.render

嗨,我认为下面的代码片段会对您有所帮助:

chart: {
  type: "line",
  renderTo: "chart",
  events: {
    render(events) {
      let chart = this;

      if (chart.customButton) {
        chart.customButton.destroy();
      }
      chart.customButton = chart.renderer
        .button("custom button", 100, 40, () => {
          console.log("clicked.....");
          chart.exportChart({
            type: "application/pdf",
            filename: "line-chart"
          });
        })
        .add();
    }
  }
}

这里点击按钮,可以实现导出。这里的示例导出 PDF。

演示:

https://stackblitz.com/edit/highcharts-angular-functionality?file=src%2Fapp%2FWhosebug%2Fhigh-chart-question%2Fhigh-chart-question.component.ts

https://highcharts-angular-functionality.stackblitz.io/exportcolor

希望这对您有所帮助。