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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 4x 4x 4x 16x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 46x 58x 400x 400x 400x 58x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 50x 50x 36x 6x 6x 6x 192x 24x 24x 6x 16x 2x 2x 2x 162x 18x 18x 18x 12x 2x 10x 2x 8x 8x 28x 20x 8x 4x 4x 4x 4x 32x 28x 4x | import { Component, Input, OnChanges } from "@angular/core" import * as d3 from "d3" type VGElement = d3.Selection<SVGSVGElement, unknown, HTMLElement, any> type GElement = d3.Selection<SVGGElement, unknown, HTMLElement, any> type Scale = d3.ScaleLinear<number, number> @Component({ selector: "cc-metric-color-range-diagram", templateUrl: "./metricColorRangeDiagram.component.html", styleUrls: ["./metricColorRangeDiagram.component.scss"], standalone: true }) export class MetricColorRangeDiagramComponent implements OnChanges { @Input() minValue: number @Input() maxValue: number @Input() colorMetric: string @Input() values: number[] @Input() currentLeftValue: number @Input() currentRightValue: number @Input() leftColor: string @Input() middleColor: string @Input() rightColor: string @Input() isAttributeDirectionInversed: boolean private frameWidth: number private frameBuffer: number private frameHeight: number private diagramWidth: number private diagramHeight: number private marginTop: number private marginBottom: number private marginLeft: number private marginRight: number private yLabelYOffset: number private percentileRanks: { x: number; y: number }[] ngOnChanges() { if (this.values.length > 0) { this.percentileRanks = this.isAttributeDirectionInversed ? this.calculateReversedPercentileRanks(this.values) : this.calculatePercentileRanks(this.values) this.renderDiagram() } } private renderDiagram() { this.initializeDiagramDimesions() this.clearDiagramContainer() const svg = this.createSvg() const group = this.createGroup(svg) const x = this.createXScale() const y = this.createYScale() this.drawAxes(group, x, y) this.drawFrame(group) this.drawLabels(group) this.drawAreas(group, x) this.drawLine(group) } private initializeDiagramDimesions() { this.frameWidth = 296 this.frameBuffer = 10 this.frameHeight = 80 this.marginTop = 10 this.marginBottom = 10 this.marginLeft = 66 this.marginRight = 54 this.diagramWidth = this.frameWidth - this.marginLeft - this.marginRight this.diagramHeight = this.frameHeight - this.marginTop - this.marginBottom this.yLabelYOffset = -47 } private clearDiagramContainer() { d3.select("#cc-range-diagram-container").selectAll("*").remove() } private createSvg() { return d3.select("#cc-range-diagram-container").append("svg") } private createGroup(svg: VGElement) { return svg.append("g").attr("transform", `translate(${this.marginLeft}, ${this.marginTop})`) } private drawFrame(g: GElement) { g.append("path") .attr( "d", `M ${-this.frameBuffer} ${-this.frameBuffer} h${this.diagramWidth + 2 * this.frameBuffer} v${ this.diagramHeight + 2 * this.frameBuffer } h${-this.diagramWidth - 2 * this.frameBuffer} v${-this.diagramHeight - 2 * this.frameBuffer}` ) .attr("fill", "none") .attr("stroke", "#888") .attr("stroke-width", "1px") } private drawAxes(g: GElement, x: Scale, y: Scale) { g.append("g") .attr("id", "axis-x") .attr("transform", `translate(0,${this.diagramHeight + this.frameBuffer})`) .call(d3.axisBottom(x).ticks(5)) .attr("color", "#888") g.append("g") .attr("id", "axis-y") .attr("transform", `translate(${-this.frameBuffer}, 0)`) .call( d3 .axisLeft(y) .ticks(5) .tickFormat(function (d) { return (d as number) >= 10_000 ? `${d3.format(".0f")((d as number) / 1000)}k` : (d as number) >= 1000 ? `${d3.format(".1f")((d as number) / 1000)}k` : d.toString() }) ) .attr("color", "#888") } private createXScale() { return d3 .scaleLinear() .domain(d3.extent(this.percentileRanks, d => d["x"] as number)) .range([0, this.diagramWidth]) } private createYScale() { const domainStandard = [0, d3.max(this.percentileRanks, d => d["y"] as number)] const domainInversed = [d3.max(this.percentileRanks, d => d["y"] as number), 0] return d3 .scaleLinear() .domain(this.isAttributeDirectionInversed ? domainInversed : domainStandard) .range([this.diagramHeight, 0]) } private drawLabels(g: GElement) { const pathStartY = -this.frameBuffer const pathHeight = this.diagramHeight + 2 * this.frameBuffer const verticalCenter = pathStartY + pathHeight / 2 g.append("text") .attr("id", "y-label") .attr("class", "y label") .attr("transform", `rotate(-90)`) .attr("x", -verticalCenter) .attr("y", this.yLabelYOffset) .attr("text-anchor", "middle") .attr("fill", "#888") .text(`${this.colorMetric}`) g.append("text") .attr("id", "x-label") .attr("class", "x label") .attr("text-anchor", "middle") .attr("x", this.diagramWidth / 2) .attr("y", this.diagramHeight + this.marginTop + this.marginBottom + 2 * this.frameBuffer) .attr("fill", "#888") .text(`Quantiles (% of ${this.colorMetric})`) } private drawAreas(g: GElement, x: Scale) { const leftValue = x( this.isAttributeDirectionInversed ? this.calculateReversedPercentileFromMetricValue(this.currentRightValue) : this.calculatePercentileFromMetricValue(this.currentLeftValue) ) const rightValue = x( this.isAttributeDirectionInversed ? this.calculateReversedPercentileFromMetricValue(this.currentLeftValue) : this.calculatePercentileFromMetricValue(this.currentRightValue) ) g.append("rect") .attr("class", "left-area") .attr("x", 0) .attr("y", -this.frameBuffer) .attr("width", leftValue) .attr("height", this.diagramHeight + 2 * this.frameBuffer) .style("fill", this.isAttributeDirectionInversed ? this.rightColor : this.leftColor) .style("fill-opacity", "0.3") g.append("rect") .attr("class", "middle-area") .attr("x", leftValue) .attr("y", -this.frameBuffer) .attr("width", rightValue - leftValue) .attr("height", this.diagramHeight + 2 * this.frameBuffer) .style("fill", this.middleColor) .style("fill-opacity", "0.3") g.append("rect") .attr("class", "right-area") .attr("x", rightValue) .attr("y", -this.frameBuffer) .attr("width", this.diagramWidth - rightValue) .attr("height", this.diagramHeight + 2 * this.frameBuffer) .style("fill", this.isAttributeDirectionInversed ? this.leftColor : this.rightColor) .style("fill-opacity", "0.3") } private drawLine(g: GElement) { g.append("path") .attr("id", "diagram-path") .datum(this.percentileRanks) .attr("fill", "none") .attr("stroke", "#888") .attr("stroke-width", 1) .attr( "d", d3 .line<{ x: number; y: number }>() .curve(d3.curveStepBefore) .x(d => this.createXScale()(d["x"])) .y(d => this.createYScale()(d["y"])) ) } private calculatePercentileRanks(array: number[]) { const uniqueSortedNumbers = [...new Set(array)].sort((a, b) => a - b) const totalNumbers = array.length const percentileRanks = [{ x: 0, y: uniqueSortedNumbers[0] }] for (const value of uniqueSortedNumbers) { const countLessThanOrEqualToUniqueNumber = array.filter(number_ => number_ <= value).length const percentileRank = (countLessThanOrEqualToUniqueNumber / totalNumbers) * 100 percentileRanks.push({ x: percentileRank, y: value }) } return percentileRanks } private calculateReversedPercentileRanks(array: number[]) { const uniqueSortedNumbers = [...new Set(array)].sort((a, b) => a - b).reverse() const totalNumbers = array.length const percentileRanks = [{ x: 0, y: uniqueSortedNumbers[0] }] for (const value of uniqueSortedNumbers) { const countGreaterThanOrEqualToUniqueNumber = array.filter(number_ => number_ >= value).length const percentileRank = (countGreaterThanOrEqualToUniqueNumber / totalNumbers) * 100 percentileRanks.push({ x: percentileRank, y: value }) } return percentileRanks.sort((a, b) => a.x - b.x) } private calculatePercentileFromMetricValue(metricValue: number) { if (metricValue === this.minValue) { return 0 } if (metricValue === this.maxValue) { return 100 } let maxPercentile = null for (const percentileRank of this.percentileRanks) { if (percentileRank["y"] < metricValue) { maxPercentile = percentileRank["x"] } else { return maxPercentile } } } private calculateReversedPercentileFromMetricValue(metricValue: number) { Iif (metricValue === this.maxValue) { return 0 } Iif (metricValue === this.minValue) { return 100 } let minPercentile = null for (const percentileRank of this.percentileRanks) { if (percentileRank["y"] > metricValue) { minPercentile = percentileRank["x"] } else { return minPercentile } } } } |