export class ChartSfComponent implements AfterViewInit, OnInit {
@Input()
public chartInput: Subject<RealChartItem>;
@Input()
public isChartLoading: Subject<LoadingHelper>;
@Input()
public height: number;
@Input()
public isCustomLegend: boolean;
@Input()
public tooltipFormat: Object;
@Input()
public labelRotate: string;
@Input()
public clearChart: Subject<string>;
public isLoading: LoadingHelper = 0;
public realChartItem: RealChartItem = new RealChartItem();
//Chart variables
@ViewChild("chart")
public chart: Chart;
public primaryXAxis: Object;
public titleStyle: Object;
public legendSettings: Object;
public title: string;
public palette: string[];
private width: number;
private numOfChars: number;
public tooltip: Object;
//Constructor
constructor() {}
ngOnInit(): void {
this.titleStyle = {
textAlignment: "Near",
textOverflow: "Wrap"
};
this.primaryXAxis = {
valueType: "Category",
interval: 1,
majorGridLines: { width: 0 },
labelIntersectAction: this.labelRotate
};
this.tooltip = {
enable: true,
/* template:
"<div class='d-flex-column bg-transparent text-white border border-secondary' style='width:8rem; height: 4rem;'><h6 class='d-flex justify-content-center align-items-center no-padding'>${point.tooltip}</h6><div class='d-flex justify-content-center align-items-center'><hr class='no-padding' style='width: 75%; height:1px; background-color:white;'></hr></div><h6 class='d-flex justify-content-center align-items-center no-padding'>${y}</h6><div style='position: absolute; top:0; left:0; right:0; bottom:0; background-color:black; opacity: 0.75; z-index:-1;'></div></div>" */
};
if (this.isCustomLegend)
if (this.chart != undefined)
this.chart.resized.subscribe(data => {
this.legendSettings = {
visible: true,
position: "Custom",
location: {
x: data.currentSize.width - this.numOfChars * 10.5,
y: 15
}
};
});
if (isNullOrUndefined(this.isCustomLegend)) this.isCustomLegend = true;
this.updateChart();
}
updateChart() {
if (
isNullOrUndefined(this.chart) ||
isNullOrUndefined(this.realChartItem) ||
isNullOrUndefined(this.realChartItem.itemSource)
) {
return;
}
this.title = this.realChartItem.legend;
this.palette = this.realChartItem.pallete;
if (this.chart != undefined) {
this.chart.series = [];
let series = [];
this.numOfChars = 0;
this.realChartItem.itemSource.forEach(item => {
let model = {
dataSource: item.chartItems,
xName: "month",
yName: "value",
type: ChartStyle[item.style],
legendShape: "Circle",
name: item.title,
tooltipMappingName: item.title
};
this.numOfChars +=
item.title.length * 14 + 40 / this.realChartItem.itemSource.length;
series.push(model);
});
if (this.isCustomLegend)
this.legendSettings = {
visible: true,
position: "Custom",
location: {
x: this.width - this.numOfChars,
y: 15
}
};
setTimeout(() => {
this.chart.series = series;
this.chart.refresh();
}, 250);
}
}
loaded(event) {
this.width = event.chart.availableSize.width;
}
ngAfterViewInit(): void {
this.chartInput.subscribe(chartItem => {
this.realChartItem = chartItem;
this.updateChart();
});
this.isChartLoading.subscribe(state => {
this.isLoading = state;
});
this.clearChart.subscribe(clear => {
this.chart.series = new Array();
this.chart.refresh();
});
}
}