1 import { Component, ElementRef, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core';
2 import { Input } from '@angular/core';
4 import { ChartTooltip } from '../../models/chart-tooltip';
5 import { DimlessBinaryPipe } from '../../pipes/dimless-binary.pipe';
8 selector: 'cd-sparkline',
9 templateUrl: './sparkline.component.html',
10 styleUrls: ['./sparkline.component.scss']
12 export class SparklineComponent implements OnInit, OnChanges {
13 @ViewChild('sparkCanvas')
14 chartCanvasRef: ElementRef;
15 @ViewChild('sparkTooltip')
16 chartTooltipRef: ElementRef;
28 public colors: Array<any> = [
30 backgroundColor: 'rgba(40,140,234,0.2)',
31 borderColor: 'rgba(40,140,234,1)',
32 pointBackgroundColor: 'rgba(40,140,234,1)',
33 pointBorderColor: '#fff',
34 pointHoverBackgroundColor: '#fff',
35 pointHoverBorderColor: 'rgba(40,140,234,0.8)'
44 maintainAspectRatio: false,
59 label: (tooltipItem) => {
61 return this.dimlessBinaryPipe.transform(tooltipItem.yLabel);
63 return tooltipItem.yLabel;
82 public datasets: Array<any> = [
88 public labels: Array<any> = [];
90 constructor(private dimlessBinaryPipe: DimlessBinaryPipe) {}
93 const getStyleTop = (tooltip) => {
94 return tooltip.caretY - tooltip.height - tooltip.yPadding - 5 + 'px';
97 const getStyleLeft = (tooltip, positionX) => {
98 return positionX + tooltip.caretX + 'px';
101 const chartTooltip = new ChartTooltip(
103 this.chartTooltipRef,
108 chartTooltip.customColors = {
109 backgroundColor: this.colors[0].pointBackgroundColor,
110 borderColor: this.colors[0].pointBorderColor
113 this.options.tooltips.custom = (tooltip) => {
114 chartTooltip.customTooltips(tooltip);
118 ngOnChanges(changes: SimpleChanges) {
119 this.datasets[0].data = changes['data'].currentValue;
120 this.labels = [...Array(changes['data'].currentValue.length)];