import Highcharts from 'highcharts' import { highchartsTheme } from '../pb_dashboard/pbChartsLightTheme.js' require('highcharts/modules/variable-pie')(Highcharts) class pbChart { defaults = { callbackInitializeBefore: () => {}, callbackInitializeAfter: () => {}, callbackRefreshBefore: () => {}, callbackRefreshAfter: () => {}, callbackDestroyBefore: () => {}, callbackDestroyAfter: () => {}, property: 'Value', } extendDefaults(defaults, options) { for (const property in options) { if (Object.prototype.hasOwnProperty.call(options, property)) { defaults[property] = options[property] } } return defaults } constructor(element, options) { this.element = element this.options = options this.settings = this.extendDefaults(this.defaults, options) if (this.options.type == 'variablepie' || this.options.type == 'pie'){ this.setupPieChart() } else { this.setupChart() } } setupPieChart() { Highcharts.setOptions(highchartsTheme) Highcharts.chart(this.defaults.id, { title: { text: this.defaults.title, }, chart: { type: this.defaults.type, }, plotOptions: { pie: { dataLabels: { enabled: this.defaults.dataLabels, connectorShape: 'straight', connectorWidth: 3, format: this.defaults.dataLabelHtml, }, showInLegend: this.defaults.showInLegend, }, }, tooltip: { headerFormat: this.defaults.headerFormat, pointFormat: this.defaults.tooltipHtml, useHTML: this.defaults.useHTML, }, series: [{ minPointSize: this.defaults.minPointSize, maxPointSize: this.defaults.maxPointSize, innerSize: this.defaults.innerSize, data: this.defaults.chartData, zMin: this.defaults.zMin, startAngle: this.defaults.startAngle, }], credits: false, }) } setupChart() { Highcharts.setOptions(highchartsTheme) Highcharts.chart(this.defaults.id, { title: { text: this.defaults.title, }, chart: { type: this.defaults.type, }, subtitle: { text: this.defaults.subtitle, }, yAxis: { min: this.defaults.yAxisMin, max: this.defaults.yAxisMax, title: { text: this.defaults.axisTitle, }, }, xAxis: { categories: this.defaults.xAxisCategories, }, plotOptions: { series: { pointStart: this.defaults.pointStart, dataLabels: { enabled: false, }, }, }, series: this.defaults.chartData, credits: false, }) } refresh(silent = false) { if (!silent) { this.settings.callbackRefreshBefore.call() } this.this.destroy(silent) this.this.initialize(silent) if (!silent) { this.settings.callbackRefreshAfter.call() } } destroy(silent = false) { if (!silent) { this.settings.callbackDestroyBefore.call() } if (!silent) { this.settings.callbackDestroyAfter.call() } } refreshSilently() { this.this.refresh(true) } destroySilently() { this.this.destroy(true) } } export default pbChart