|
| 1 | +// anyplot.ai |
| 2 | +// cat-box-strip: Box Plot with Strip Overlay |
| 3 | +// Library: chartjs 4.4.7 | JavaScript 22.23.2 |
| 4 | +// Quality: 88/100 | Created: 2026-09-02 |
| 5 | + |
| 6 | +const t = window.ANYPLOT_TOKENS; |
| 7 | + |
| 8 | +// --- Data (in-memory, deterministic LCG + Box-Muller) ----------------------- |
| 9 | +let lcgState = 42; |
| 10 | +function nextRandom() { |
| 11 | + lcgState = (lcgState * 1103515245 + 12345) % 2147483648; |
| 12 | + return lcgState / 2147483648; |
| 13 | +} |
| 14 | +function nextGaussian() { |
| 15 | + const u1 = Math.max(nextRandom(), 1e-9); |
| 16 | + const u2 = nextRandom(); |
| 17 | + return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2); |
| 18 | +} |
| 19 | +function hexToRgba(hex, alpha) { |
| 20 | + const r = parseInt(hex.slice(1, 3), 16); |
| 21 | + const g = parseInt(hex.slice(3, 5), 16); |
| 22 | + const b = parseInt(hex.slice(5, 7), 16); |
| 23 | + return `rgba(${r}, ${g}, ${b}, ${alpha})`; |
| 24 | +} |
| 25 | +function quantile(sortedValues, p) { |
| 26 | + const idx = p * (sortedValues.length - 1); |
| 27 | + const lower = Math.floor(idx); |
| 28 | + const upper = Math.ceil(idx); |
| 29 | + if (lower === upper) return sortedValues[lower]; |
| 30 | + return ( |
| 31 | + sortedValues[lower] + |
| 32 | + (sortedValues[upper] - sortedValues[lower]) * (idx - lower) |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +const categories = ["Low Light", "Medium Light", "High Light", "Full Sun"]; |
| 37 | +const means = [12, 22, 34, 41]; |
| 38 | +const sds = [3, 4, 5, 6]; |
| 39 | +const sampleSize = 80; |
| 40 | + |
| 41 | +const groupValues = categories.map((_, i) => { |
| 42 | + const values = []; |
| 43 | + for (let j = 0; j < sampleSize; j++) { |
| 44 | + values.push(Math.max(1, means[i] + sds[i] * nextGaussian())); |
| 45 | + } |
| 46 | + return values; |
| 47 | +}); |
| 48 | + |
| 49 | +const stats = groupValues.map((values) => { |
| 50 | + const sorted = [...values].sort((a, b) => a - b); |
| 51 | + const q1 = quantile(sorted, 0.25); |
| 52 | + const median = quantile(sorted, 0.5); |
| 53 | + const q3 = quantile(sorted, 0.75); |
| 54 | + const iqr = q3 - q1; |
| 55 | + const lowerFence = q1 - 1.5 * iqr; |
| 56 | + const upperFence = q3 + 1.5 * iqr; |
| 57 | + const withinLower = sorted.filter((v) => v >= lowerFence); |
| 58 | + const withinUpper = sorted.filter((v) => v <= upperFence); |
| 59 | + return { |
| 60 | + q1, |
| 61 | + median, |
| 62 | + q3, |
| 63 | + whiskerMin: withinLower.length ? withinLower[0] : sorted[0], |
| 64 | + whiskerMax: withinUpper.length |
| 65 | + ? withinUpper[withinUpper.length - 1] |
| 66 | + : sorted[sorted.length - 1], |
| 67 | + }; |
| 68 | +}); |
| 69 | + |
| 70 | +const globalMin = Math.min(...groupValues.flat()); |
| 71 | +const globalMax = Math.max(...groupValues.flat()); |
| 72 | +const medianEpsilon = (globalMax - globalMin) * 0.01; |
| 73 | + |
| 74 | +// --- Mount ------------------------------------------------------------------- |
| 75 | +const canvas = document.createElement("canvas"); |
| 76 | +document.getElementById("container").appendChild(canvas); |
| 77 | + |
| 78 | +// --- Chart (floating bars for box/whisker/median, scatter for strip) -------- |
| 79 | +const whiskerDataset = { |
| 80 | + type: "bar", |
| 81 | + label: "Whisker range", |
| 82 | + data: stats.map((s, i) => ({ x: i, y: [s.whiskerMin, s.whiskerMax] })), |
| 83 | + backgroundColor: (ctx) => |
| 84 | + hexToRgba(t.palette[ctx.dataIndex % t.palette.length], 0.5), |
| 85 | + barThickness: 6, |
| 86 | + borderWidth: 0, |
| 87 | + grouped: false, |
| 88 | +}; |
| 89 | + |
| 90 | +const boxDataset = { |
| 91 | + type: "bar", |
| 92 | + label: "Interquartile range", |
| 93 | + data: stats.map((s, i) => ({ x: i, y: [s.q1, s.q3] })), |
| 94 | + backgroundColor: (ctx) => |
| 95 | + hexToRgba(t.palette[ctx.dataIndex % t.palette.length], 0.35), |
| 96 | + borderColor: (ctx) => t.palette[ctx.dataIndex % t.palette.length], |
| 97 | + borderWidth: 2, |
| 98 | + borderSkipped: false, |
| 99 | + barThickness: 90, |
| 100 | + grouped: false, |
| 101 | +}; |
| 102 | + |
| 103 | +const medianDataset = { |
| 104 | + type: "bar", |
| 105 | + label: "Median", |
| 106 | + data: stats.map((s, i) => ({ |
| 107 | + x: i, |
| 108 | + y: [s.median - medianEpsilon, s.median + medianEpsilon], |
| 109 | + })), |
| 110 | + backgroundColor: t.ink, |
| 111 | + borderWidth: 0, |
| 112 | + borderSkipped: false, |
| 113 | + barThickness: 90, |
| 114 | + grouped: false, |
| 115 | +}; |
| 116 | + |
| 117 | +const stripDatasets = categories.map((category, i) => ({ |
| 118 | + type: "scatter", |
| 119 | + label: category, |
| 120 | + data: groupValues[i].map((value) => ({ |
| 121 | + x: i + (nextRandom() - 0.5) * 0.36, |
| 122 | + y: value, |
| 123 | + })), |
| 124 | + backgroundColor: hexToRgba(t.palette[i % t.palette.length], 0.45), |
| 125 | + pointRadius: 3.5, |
| 126 | + pointHoverRadius: 3.5, |
| 127 | + pointBorderWidth: 1, |
| 128 | + pointBorderColor: t.pageBg, |
| 129 | +})); |
| 130 | + |
| 131 | +// Whisker end caps: short horizontal strokes at whiskerMin/whiskerMax so the |
| 132 | +// range reads unambiguously as a box-plot whisker rather than a plain bar. |
| 133 | +const capDataset = { |
| 134 | + type: "scatter", |
| 135 | + label: "Whisker caps", |
| 136 | + data: stats.flatMap((s, i) => [ |
| 137 | + { x: i, y: s.whiskerMin }, |
| 138 | + { x: i, y: s.whiskerMax }, |
| 139 | + ]), |
| 140 | + pointStyle: "line", |
| 141 | + pointRotation: 0, |
| 142 | + pointRadius: 20, |
| 143 | + pointBorderColor: (ctx) => |
| 144 | + t.palette[Math.floor(ctx.dataIndex / 2) % t.palette.length], |
| 145 | + pointBorderWidth: 2.5, |
| 146 | + showLine: false, |
| 147 | +}; |
| 148 | + |
| 149 | +new Chart(canvas, { |
| 150 | + type: "bar", |
| 151 | + data: { |
| 152 | + datasets: [ |
| 153 | + whiskerDataset, |
| 154 | + boxDataset, |
| 155 | + medianDataset, |
| 156 | + ...stripDatasets, |
| 157 | + capDataset, |
| 158 | + ], |
| 159 | + }, |
| 160 | + options: { |
| 161 | + responsive: true, |
| 162 | + maintainAspectRatio: false, |
| 163 | + animation: false, |
| 164 | + plugins: { |
| 165 | + title: { |
| 166 | + display: true, |
| 167 | + text: "cat-box-strip · javascript · chartjs · anyplot.ai", |
| 168 | + color: t.ink, |
| 169 | + font: { size: 22 }, |
| 170 | + }, |
| 171 | + legend: { display: false }, |
| 172 | + }, |
| 173 | + scales: { |
| 174 | + x: { |
| 175 | + type: "linear", |
| 176 | + min: -0.6, |
| 177 | + max: categories.length - 1 + 0.6, |
| 178 | + ticks: { |
| 179 | + stepSize: 1, |
| 180 | + color: t.inkSoft, |
| 181 | + font: { size: 14 }, |
| 182 | + callback: (value) => |
| 183 | + categories[value] !== undefined ? categories[value] : "", |
| 184 | + }, |
| 185 | + grid: { display: false }, |
| 186 | + title: { |
| 187 | + display: true, |
| 188 | + text: "Light Condition", |
| 189 | + color: t.ink, |
| 190 | + font: { size: 16 }, |
| 191 | + }, |
| 192 | + }, |
| 193 | + y: { |
| 194 | + ticks: { color: t.inkSoft, font: { size: 14 } }, |
| 195 | + grid: { color: t.grid }, |
| 196 | + title: { |
| 197 | + display: true, |
| 198 | + text: "Plant Height (cm)", |
| 199 | + color: t.ink, |
| 200 | + font: { size: 16 }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + }, |
| 204 | + }, |
| 205 | +}); |
0 commit comments