This also adds a template in the table component.
Signed-off-by: Tiago Melo <tmelo@suse.com>
import { ChartsModule } from 'ng2-charts/ng2-charts';
import { AlertModule } from 'ngx-bootstrap';
+import { SparklineComponent } from './sparkline/sparkline.component';
import { ViewCacheComponent } from './view-cache/view-cache.component';
@NgModule({
AlertModule.forRoot(),
ChartsModule
],
- declarations: [ViewCacheComponent],
+ declarations: [
+ ViewCacheComponent,
+ SparklineComponent
+ ],
providers: [],
exports: [
- ViewCacheComponent
+ ViewCacheComponent,
+ SparklineComponent
]
})
export class ComponentsModule { }
--- /dev/null
+<div class="chart-container"
+ [ngStyle]="style">
+ <canvas baseChart
+ [labels]="labels"
+ [datasets]="datasets"
+ [options]="options"
+ [colors]="colors"
+ [chartType]="'line'">
+ </canvas>
+</div>
--- /dev/null
+.chart-container {
+ position: relative;
+ margin: auto;
+}
--- /dev/null
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { AppModule } from '../../../app.module';
+import { SparklineComponent } from './sparkline.component';
+
+describe('SparklineComponent', () => {
+ let component: SparklineComponent;
+ let fixture: ComponentFixture<SparklineComponent>;
+
+ beforeEach(
+ async(() => {
+ TestBed.configureTestingModule({
+ imports: [AppModule]
+ }).compileComponents();
+ })
+ );
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(SparklineComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
--- /dev/null
+import { Component, OnChanges, OnInit, SimpleChanges } from '@angular/core';
+import { Input } from '@angular/core';
+
+@Component({
+ selector: 'cd-sparkline',
+ templateUrl: './sparkline.component.html',
+ styleUrls: ['./sparkline.component.scss']
+})
+export class SparklineComponent implements OnInit, OnChanges {
+ @Input() data: any;
+ @Input()
+ style = {
+ height: '30px',
+ width: '100px'
+ };
+
+ public colors: Array<any> = [
+ {
+ backgroundColor: 'rgba(40,140,234,0.2)',
+ borderColor: 'rgba(40,140,234,1)',
+ pointBackgroundColor: 'rgba(40,140,234,1)',
+ pointBorderColor: '#fff',
+ pointHoverBackgroundColor: '#fff',
+ pointHoverBorderColor: 'rgba(40,140,234,0.8)'
+ }
+ ];
+
+ options = {
+ animation: {
+ duration: 0
+ },
+ responsive: true,
+ maintainAspectRatio: false,
+ legend: {
+ display: false
+ },
+ elements: {
+ line: {
+ borderWidth: 1
+ }
+ },
+ tooltips: {
+ enabled: true
+ },
+ scales: {
+ yAxes: [
+ {
+ display: false
+ }
+ ],
+ xAxes: [
+ {
+ display: false
+ }
+ ]
+ }
+ };
+
+ public datasets: Array<any> = [
+ {
+ data: []
+ }
+ ];
+
+ public labels: Array<any> = [];
+
+ constructor() {}
+
+ ngOnInit() {}
+
+ ngOnChanges(changes: SimpleChanges) {
+ this.datasets[0].data = changes['data'].currentValue;
+ this.datasets = [...this.datasets];
+ this.labels = [...Array(changes['data'].currentValue.length)];
+ }
+}
import { NgxDatatableModule } from '@swimlane/ngx-datatable';
+import { ComponentsModule } from '../../components/components.module';
import { TableComponent } from '../table/table.component';
import { TableKeyValueComponent } from './table-key-value.component';
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TableComponent, TableKeyValueComponent ],
- imports: [ FormsModule, NgxDatatableModule ]
+ imports: [ FormsModule, NgxDatatableModule, ComponentsModule ]
})
.compileComponents();
}));
*ngIf="toolHeader">
<!-- actions -->
<div class="oadatatableactions">
- <ng-content select="table-actions"></ng-content>
+ <ng-content select="table-actions"></ng-content>
</div>
<!-- end actions -->
</ngx-datatable>
</div>
<ng-template cdTableDetails></ng-template>
+
<!-- cell templates that can be accessed from outside -->
<ng-template #tableCellBoldTpl
let-row="row"
let-value="value">
<strong>{{ value }}</strong>
</ng-template>
+
+<ng-template #sparklineTpl
+ let-value="value">
+ <cd-sparkline [data]="value"></cd-sparkline>
+</ng-template>
import { NgxDatatableModule, TableColumn } from '@swimlane/ngx-datatable';
+import { ComponentsModule } from '../../components/components.module';
import { TableComponent } from './table.component';
describe('TableComponent', () => {
async(() => {
TestBed.configureTestingModule({
declarations: [TableComponent],
- imports: [NgxDatatableModule, FormsModule]
+ imports: [NgxDatatableModule, FormsModule, ComponentsModule]
}).compileComponents();
})
);
@ViewChild(DatatableComponent) table: DatatableComponent;
@ViewChild(TableDetailsDirective) detailTemplate: TableDetailsDirective;
@ViewChild('tableCellBoldTpl') tableCellBoldTpl: TemplateRef<any>;
+ @ViewChild('sparklineTpl') sparklineTpl: TemplateRef<any>;
// This is the array with the items to be shown.
@Input() data: any[];
_addTemplates () {
this.cellTemplates.bold = this.tableCellBoldTpl;
+ this.cellTemplates.sparkline = this.sparklineTpl;
}
ngOnChanges(changes) {
export enum CellTemplate {
- bold = 'bold'
+ bold = 'bold',
+ sparkline = 'sparkline'
}