import {
Component, Input, OnChanges, ChangeDetectionStrategy, SimpleChanges
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
selector: 'ngx-charts-scale-legend',
template: `
{{ valueRange[1].toLocaleString() }}
{{ valueRange[0].toLocaleString() }}
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ScaleLegendComponent implements OnChanges {
@Input() valueRange;
@Input() colors;
@Input() height;
@Input() width;
gradient: any;
constructor(private sanitizer: DomSanitizer) { }
ngOnChanges(changes: SimpleChanges): void {
const gradientValues = this.gradientString(this.colors.range(), this.colors.domain());
this.gradient = this.sanitizer.bypassSecurityTrustStyle(`linear-gradient(to bottom, ${gradientValues})`);
}
/**
* Generates the string used in the gradient stylesheet properties
* @param {array} colors array of colors
* @param {array} splits array of splits on a scale of (0, 1)
* @return {string}
*/
gradientString(colors, splits): string {
// add the 100%
splits.push(1);
let pairs = [];
colors.reverse().forEach((c, i) => {
pairs.push(`${c} ${Math.round(splits[i] * 100)}%`);
});
return pairs.join(', ');
}
}