/
BarChart.component.ts
97 lines (79 loc) · 3.16 KB
/
BarChart.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Component, OnInit, Input, Output, EventEmitter, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import * as bar from 'britecharts/dist/umd/bar.min';
import * as d3Selection from 'd3-selection';
import * as miniTooltip from 'britecharts/dist/umd/miniTooltip.min';
import * as colors from 'britecharts/dist/umd/colors.min';
@Component({
selector: 'ngx-bc-barchart',
templateUrl: './BarChart.component.html'
})
export class BarChartComponent implements OnInit {
@Input() data: any;
@Input() chartConfig: any;
@Input() exportAsImageEvt: Observable<any>;
@Output() ready: EventEmitter<boolean> = new EventEmitter<boolean>();
private el: HTMLElement;
public bar: any;
public tooltip: any;
public tooltipContainer: any;
constructor(elementRef: ElementRef) {
Observable.fromEvent(window, 'resize')
.debounceTime(250)
.subscribe(() => {
this.redrawChart();
});
this.el = elementRef.nativeElement;
}
ngOnInit() {
this.drawChart();
let that = this;
if (this.exportAsImageEvt) {
this.exportAsImageEvt.subscribe(data => {
that.bar.exportChart(data['filename'], data['title']);
});
}
}
private drawChart() {
this.bar = bar();
this.tooltip = miniTooltip();
let barContainer = d3Selection.select(this.el).select('.bar-chart-container'),
containerWidth = barContainer.node() ? barContainer.node().getBoundingClientRect().width : false;
if (containerWidth) {
this.bar.width(containerWidth);
this.bar.shouldReverseColorList(false);
for (let option in this.chartConfig['properties']) {
if (this.bar.hasOwnProperty(option) && option !== 'colorSchema') {
this.bar[option](this.chartConfig['properties'][option]);
}
}
let showTooltip = false;
if (this.chartConfig.hasOwnProperty('showTooltip') && this.chartConfig['showTooltip'] === true) {
showTooltip = true;
this.bar.on('customMouseOver', this.tooltip.show);
this.bar.on('customMouseMove', this.tooltip.update);
this.bar.on('customMouseOut', this.tooltip.hide);
}
if (this.chartConfig.hasOwnProperty('colors')) {
if (this.chartConfig['colors'].hasOwnProperty('colorSchema')) {
if (colors.colorSchemas.hasOwnProperty(this.chartConfig['colors']['colorSchema'])) {
this.bar.colorSchema(colors.colorSchemas[this.chartConfig['colors']['colorSchema']]);
}
} else if (this.chartConfig['colors'].hasOwnProperty('customSchema')) {
this.bar.colorSchema(this.chartConfig['colors']['customSchema']);
}
}
barContainer.datum(this.data).call(this.bar);
if (this.chartConfig.hasOwnProperty('click')) {
d3Selection.select(this.el).selectAll('.bar-chart .bar').on('click', (ev) => this.chartConfig['click'](ev));
}
this.tooltipContainer = d3Selection.select(this.el).select('.bar-chart-container .metadata-group');
this.tooltipContainer.datum(this.data).call(this.tooltip);
this.ready.emit(true);
}
}
public redrawChart() {
d3Selection.select(this.el).selectAll('.bar-chart').remove();
this.drawChart();
}
}