UNPKG

1.84 kBPlain TextView Raw
1import { Component } from '@angular/core';
2
3// Import dependencies
4import moment from 'moment';
5import * as d3 from 'd3';
6
7@Component({
8 selector: 'my-app',
9 template: `
10 <calendar-heatmap
11 [data]="data"
12 [color]="color"
13 [overview]="overview"
14 (handler)="print($event)"
15 (onChange)="handleOnChange($event)">
16 </calendar-heatmap>
17 `,
18})
19export class AppComponent {
20
21 // Initialize random data for the demo
22 private now = moment().endOf('day').toDate();
23 private time_ago = moment().startOf('day').subtract(10, 'year').toDate();
24 data = d3.timeDays(this.time_ago, this.now).map((dateElement: any, index: number) => {
25 return {
26 date: dateElement,
27 details: Array.apply(null, new Array(Math.floor(Math.random() * 15))).map((e: number, i: number, arr: any) => {
28 return {
29 'name': 'Project ' + Math.ceil(Math.random() * 10),
30 'date': function () {
31 var projectDate = new Date(dateElement.getTime());
32 projectDate.setHours(Math.floor(Math.random() * 24))
33 projectDate.setMinutes(Math.floor(Math.random() * 60));
34 return projectDate;
35 }(),
36 'value': 3600 * ((arr.length - i) / 5) + Math.floor(Math.random() * 3600) * Math.round(Math.random() * (index / 365))
37 }
38 }),
39 init: function () {
40 this.total = this.details.reduce((prev: number, e: any) => {
41 return prev + e.value;
42 }, 0);
43 return this;
44 }
45 }.init();
46 });
47
48 // Set custom color for the calendar heatmap
49 color = '#cd2327';
50
51 // Set overview type (choices are year, month and day)
52 overview = 'year';
53
54 // Click handler function
55 print(val: object):void {
56 console.log(val);
57 }
58
59 // On change handler
60 handleOnChange(val: object):void {
61 console.log('onChange', val)
62 }
63}