@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
176 lines (157 loc) • 4.45 kB
text/typescript
import {
Component,
Input,
Output,
EventEmitter,
OnChanges,
trigger,
style,
transition,
SimpleChanges,
animate,
ChangeDetectionStrategy
} from '@angular/core';
import { formatLabel } from '../common/label.helper';
export class SeriesHorizontal implements OnChanges {
bars: any;
x: any;
y: any;
dims;
type = 'standard';
series;
xScale;
yScale;
colors;
gradient: boolean;
activeEntries: any[];
select = new EventEmitter();
activate = new EventEmitter();
deactivate = new EventEmitter();
ngOnChanges(changes: SimpleChanges): void {
this.update();
}
update(): void {
let d0 = 0;
let total;
if (this.type === 'normalized') {
total = this.series.map(d => d.value).reduce((sum, d) => { return sum + d; }, 0);
}
this.bars = this.series.map((d, index) => {
let value = d.value;
let label = d.name;
const formattedLabel = formatLabel(label);
const roundEdges = this.type === 'standard';
let bar: any = {
value,
label,
roundEdges,
data: d,
formattedLabel
};
bar.height = this.yScale.bandwidth();
if (this.type === 'standard') {
bar.width = Math.abs(this.xScale(value) - this.xScale(0));
if (value < 0) {
bar.x = this.xScale(value);
} else {
bar.x = this.xScale(0);
}
bar.y = this.yScale(label);
} else if (this.type === 'stacked') {
let offset0 = d0;
let offset1 = offset0 + value;
d0 += value;
bar.width = this.xScale(offset1) - this.xScale(offset0);
bar.x = this.xScale(offset0);
bar.y = 0;
bar.offset0 = offset0;
bar.offset1 = offset1;
} else if (this.type === 'normalized') {
let offset0 = d0;
let offset1 = offset0 + value;
d0 += value;
if (total > 0) {
offset0 = (offset0 * 100) / total;
offset1 = (offset1 * 100) / total;
} else {
offset0 = 0;
offset1 = 0;
}
bar.width = this.xScale(offset1) - this.xScale(offset0);
bar.x = this.xScale(offset0);
bar.y = 0;
bar.offset0 = offset0;
bar.offset1 = offset1;
value = (offset1 - offset0).toFixed(2) + '%';
}
if (this.colors.scaleType === 'ordinal') {
bar.color = this.colors.getColor(label);
} else {
if (this.type === 'standard') {
bar.color = this.colors.getColor(value);
bar.gradientStops = this.colors.getLinearGradientStops(value);
} else {
bar.color = this.colors.getColor(bar.offset1);
bar.gradientStops = this.colors.getLinearGradientStops(bar.offset1, bar.offset0);
}
}
bar.tooltipText = `
<span class="tooltip-label">${formattedLabel}</span>
<span class="tooltip-val">${value.toLocaleString()}</span>
`;
return bar;
});
}
isActive(entry): boolean {
if(!this.activeEntries) return false;
let item = this.activeEntries.find(d => {
return entry.name === d.name && entry.series === d.series;
});
return item !== undefined;
}
trackBy(index, bar) {
return bar.label;
}
click(data): void {
this.select.emit(data);
}
}