|
| 1 | +import 'chartjs-chart-treemap'; |
| 2 | + |
| 3 | +import { ChartEvent, InteractionItem } from 'chart.js'; |
| 4 | + |
| 5 | +import { AggregatedHotspot } from '../../model/hotspot-result'; |
| 6 | +import { TreeMapChartConfig } from '../../ui/treemap/treemap.component'; |
| 7 | + |
| 8 | +type HotspotDataSet = { |
| 9 | + tree: AggregatedHotspot[]; |
| 10 | +}; |
| 11 | + |
| 12 | +export function toTreeMapConfig( |
| 13 | + aggregated: AggregatedHotspot[] |
| 14 | +): TreeMapChartConfig { |
| 15 | + const values = aggregated.flatMap((v) => [ |
| 16 | + { ...v, count: v.countHotspot, type: 'hotspot' }, |
| 17 | + { ...v, count: v.countWarning, type: 'warning' }, |
| 18 | + { ...v, count: v.countOk, type: 'fine' }, |
| 19 | + ]); |
| 20 | + |
| 21 | + const options = { |
| 22 | + onClick: (_event: ChartEvent, elements: InteractionItem[]) => { |
| 23 | + if (elements.length > 0) { |
| 24 | + const element = elements[elements.length - 1]; |
| 25 | + const dataIndex = element.index; |
| 26 | + const dataset = config.data.datasets[0] as unknown as HotspotDataSet; |
| 27 | + const tree = dataset.tree; |
| 28 | + const item = tree[dataIndex]; |
| 29 | + console.log('item', item); |
| 30 | + } |
| 31 | + }, |
| 32 | + onHover: (event: ChartEvent, elements: InteractionItem[]) => { |
| 33 | + const chartElement = event.native?.target as HTMLCanvasElement; |
| 34 | + if (elements.length >= 2) { |
| 35 | + chartElement.style.cursor = 'pointer'; |
| 36 | + } else { |
| 37 | + chartElement.style.cursor = 'default'; |
| 38 | + } |
| 39 | + }, |
| 40 | + plugins: { |
| 41 | + title: { |
| 42 | + display: true, |
| 43 | + text: 'Hotspots', |
| 44 | + }, |
| 45 | + legend: { |
| 46 | + display: false, |
| 47 | + }, |
| 48 | + tooltip: { |
| 49 | + callbacks: { |
| 50 | + title() { |
| 51 | + return 'File Count'; |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + const config: TreeMapChartConfig = { |
| 59 | + type: 'treemap', |
| 60 | + data: { |
| 61 | + datasets: [ |
| 62 | + { |
| 63 | + data: values, |
| 64 | + key: 'count', |
| 65 | + groups: ['parent', 'module', 'type'], |
| 66 | + spacing: 1, |
| 67 | + borderWidth: 0.5, |
| 68 | + borderColor: '#EFEFEF', |
| 69 | + // backgroundColor: 'rgba(220,230,220,0.3)', |
| 70 | + backgroundColor: (ctx) => { |
| 71 | + if (typeof ctx.raw?.l !== 'undefined' && ctx.raw?.l < 2) { |
| 72 | + return '#EFEFEF'; |
| 73 | + } |
| 74 | + |
| 75 | + switch (ctx.raw?.g) { |
| 76 | + case 'hotspot': |
| 77 | + return '#E74C3C'; |
| 78 | + case 'warning': |
| 79 | + return '#F1C40F'; |
| 80 | + case 'fine': |
| 81 | + return '#2ECC71'; |
| 82 | + } |
| 83 | + |
| 84 | + return 'gray'; |
| 85 | + }, |
| 86 | + // hoverBackgroundColor: 'rgba(220,230,220,0.5)', |
| 87 | + captions: { |
| 88 | + align: 'center', |
| 89 | + display: true, |
| 90 | + color: 'black', |
| 91 | + font: { |
| 92 | + size: 14, |
| 93 | + }, |
| 94 | + hoverFont: { |
| 95 | + size: 16, |
| 96 | + weight: 'bold', |
| 97 | + }, |
| 98 | + padding: 5, |
| 99 | + }, |
| 100 | + labels: { |
| 101 | + display: false, |
| 102 | + overflow: 'hidden', |
| 103 | + }, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + options: options, |
| 108 | + }; |
| 109 | + |
| 110 | + return config; |
| 111 | +} |
0 commit comments