|
| 1 | +import QuickChart from 'quickchart-js'; |
| 2 | +import type { Context } from 'chartjs-plugin-datalabels'; |
| 3 | +import type { Artifact } from '../types.js'; |
| 4 | +import { type MinifierWithScore, isSuccessful } from './analyzed-data.js'; |
| 5 | + |
| 6 | +/* eslint-disable no-bitwise */ |
| 7 | +const hash32 = (string_: string) => { |
| 8 | + let h = 2_166_136_261 >>> 0; |
| 9 | + for (let i = 0; i < string_.length;) { |
| 10 | + const cp = string_.codePointAt(i)!; |
| 11 | + h ^= cp; |
| 12 | + h = Math.imul(h, 16_777_619); |
| 13 | + i += cp > 0xFF_FF ? 2 : 1; |
| 14 | + } |
| 15 | + return h >>> 0; |
| 16 | +}; |
| 17 | + |
| 18 | +const getColorFromName = (name: string, opacity = 1) => { |
| 19 | + const GOLDEN_ANGLE = 137.507_764_050_037_85; |
| 20 | + const hv = hash32(name); |
| 21 | + |
| 22 | + const h = (hv * GOLDEN_ANGLE) % 360; |
| 23 | + |
| 24 | + const sJitter = ((hv >>> 8) & 0xFF) / 255; |
| 25 | + const s = 75 + (sJitter - 0.5) * 15; // ~55β70% |
| 26 | + |
| 27 | + const lJitter = ((hv >>> 16) & 0xFF) / 255; |
| 28 | + const l = 50 + (lJitter - 0.5) * 15; // ~60β75% |
| 29 | + |
| 30 | + return `hsla(${h.toFixed(0)}, ${s.toFixed(0)}%, ${l.toFixed(0)}%, ${opacity})`; |
| 31 | +}; |
| 32 | +/* eslint-enable no-bitwise */ |
| 33 | + |
| 34 | +const byteFormatter = (n: number | string) => `${Intl.NumberFormat('en', { |
| 35 | + notation: 'compact', |
| 36 | + compactDisplay: 'short', |
| 37 | +}).format(Number(n))}B`; |
| 38 | + |
| 39 | +const percentFormatter = ( |
| 40 | + value: number, |
| 41 | + context: Context, |
| 42 | +) => { |
| 43 | + const base = Number(context.dataset.data[0] ?? 0); |
| 44 | + if (!base || value === base) { return ''; } |
| 45 | + return `${Math.round((value / base) * 100)}%`; |
| 46 | +}; |
| 47 | + |
| 48 | +const lightMode = { |
| 49 | + text: '#333', |
| 50 | + grid: '#33333340', |
| 51 | +} as const; |
| 52 | + |
| 53 | +const darkMode = { |
| 54 | + text: '#f0f6fc', |
| 55 | + grid: '#f0f6fc1a', |
| 56 | +} as const; |
| 57 | + |
| 58 | +export const getBarChartUrl = ( |
| 59 | + name: string, |
| 60 | + artifact: Artifact, |
| 61 | + minifiedWithScores: MinifierWithScore[], |
| 62 | + isDarkMode?: boolean, |
| 63 | +) => { |
| 64 | + const colors = isDarkMode ? darkMode : lightMode; |
| 65 | + const successfulMinifiers = minifiedWithScores.filter(isSuccessful); |
| 66 | + const labels = successfulMinifiers.map(m => m.minifierName); |
| 67 | + const minzippedData = successfulMinifiers.map(m => m.minifier.result.data.minzippedBytes); |
| 68 | + |
| 69 | + const myChart = new QuickChart(); |
| 70 | + myChart.setFormat('svg'); |
| 71 | + myChart.setWidth(720); |
| 72 | + myChart.setHeight(400); |
| 73 | + myChart.setBackgroundColor('transparent'); |
| 74 | + myChart.setVersion('4'); |
| 75 | + myChart.setConfig({ |
| 76 | + type: 'bar', |
| 77 | + data: { |
| 78 | + labels: ['Original', ...labels], |
| 79 | + datasets: [ |
| 80 | + { |
| 81 | + label: 'Gzipped Size', |
| 82 | + data: [artifact.gzipSize, ...minzippedData], |
| 83 | + backgroundColor: [ |
| 84 | + 'rgba(150, 150, 150, 0.7)', |
| 85 | + ...labels.map(lbl => getColorFromName(lbl, 0.7)), |
| 86 | + ], |
| 87 | + borderColor: [ |
| 88 | + 'rgb(150, 150, 150)', |
| 89 | + ...labels.map(lbl => getColorFromName(lbl)), |
| 90 | + ], |
| 91 | + borderWidth: 1, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + options: { |
| 96 | + scales: { |
| 97 | + x: { |
| 98 | + ticks: { |
| 99 | + color: colors.text, |
| 100 | + maxRotation: 45, |
| 101 | + minRotation: 45, |
| 102 | + }, |
| 103 | + grid: { |
| 104 | + display: false, |
| 105 | + }, |
| 106 | + }, |
| 107 | + y: { |
| 108 | + min: 0, |
| 109 | + ticks: { |
| 110 | + color: colors.text, |
| 111 | + callback: byteFormatter, |
| 112 | + font: { |
| 113 | + size: 10, |
| 114 | + }, |
| 115 | + }, |
| 116 | + title: { |
| 117 | + display: true, |
| 118 | + text: 'Gzipped Size', |
| 119 | + color: colors.text, |
| 120 | + font: { |
| 121 | + size: 14, |
| 122 | + }, |
| 123 | + }, |
| 124 | + grid: { |
| 125 | + color: colors.grid, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + plugins: { |
| 130 | + title: { |
| 131 | + display: true, |
| 132 | + text: `${name} v${artifact.version}`, |
| 133 | + font: { |
| 134 | + size: 20, |
| 135 | + }, |
| 136 | + color: colors.text, |
| 137 | + }, |
| 138 | + legend: { |
| 139 | + display: false, |
| 140 | + }, |
| 141 | + datalabels: { |
| 142 | + labels: { |
| 143 | + bytes: { |
| 144 | + anchor: 'end', |
| 145 | + align: 'top', |
| 146 | + formatter: byteFormatter, |
| 147 | + color: colors.text, |
| 148 | + font: { size: 11 }, |
| 149 | + }, |
| 150 | + percent: { |
| 151 | + anchor: 'center', |
| 152 | + align: 'center', |
| 153 | + formatter: percentFormatter, |
| 154 | + color: '#fff', |
| 155 | + font: { |
| 156 | + size: 11, |
| 157 | + weight: 'bold', |
| 158 | + }, |
| 159 | + clamp: true, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }); |
| 166 | + |
| 167 | + return myChart.getUrl(); |
| 168 | +}; |
0 commit comments