Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Component, ElementRef, input, OnChanges, ViewChild } from '@angular/core'; import { PerformanceDataItem } from '@reflex/shared-types'; import * as d3 from 'd3'; @Component({ selector: 'app-performance-visualization', templateUrl: './performance-visualization.component.html', styleUrls: ['./performance-visualization.component.scss'] }) export class PerformanceVisualizationComponent implements OnChanges { @ViewChild('panel') public d3Panel!: ElementRef; public readonly data = input<Array<PerformanceDataItem>>([]); public readonly groups = input<Array<string>>([]); public readonly visId = input('defaultVis'); public readonly refreshRate = input(10); public readonly numSamples = input(200); public readonly isProcessingGraph = input(false); public width = 200; public height = 150; private showTooltip = false; private mousePosition = 0; private updateIdx = 0; private average = 0; public ngOnChanges(): void { this.updateIdx++; Iif (this.updateIdx % this.refreshRate() !== 0) { return; } // skip changes accroding to refresh rate this.updateIdx = 0; const entries = this.data().length; const isProcessingGraph = this.isProcessingGraph(); if (isProcessingGraph) { // const prep = this.data.map((item) => item.processingPreparation ?? 0).reduce((prev, curr) => prev + curr, 0) / entries; // const upd = this.data.map((item) => item.processingUpdate ?? 0).reduce((prev, curr) => prev + curr, 0) / entries; // const conv = this.data.map((item) => item.processingConvert ?? 0).reduce((prev, curr) => prev + curr, 0) / entries; // const smooth = this.data.map((item) => item.processingSmoothing ?? 0).reduce((prev, curr) => prev + curr, 0) / entries; // const extr = this.data.map((item) => item.processingExtremum ?? 0).reduce((prev, curr) => prev + curr, 0) / entries; this.average = entries > 0 ? this.data().map((item) => item.totalProcessing ?? 0).reduce((prev, curr) => prev + curr, 0) / entries : 0; } else { this.average = entries > 0 ? this.data().map((item) => item.totalFilter ?? 0).reduce((prev, curr) => prev + curr, 0) / entries : 0; } d3.selectAll(`#${this.visId()} > *`).remove(); this.width = this.d3Panel.nativeElement.clientWidth; this.height = this.d3Panel.nativeElement.clientHeight; const svg = d3.select(`#${this.visId()}`) .append('svg') .attr('viewBox', [0, 0, this.width, this.height]) .attr('id', `${this.visId()}_svg`) .attr('width', this.width) .attr('height', this.height) .style('-webkit-tap-highlight-color', 'transparent') .style('overflow', 'visible') .on('pointerenter pointermove', (event: Event) => this.pointermoved(event)) .on('pointerleave', () => this.pointerleft()) .on('touchstart', (event) => event.preventDefault()); const colors = ['#002338', '#225473', '#005b94', '#0071B7', '#3f99d1']; const stackedData = d3.stack<PerformanceDataItem>().keys(this.groups())(this.data()); // Add X axis --> scale to number of samples const x = d3.scaleLinear() .domain([0, this.numSamples()]) .range([0, this.width]); const y = d3.scaleLinear() .range([this.height, 0]) .domain([0, d3.max(this.data(), (d) => this.isProcessingGraph() ? d.totalProcessing : d.totalFilter) ?? 100]); svg.append('g') .call(d3.axisLeft(y).ticks(5)) .call((g) => g.select('.domain').remove()) .call((g) => g.selectAll('.tick line').clone() .attr('x2', this.width) .attr('stroke-opacity', 0.2)); const area = d3.area() .x((d, i) => x(i)) .y0((d) => y(d[0])) .y1((d) => y(d[1])); // Show the areas const valueGroups = svg.selectAll('.valgroup') .data(stackedData) .enter() .append('g') .attr('class', 'valgroup') .style('fill', (d, i) => colors[i]); // @ts-ignore valueGroups.append('path').attr('d', (d) => area(d)); const yAvg = y(this.average); // draw average line svg.append('g') .append('line') .attr('stroke', '#aaa') .attr('stroke-dasharray', '8 4') .attr('x1', x(0)) .attr('y1', yAvg) .attr('x2', x(this.numSamples())) .attr('y2', yAvg); // darw background for average label const avgBg = svg.append('rect') .attr('width', x(0.15 * this.numSamples())) .attr('height', 20) .attr('x', x(0.85 * this.numSamples())) .attr('y', 0) .attr('fill', '#ffffff66'); // draw averabe label const avgText = svg.append('g') .append('text') .call((text) => text .append('tspan') .data([`${this.average.toFixed(2)} ms`]) .append('tspan') .attr('x', 0) .attr('y', (n, i) => `${i * 1.1}em`) .attr('font-weight', (n, i) => i ? null : 'bold') .text((d) => d)); avgText.attr('transform', `translate(${x(0.9 * this.numSamples())},${yAvg - 12})`); avgBg.attr('transform', `translate(${0},${yAvg - 25})`); Iif (this.showTooltip) { // const bisect = d3.bisector((d: PerformanceDataItem) => d.frameId).center; const i = Math.round(x.invert(this.mousePosition)); Iif (i >= this.data().length) { return; } // const i = bisect(this.data, x.invert(this.mousePosition)); // Create the tooltip container. const tooltip = svg.append('g'); tooltip.style('display', null); const yValue = isProcessingGraph ? this.data()[i].totalProcessing ?? 0 : this.data()[i].totalFilter ?? 0; const posX = x(i); const posY = y(yValue); tooltip.attr('transform', `translate(${posX},${posY})`); const path = tooltip.selectAll('path') .join('path') .attr('fill', '#ffffffaa') .attr('stroke', 'black'); const text = tooltip.selectAll('text') .join('text') .call((tooltiptext) => tooltiptext .selectAll('tspan') .data(['Total: ', `${yValue.toFixed(2)} ms`]) .join('tspan') .attr('x', 0) .attr('y', (n, ttidx) => `${ttidx * 1.1}em`) .attr('font-weight', (n, ttidx) => ttidx ? null : 'bold') .text((d) => d)); const ttWidth = 50; const ttHeight = 25; // move the text upwards text.attr('transform', `translate(${-ttWidth / 2},${-ttHeight - 5})`); // draw the box path.attr('d', `M${(-ttWidth / 2) - 10},-5H-5l5,5l5,-5H${(ttWidth / 2) + 10}v${-ttHeight - 20}h-${ttWidth + 20}z`); } } private pointermoved(event: Event): void { this.showTooltip = true; this.mousePosition = d3.pointer(event)[0]; } private pointerleft(): void { this.showTooltip = false; } } |