|
| 1 | +import { Component, Input, OnChanges, SimpleChanges, Output, EventEmitter } from '@angular/core'; |
| 2 | +import {TimelineItem} from '../../types/utm-timeline-item' |
| 3 | + |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'utm-timeline', |
| 7 | + templateUrl: './utm-timeline.component.html', |
| 8 | + styleUrls: ['./utm-timeline.component.scss'] |
| 9 | +}) |
| 10 | +export class UtmTimeLineComponent implements OnChanges { |
| 11 | + @Input() items: TimelineItem[] = []; |
| 12 | + @Input() title: string = ''; |
| 13 | + @Output() itemClick = new EventEmitter<TimelineItem>(); |
| 14 | + |
| 15 | + chartOption: any = {}; |
| 16 | + |
| 17 | + ngOnChanges(changes: SimpleChanges): void { |
| 18 | + if ((this.items.length || 0) > 0) { |
| 19 | + this.buildChart(); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + private buildChart() { |
| 24 | + const groups: Record<number, TimelineItem[]> = {}; |
| 25 | + this.items.forEach(item => { |
| 26 | + const ts = new Date(item.startDate).getTime(); |
| 27 | + if (!groups[ts]) groups[ts] = []; |
| 28 | + groups[ts].push(item); |
| 29 | + }); |
| 30 | + |
| 31 | + const seriesData: any[] = []; |
| 32 | + Object.keys(groups).forEach(tsStr => { |
| 33 | + const ts = Number(tsStr); |
| 34 | + const group = groups[ts]; |
| 35 | + |
| 36 | + group.forEach((item, idx) => { |
| 37 | + seriesData.push({ |
| 38 | + value: [ |
| 39 | + ts, // index 0: timestamp |
| 40 | + 0, // index 1: Y coordinate |
| 41 | + item.name, // index 2: name |
| 42 | + new Date(item.startDate).toLocaleString(), // index 3:formatted date |
| 43 | + !!item.iconUrl?item.iconUrl:'', // index 4: offsetIndex |
| 44 | + idx, // index 5: offsetIndex |
| 45 | + group.length // index 6: groupSize |
| 46 | + ], |
| 47 | + itemData: item |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + this.chartOption = { |
| 53 | + title: { |
| 54 | + text: this.title, |
| 55 | + left: 'center', |
| 56 | + textStyle: { |
| 57 | + fontSize: 16, |
| 58 | + fontWeight: 'bold' |
| 59 | + } |
| 60 | + }, |
| 61 | + grid:{ |
| 62 | + left:0, |
| 63 | + right:0 |
| 64 | + }, |
| 65 | + tooltip: { |
| 66 | + trigger: 'item', |
| 67 | + formatter: (params: any) => |
| 68 | + `<b>${params.data.value[2]}</b><br/>${params.data.value[3]}` |
| 69 | + }, |
| 70 | + xAxis: { |
| 71 | + type: 'time', |
| 72 | + //let a space between init of the timeline and the first/last element so they can be showed |
| 73 | + min: (value: any) => { |
| 74 | + return value.min - 5 * 1000; |
| 75 | + }, |
| 76 | + max: (value: any) => { |
| 77 | + return value.max + 10 * 1000; |
| 78 | + }, |
| 79 | + axisLabel: { |
| 80 | + formatter: (val: number) => { |
| 81 | + const date = new Date(val) |
| 82 | + return `${date.getHours()%12}:${date.getMinutes() < 10 ? '0' : ''}${date.getMinutes()}:${date.getSeconds() < 10 ? '0' : ''}${date.getSeconds()} \n ${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()} ` |
| 83 | + } |
| 84 | + }, |
| 85 | + }, |
| 86 | + yAxis: { show: false, type: 'value'}, |
| 87 | + series: [ |
| 88 | + { |
| 89 | + type: 'custom', |
| 90 | + data: seriesData, |
| 91 | + renderItem: (params: any, api: any) => { |
| 92 | + const timestamp = api.value(0); |
| 93 | + const coord = api.coord([timestamp, 0]); |
| 94 | + |
| 95 | + const cardHeight = 60; |
| 96 | + const spacing = 10; |
| 97 | + const offsetIndex = api.value(5) || 0; |
| 98 | + const totalOffset = (cardHeight + spacing) * offsetIndex + 80; |
| 99 | + |
| 100 | + const name = api.value(2); |
| 101 | + const date = api.value(3); |
| 102 | + const iconUrl = api.value(4); |
| 103 | + |
| 104 | + const iconPadding = 0; |
| 105 | + |
| 106 | + return { |
| 107 | + type: 'group', |
| 108 | + children: [ |
| 109 | + { |
| 110 | + type: 'rect', |
| 111 | + shape: { |
| 112 | + x: coord[0] - 75, |
| 113 | + y: coord[1] - totalOffset - cardHeight, |
| 114 | + width: 250, |
| 115 | + height: cardHeight, |
| 116 | + r: 8 |
| 117 | + }, |
| 118 | + style: { |
| 119 | + fill: '#fefefe', |
| 120 | + stroke: '#0277bd', |
| 121 | + lineWidth: 1, |
| 122 | + shadowBlur: 8, |
| 123 | + shadowColor: 'rgba(0,0,0,0.2)', |
| 124 | + cursor: 'pointer', |
| 125 | + overflow:'hidden' |
| 126 | + } |
| 127 | + }, |
| 128 | + { |
| 129 | + type: 'image', |
| 130 | + style: { |
| 131 | + image: iconUrl, |
| 132 | + x: coord[0] - 75 + iconPadding +1.5, |
| 133 | + y: coord[1] - totalOffset - cardHeight+1.5, |
| 134 | + width: cardHeight-2.5, |
| 135 | + height:cardHeight-2.5, |
| 136 | + r: 8 |
| 137 | + } |
| 138 | + }, |
| 139 | + { |
| 140 | + type: 'text', |
| 141 | + style: { |
| 142 | + x: coord[0] - 75 + iconPadding + cardHeight + 10, |
| 143 | + y: coord[1] - totalOffset - cardHeight + 20, |
| 144 | + text:this.truncateText(name,150), |
| 145 | + textAlign: 'left', |
| 146 | + fill: '#000', |
| 147 | + fontSize: 13, |
| 148 | + fontWeight: 600, |
| 149 | + width: 150 - cardHeight - iconPadding * 2 - 10, |
| 150 | + overflow: 'break', |
| 151 | + ellipsis: '...' |
| 152 | + } |
| 153 | + }, |
| 154 | + { |
| 155 | + type: 'text', |
| 156 | + style: { |
| 157 | + x: coord[0] - 75 + iconPadding + cardHeight + 10, |
| 158 | + y: coord[1] - totalOffset - cardHeight + 40, |
| 159 | + text: date, |
| 160 | + textAlign: 'left', |
| 161 | + fill: '#666', |
| 162 | + fontSize: 10 |
| 163 | + } |
| 164 | + }, |
| 165 | + { |
| 166 | + type: 'line', |
| 167 | + shape: { |
| 168 | + x1: coord[0], |
| 169 | + y1: coord[1] - totalOffset, |
| 170 | + x2: coord[0], |
| 171 | + y2: coord[1] |
| 172 | + }, |
| 173 | + style: { stroke: '#0277bd', lineWidth: 1.5 } |
| 174 | + } |
| 175 | + ] |
| 176 | + }; |
| 177 | + }, |
| 178 | + encode: { x: 0, y: 1 } |
| 179 | + } |
| 180 | + ], |
| 181 | + dataZoom: [ |
| 182 | + { type: 'slider', xAxisIndex: 0 }, |
| 183 | + { type: 'inside', xAxisIndex: 0 } |
| 184 | + ] |
| 185 | + }; |
| 186 | + } |
| 187 | + |
| 188 | + onChartClick(event: any): void { |
| 189 | + if (event.data && event.data.itemData) { |
| 190 | + this.itemClick.emit(event.data.itemData.metadata); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + truncateText(text: string, maxWidth: number) { |
| 195 | + const avgCharWidth = 7; |
| 196 | + const maxChars = Math.floor(maxWidth / avgCharWidth); |
| 197 | + return text.length > maxChars ? text.substring(0, maxChars - 3) + '...' : text; |
| 198 | +}; |
| 199 | + |
| 200 | +} |
0 commit comments