MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

cool graph example

+275 -1
+274
examples/graph.js
··· 1 + const data = { 2 + 0: 2, 3 + 1: 5, 4 + 2: 8, 5 + 3: 12, 6 + 4: 15, 7 + 5: 18, 8 + 6: 16, 9 + 7: 13, 10 + 8: 10, 11 + 9: 7, 12 + 10: 4 13 + }; 14 + 15 + const config = { 16 + width: 60, 17 + height: 20, 18 + xLabel: 'Time', 19 + yLabel: 'Value', 20 + barChar: 'โ–ˆ', 21 + pointChar: 'โ—', 22 + axisChar: 'โ”‚', 23 + gridChar: 'ยท' 24 + }; 25 + 26 + function getMinMax(dataObj) { 27 + const keys = Object.keys(dataObj); 28 + if (keys.length === 0) { 29 + return { min: 0, max: 0, xMin: 0, xMax: 0 }; 30 + } 31 + 32 + let yMin = dataObj[keys[0]]; 33 + let yMax = dataObj[keys[0]]; 34 + let xMin = parseInt(keys[0]); 35 + let xMax = parseInt(keys[0]); 36 + 37 + for (let i = 0; i < keys.length; i = i + 1) { 38 + const key = keys[i]; 39 + const value = dataObj[key]; 40 + const xValue = parseInt(key); 41 + 42 + if (value < yMin) yMin = value; 43 + if (value > yMax) yMax = value; 44 + if (xValue < xMin) xMin = xValue; 45 + if (xValue > xMax) xMax = xValue; 46 + } 47 + 48 + return { min: yMin, max: yMax, xMin: xMin, xMax: xMax }; 49 + } 50 + 51 + function scale(value, dataMin, dataMax, displayMin, displayMax) { 52 + if (dataMax === dataMin) return displayMin; 53 + const ratio = (value - dataMin) / (dataMax - dataMin); 54 + return displayMin + ratio * (displayMax - displayMin); 55 + } 56 + 57 + function drawLineGraph(dataObj) { 58 + const stats = getMinMax(dataObj); 59 + const keys = Object.keys(dataObj); 60 + 61 + console.log('\nLine graph:\n'); 62 + console.log('Data Range: ' + stats.min + ' to ' + stats.max); 63 + console.log('X Range: ' + stats.xMin + ' to ' + stats.xMax); 64 + console.log(''); 65 + 66 + const grid = []; 67 + for (let y = 0; y < config.height; y = y + 1) { 68 + const row = []; 69 + for (let x = 0; x < config.width; x = x + 1) row.push(' '); 70 + grid.push(row); 71 + } 72 + 73 + for (let i = 0; i < keys.length; i = i + 1) { 74 + const key = keys[i]; 75 + const xValue = parseInt(key); 76 + const yValue = dataObj[key]; 77 + 78 + const x = Math.floor(scale(xValue, stats.xMin, stats.xMax, 0, config.width - 1)); 79 + const y = Math.floor(scale(yValue, stats.min, stats.max, config.height - 1, 0)); 80 + 81 + if (x >= 0 && x < config.width && y >= 0 && y < config.height) { 82 + grid[y][x] = config.pointChar; 83 + } 84 + 85 + if (i < keys.length - 1) { 86 + const nextKey = keys[i + 1]; 87 + const nextX = parseInt(nextKey); 88 + const nextY = dataObj[nextKey]; 89 + 90 + const x1 = Math.floor(scale(xValue, stats.xMin, stats.xMax, 0, config.width - 1)); 91 + const y1 = Math.floor(scale(yValue, stats.min, stats.max, config.height - 1, 0)); 92 + const x2 = Math.floor(scale(nextX, stats.xMin, stats.xMax, 0, config.width - 1)); 93 + const y2 = Math.floor(scale(nextY, stats.min, stats.max, config.height - 1, 0)); 94 + 95 + const dx = x2 - x1; 96 + const dy = y2 - y1; 97 + const steps = Math.max(Math.abs(dx), Math.abs(dy)); 98 + 99 + for (let step = 0; step <= steps; step = step + 1) { 100 + const t = steps === 0 ? 0 : step / steps; 101 + const ix = Math.floor(x1 + t * dx); 102 + const iy = Math.floor(y1 + t * dy); 103 + 104 + if (ix >= 0 && ix < config.width && iy >= 0 && iy < config.height) { 105 + if (grid[iy][ix] === ' ') { 106 + grid[iy][ix] = config.gridChar; 107 + } 108 + } 109 + } 110 + } 111 + } 112 + 113 + const yLabelWidth = ('' + stats.max).length; 114 + 115 + for (let y = 0; y < config.height; y = y + 1) { 116 + const yValue = scale(y, 0, config.height - 1, stats.max, stats.min); 117 + const yLabel = '' + Math.floor(yValue); 118 + const paddedLabel = yLabel.padStart(yLabelWidth, ' '); 119 + 120 + let line = paddedLabel + ' ' + config.axisChar; 121 + for (let x = 0; x < config.width; x = x + 1) { 122 + line = line + grid[y][x]; 123 + } 124 + console.log(line); 125 + } 126 + 127 + let xAxis = ''; 128 + for (let i = 0; i < yLabelWidth + 1; i = i + 1) { 129 + xAxis = xAxis + ' '; 130 + } 131 + xAxis = xAxis + 'โ””'; 132 + for (let x = 0; x < config.width; x = x + 1) { 133 + xAxis = xAxis + 'โ”€'; 134 + } 135 + console.log(xAxis); 136 + 137 + let xLabels = ''; 138 + for (let i = 0; i < yLabelWidth + 2; i = i + 1) { 139 + xLabels = xLabels + ' '; 140 + } 141 + xLabels = xLabels + ('' + stats.xMin); 142 + const endLabel = '' + stats.xMax; 143 + const middleSpace = config.width - ('' + stats.xMin).length - endLabel.length; 144 + for (let i = 0; i < middleSpace; i = i + 1) { 145 + xLabels = xLabels + ' '; 146 + } 147 + xLabels = xLabels + endLabel; 148 + console.log(xLabels); 149 + console.log(''); 150 + } 151 + 152 + function drawBarChart(dataObj) { 153 + const stats = getMinMax(dataObj); 154 + const keys = Object.keys(dataObj); 155 + 156 + console.log('\nBar Chart:\n'); 157 + console.log('Data Range: ' + stats.min + ' to ' + stats.max); 158 + console.log(''); 159 + 160 + const yLabelWidth = ('' + stats.max).length; 161 + const barWidth = Math.floor((config.width - keys.length + 1) / keys.length); 162 + 163 + const grid = []; 164 + for (let y = 0; y < config.height; y = y + 1) { 165 + const row = []; 166 + for (let x = 0; x < config.width; x = x + 1) { 167 + row.push(' '); 168 + } 169 + grid.push(row); 170 + } 171 + 172 + for (let i = 0; i < keys.length; i = i + 1) { 173 + const key = keys[i]; 174 + const value = dataObj[key]; 175 + const barHeight = Math.floor(scale(value, stats.min, stats.max, 0, config.height)); 176 + const xPos = i * (barWidth + 1); 177 + 178 + for (let h = 0; h < barHeight; h = h + 1) { 179 + const y = config.height - 1 - h; 180 + for (let w = 0; w < barWidth && xPos + w < config.width; w = w + 1) { 181 + grid[y][xPos + w] = config.barChar; 182 + } 183 + } 184 + } 185 + 186 + for (let y = 0; y < config.height; y = y + 1) { 187 + const yValue = scale(y, 0, config.height - 1, stats.max, stats.min); 188 + const yLabel = '' + Math.floor(yValue); 189 + const paddedLabel = yLabel.padStart(yLabelWidth, ' '); 190 + 191 + let line = paddedLabel + ' ' + config.axisChar; 192 + for (let x = 0; x < config.width; x = x + 1) { 193 + line = line + grid[y][x]; 194 + } 195 + console.log(line); 196 + } 197 + 198 + let xAxis = ''; 199 + for (let i = 0; i < yLabelWidth + 1; i = i + 1) { 200 + xAxis = xAxis + ' '; 201 + } 202 + xAxis = xAxis + 'โ””'; 203 + for (let x = 0; x < config.width; x = x + 1) { 204 + xAxis = xAxis + 'โ”€'; 205 + } 206 + console.log(xAxis); 207 + 208 + let xLabels = ''; 209 + for (let i = 0; i < yLabelWidth + 2; i = i + 1) { 210 + xLabels = xLabels + ' '; 211 + } 212 + for (let i = 0; i < keys.length; i = i + 1) { 213 + const key = keys[i]; 214 + xLabels = xLabels + key; 215 + for (let j = 0; j < barWidth; j = j + 1) { 216 + xLabels = xLabels + ' '; 217 + } 218 + } 219 + console.log(xLabels); 220 + console.log(''); 221 + } 222 + 223 + function drawSparkline(dataObj) { 224 + const keys = Object.keys(dataObj); 225 + const stats = getMinMax(dataObj); 226 + 227 + console.log('\nSparkline:\n'); 228 + 229 + const blocks = ['โ–', 'โ–‚', 'โ–ƒ', 'โ–„', 'โ–…', 'โ–†', 'โ–‡', 'โ–ˆ']; 230 + let sparkline = ''; 231 + 232 + for (let i = 0; i < keys.length; i = i + 1) { 233 + const key = keys[i]; 234 + const value = dataObj[key]; 235 + const normalized = scale(value, stats.min, stats.max, 0, blocks.length - 1); 236 + const blockIndex = Math.floor(normalized); 237 + sparkline = sparkline + blocks[blockIndex]; 238 + } 239 + 240 + console.log(sparkline); 241 + console.log('Min: ' + stats.min + ' | Max: ' + stats.max + ' | Points: ' + keys.length); 242 + console.log(''); 243 + } 244 + 245 + function printSummary(dataObj) { 246 + const keys = Object.keys(dataObj); 247 + const stats = getMinMax(dataObj); 248 + 249 + console.log('\nSummary:\n'); 250 + console.log('Points: ' + keys.length); 251 + console.log('Y Range: ' + stats.min + ' - ' + stats.max); 252 + console.log('X Range: ' + stats.xMin + ' - ' + stats.xMax); 253 + 254 + let sum = 0; 255 + for (let i = 0; i < keys.length; i = i + 1) { 256 + sum = sum + dataObj[keys[i]]; 257 + } 258 + const avg = sum / keys.length; 259 + console.log('Average: ' + avg.toFixed(2)); 260 + console.log(''); 261 + 262 + console.log('Data Points:'); 263 + for (let i = 0; i < keys.length; i = i + 1) { 264 + const key = keys[i]; 265 + const value = dataObj[key]; 266 + const bar = config.barChar.repeat(Math.floor(value)); 267 + console.log(' ' + key.padStart(3, ' ') + ' | ' + bar + ' (' + value + ')'); 268 + } 269 + } 270 + 271 + printSummary(data); 272 + drawSparkline(data); 273 + drawLineGraph(data); 274 + drawBarChart(data);
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.1.0.8') 77 + version_conf.set('ANT_VERSION', '0.1.0.9') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80