簡體   English   中英

將 Highcharts 數據表導出到 PDF - Angular

[英]Export Highcharts DataTable to PDF - Angular

我在我的 angular 應用程序中使用 Highchart。

我想格式化 Highcharts 數據表並將其與圖表一起導出到 PDF。

在stackbliz中復制了相同的內容。

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

此處下載的 PDF 僅包含圖表。

I tried to implement < https://jsfiddle.net/Ld561nbt/4/> mentioned in Official Highchart page https://api.highcharts.com/highcharts/exporting.showTable?_ga=2.151177465.1965572452.1639477244-32713185.1637664871 in angular but no運氣。

誰能建議我如何在導出 PDF 中包含 Highchart 數據表

.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' }
    );
  }
}

該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
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM