将Highcharts数据表导出为PDF -Angular

tquggr8v  于 2022-11-11  发布在  Highcharts
关注(0)|答案(1)|浏览(195)

我在我的Angular 应用程序中使用Highchart。
我想格式化一个Highcharts数据表,并将其与图表沿着导出为PDF。
在stackbliz复制了同样的东西。
https://stackblitz.com/edit/angular-ivy-u9zvps?file=src%2Fapp%2Fapp.component.ts
这里的PDF下载只包含图表。
我试图实现〈https://jsfiddle.net/Ld561nbt/4/>在官方Highchart页https://api.highcharts.com/highcharts/exporting.showTable?_ga=2.151177465.1965572452.1639477244-32713185.1637664871中提到的Angular ,但没有运气。
有人能给我建议如何在导出PDF中包含Highchart dataTable吗

.HTML内容

<div class="row">
<div class="col-md-4">
<div class="charts card mb-3">
<highcharts-chart
[Highcharts]="Highcharts"
[options]="chartofTop10Senders"
[(update)]="updateFlag"
[oneToOne]="true"
style="width: 100%; display: block; height: 220px;">
</highcharts-chart>
</div>
</div>
</div>

.TS

import { Component, VERSION } from '@angular/core';
import * as Highcharts from 'highcharts';
import HC_exporting from 'highcharts/modules/exporting';
import offlineExporting from 'highcharts/modules/offline-exporting';
import HC_Data from 'highcharts/modules/export-data';

HC_exporting(Highcharts);
offlineExporting(Highcharts);
HC_Data(Highcharts);

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  chartRef;
  Highcharts: typeof Highcharts = Highcharts; // required
  chartConstructor: string = 'chart'; // optional string, defaults to 'chart'
  chartofTop10Senders: Highcharts.Options = {
    chart: { renderTo: 'chartTop10Senders' },
    lang: { noData: '' },
    title: { text: 'Top 10 Senders', useHTML: false },
    subtitle: { text: null },
    credits: { enabled: false },
    xAxis: { title: { text: 'Sender' }, categories: [], visible: false },
    yAxis: { visible: false },
    legend: { enabled: false },
    series: [],

    exporting: { showTable: true, allowHTML: true, enabled: true },
  };

  chartCallback: Highcharts.ChartCallbackFunction = function (chart) {
    this.chartRef = chart;
  }; // optional function, defaults to null
  updateFlag: boolean = false; // optional boolean
  oneToOneFlag: boolean = true; // optional boolean, defaults to false
  alertAnalysisData: {
    delivery_reach: { name: string; y: number }[];
    senders: { name: string; y: number }[];
    users: { name: string; y: number }[];
  };

  constructor() {}

  ngOnInit() {
    this.assigndata();
  }

  assigndata() {
    this.alertAnalysisData = {
      delivery_reach: [
        { name: 'Deliveries', y: 415 },
        { name: 'Alerts', y: 20 },
        { name: 'RecipientUsers', y: 118 },
        { name: 'RecipientGroups', y: 0 },
      ],
      senders: [
        { name: 'Daniel', y: 14 },
        { name: 'Ebison', y: 3 },
        { name: 'ibrahim ', y: 2 },
        { name: 'Vela', y: 1 },
      ],
      users: [
        { name: 'Sati', y: 17 },
        { name: 'Mobile', y: 17 },
        { name: 'ibrahim ', y: 17 },
        { name: 'Shan', y: 16 },
        { name: 'Vela', y: 16 },
        { name: 'Ebi', y: 15 },
        { name: 'Daniel', y: 14 },
        { name: 'Vela', y: 2 },
        { name: 'Vee', y: 1 },
        { name: 'Yoge', y: 1 },
        { name: 'Satish C', y: 1 },
        { name: 'Abtr', y: 1 },
      ],
    };
    this.updateChart();
  }

  updateChart() {
    this.chartofTop10Senders.series[0] = {
      type: undefined,
      data: this.alertAnalysisData.delivery_reach,
    };
    this.updateFlag = true;
  }

  exportPDF() {
    this.updateFlag = true;
    (Highcharts as any).exportAlertSummary(
      [, Highcharts.charts[Highcharts.charts.length - 1]],
      { type: 'application/pdf', filename: 'Alert Summary' }
    );
  }
}
8ljdwjyq

8ljdwjyq1#

jsfiddle使用技巧导出表的示例。添加表作为子标题,使其成为导出的一部分。
现场演示:https://jsfiddle.net/BlackLabel/5rwnsmz7/
在此部分,创建表并将其添加为副标题。

Highcharts.addEvent(Highcharts.Chart, 'render', function() {
var table = this.dataTableDiv;
if (table) {

// Apply styles inline because stylesheets are not passed to the exported SVG
Highcharts.css(table.querySelector('table'), {
  'border-collapse': 'collapse',
  'border-spacing': 0,
  background: 'white',
  'min-width': '100%',
  'font-family': 'sans-serif',
  'font-size': '14px'
});

[].forEach.call(table.querySelectorAll('td, th, caption'), function(elem) {
  Highcharts.css(elem, {
    border: '1px solid silver',
    padding: '0.5em'
  });
});

Highcharts.css(table.querySelector('caption'), {
  'border-bottom': 'none',
  'font-size': '1.1em',
  'font-weight': 'bold'
});

[].forEach.call(table.querySelectorAll('caption, tr'), function(elem, i) {
  if (i % 2) {
    Highcharts.css(elem, {
      background: '#f8f8f8'
    });
  }
});

// Add the table as the subtitle to make it part of the export
this.setTitle(null, {
  text: table.innerHTML,
  useHTML: true
});
if (table.parentNode) {
  table.parentNode.removeChild(table);
}
delete this.dataTableDiv;
}
});

这段代码呈现图表,如果你想将表格和图表一起导出到pdf,你需要改变设置并显示图表。我删除了隐藏图表的代码,并保留图表的默认设置。

Highcharts.chart('container', {
  chart: {
    width: 800,
    height: 1000,
  },
  title: {
    text: 'My custom table chart',
    style: {
      display: 'none'
    }
  },
  subtitle: {
    text: null,
    align: 'left'
  },
  series: [{
    name: 'Installation',
    data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
  }, {
    name: 'Manufacturing',
    data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
  }, {
    name: 'Sales & Distribution',
    data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
  }, {
    name: 'Project Development',
    data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
  }, {
    name: 'Other',
    data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
  }],
  exporting: {
    showTable: true,
    allowHTML: true
  }
});

相关问题