|
| 1 | +function formatDate(date) { |
| 2 | + return new Date(date).toLocaleDateString("en-US", { |
| 3 | + year: "2-digit", |
| 4 | + month: "short", |
| 5 | + }); |
| 6 | +} |
| 7 | + |
| 8 | +const lineColors = { |
| 9 | + "All subtests": "#000000", |
| 10 | + Edge: "#39ABD7", |
| 11 | + Chrome: "#F9BC30", |
| 12 | + Firefox: "#7BE1D9", |
| 13 | + Safari: "#8A4695", |
| 14 | +}; |
| 15 | + |
| 16 | +function onChartResize(chart) { |
| 17 | + const viewportAspectRatio = window.innerWidth / window.innerHeight; |
| 18 | + if (viewportAspectRatio < 0.8 && chart.aspectRatio !== 1.5) { |
| 19 | + chart.options.aspectRatio = 1.5; |
| 20 | + chart.resize(); |
| 21 | + } else if (viewportAspectRatio >= 0.8 && chart.aspectRatio !== 3) { |
| 22 | + chart.options.aspectRatio = 3; |
| 23 | + chart.resize(); |
| 24 | + } |
| 25 | + console.log(chart.aspectRatio); |
| 26 | +} |
| 27 | + |
| 28 | +function debounce(func, timeout = 300) { |
| 29 | + let timer; |
| 30 | + return (...args) => { |
| 31 | + clearTimeout(timer); |
| 32 | + timer = setTimeout(() => { |
| 33 | + func.apply(this, args); |
| 34 | + }, timeout); |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +const debouncedOnChartResize = debounce(onChartResize); |
| 39 | + |
| 40 | +const chartPlugins = { |
| 41 | + legend: { |
| 42 | + position: "top", |
| 43 | + labels: { |
| 44 | + boxHeight: 1, |
| 45 | + boxWidth: 15, |
| 46 | + }, |
| 47 | + onHover: function (event, item, legend) { |
| 48 | + for (let index = 0; index < legend.chart.data.datasets.length; index++) { |
| 49 | + if (index !== item.datasetIndex) { |
| 50 | + legend.chart.data.datasets[index].borderColor = "#0003"; |
| 51 | + } else { |
| 52 | + legend.chart.data.datasets[index].borderWidth = 3; |
| 53 | + } |
| 54 | + } |
| 55 | + legend.chart.update(); |
| 56 | + }, |
| 57 | + onLeave: function (event, item, legend) { |
| 58 | + for (let index = 0; index < legend.chart.data.datasets.length; index++) { |
| 59 | + legend.chart.data.datasets[index].borderColor = |
| 60 | + lineColors[legend.chart.data.datasets[index].label]; |
| 61 | + legend.chart.data.datasets[index].borderWidth = 2; |
| 62 | + } |
| 63 | + legend.chart.update(); |
| 64 | + }, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +const chartOptions = { |
| 69 | + pointStyle: false, |
| 70 | + animations: false, |
| 71 | + interaction: { |
| 72 | + mode: "x", |
| 73 | + }, |
| 74 | + responsive: true, |
| 75 | + aspectRatio: 3, |
| 76 | + onResize: debouncedOnChartResize, |
| 77 | + plugins: chartPlugins, |
| 78 | + scales: { |
| 79 | + y: { |
| 80 | + beginAtZero: true, |
| 81 | + }, |
| 82 | + x: { |
| 83 | + type: "time", |
| 84 | + time: { |
| 85 | + displayFormats: { |
| 86 | + quarter: "MMM YYYY", |
| 87 | + } |
| 88 | + }, |
| 89 | + ticks: { |
| 90 | + callback: function (value, index) { |
| 91 | + return index % 2 === 0 ? formatDate(value) : ""; |
| 92 | + }, |
| 93 | + maxRotation: 90, |
| 94 | + minRotation: 90, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | +}; |
| 99 | + |
| 100 | +// Experimental WPT test runs don't always have all browsers. |
| 101 | +// This utility method "smoothese" over missing data points to avoid gaps in the charts. |
| 102 | +function fixMissingDataPoint(data, dataItem, index, browser, totalOrPassed) { |
| 103 | + if (dataItem[browser] && dataItem[browser][totalOrPassed] !== 0) { |
| 104 | + // The data point exists for this browser in this test run, and is not 0. |
| 105 | + return dataItem[browser][totalOrPassed]; |
| 106 | + } else { |
| 107 | + // The data point is either missing or is 0. |
| 108 | + // Look at previous non-null/non-0 data points for this browser. |
| 109 | + for (let i = index - 1; i >= 0; i--) { |
| 110 | + if (data[i][browser] && data[i][browser][totalOrPassed] !== 0) { |
| 111 | + return data[i][browser][totalOrPassed]; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +function drawChart(canvas, data) { |
| 118 | + new Chart(canvas, { |
| 119 | + type: "line", |
| 120 | + data: { |
| 121 | + labels: data.map((d) => new Date(d.date)), |
| 122 | + datasets: [ |
| 123 | + { |
| 124 | + label: "All subtests", |
| 125 | + data: data.map((d, i) => |
| 126 | + Math.max( |
| 127 | + fixMissingDataPoint(data, d, i, "chrome", "total"), |
| 128 | + fixMissingDataPoint(data, d, i, "safari", "total"), |
| 129 | + fixMissingDataPoint(data, d, i, "firefox", "total"), |
| 130 | + fixMissingDataPoint(data, d, i, "edge", "total") |
| 131 | + ) |
| 132 | + ), |
| 133 | + borderWidth: 2, |
| 134 | + borderColor: lineColors["All subtests"], |
| 135 | + }, |
| 136 | + { |
| 137 | + label: "Edge", |
| 138 | + data: data.map((d, i) => fixMissingDataPoint(data, d, i, "edge", "passed")), |
| 139 | + borderWidth: 2, |
| 140 | + borderColor: lineColors.Edge, |
| 141 | + }, |
| 142 | + { |
| 143 | + label: "Chrome", |
| 144 | + data: data.map((d, i) => fixMissingDataPoint(data, d, i, "chrome", "passed")), |
| 145 | + borderWidth: 2, |
| 146 | + borderColor: lineColors.Chrome, |
| 147 | + }, |
| 148 | + { |
| 149 | + label: "Firefox", |
| 150 | + data: data.map((d, i) => fixMissingDataPoint(data, d, i, "firefox", "passed")), |
| 151 | + borderWidth: 2, |
| 152 | + borderColor: lineColors.Firefox, |
| 153 | + }, |
| 154 | + { |
| 155 | + label: "Safari", |
| 156 | + data: data.map((d, i) => fixMissingDataPoint(data, d, i, "safari", "passed")), |
| 157 | + borderWidth: 2, |
| 158 | + borderColor: lineColors.Safari, |
| 159 | + }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + options: chartOptions, |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +async function drawMainChart() { |
| 167 | + const response = await fetch("assets/wpt/all_tests.json"); |
| 168 | + const allTestsData = await response.json(); |
| 169 | + const canvas = document.querySelector("#main-chart"); |
| 170 | + drawChart(canvas, allTestsData); |
| 171 | +} |
| 172 | + |
| 173 | +async function drawFeatureChart(id) { |
| 174 | + const response = await fetch(`assets/wpt/${id}.json`); |
| 175 | + const data = await response.json(); |
| 176 | + const canvas = document.getElementById(`chart-${id}`); |
| 177 | + drawChart(canvas, data); |
| 178 | +} |
| 179 | + |
| 180 | +drawMainChart(); |
| 181 | + |
| 182 | +const featuresEl = document.querySelector(".features"); |
| 183 | +featuresEl.querySelectorAll(".feature").forEach((featureEl) => { |
| 184 | + const detailsEl = featureEl.querySelector("details"); |
| 185 | + |
| 186 | + detailsEl.addEventListener("toggle", (event) => { |
| 187 | + if (detailsEl.open && !featureEl.dataset.init) { |
| 188 | + featureEl.dataset.init = true; |
| 189 | + drawFeatureChart(featureEl.id); |
| 190 | + } |
| 191 | + }); |
| 192 | +}); |
0 commit comments