@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
61 lines (52 loc) • 1.64 kB
text/typescript
import {
Component, Input, OnChanges, ChangeDetectionStrategy, SimpleChanges
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
export class ScaleLegendComponent implements OnChanges {
valueRange;
colors;
height;
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(', ');
}
}