I am using Highchart in my angular app.
I want to format a Highcharts dataTable and export it along with chart to PDF.
Replicated the same in stackbliz.
https://stackblitz.com/edit/angular-ivy-u9zvps?file=src%2Fapp%2Fapp.component.ts
Here the PDF download contains only chart.
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 luck.
Can anyone suggest me how to include Highchart dataTable in export PDF
.HTML content
<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' }
);
}
}
Example from that jsfiddle use trick to have posibility to export table. Added the table as the subtitle to make it part of the export.
Live demo: https://jsfiddle.net/BlackLabel/5rwnsmz7/
At this part table is creating and added as subtitle.
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;
}
});
This code render chart and if you want to export table and chart to pdf together you need to change settings and show the chart. I remove the code responsible for hiding chart and leave defaults sets for charts.
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
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.