Spaces:
Runtime error
Runtime error
Deploy dd6afb5: Web re-plot: bars on numeric and log x axes, original log x ticks, pixel y orientation, tick float noise
Browse files- plotscribe/web.py +30 -7
plotscribe/web.py
CHANGED
|
@@ -79,7 +79,7 @@ function niceTicks(lo, hi, n = 5) {
|
|
| 79 |
const raw = (hi - lo) / n, mag = Math.pow(10, Math.floor(Math.log10(raw)));
|
| 80 |
const step = [1, 2, 2.5, 5, 10].map(m => m * mag).find(s => s >= raw);
|
| 81 |
const t = [];
|
| 82 |
-
for (let v = Math.ceil(lo / step) * step; v <= hi + step * 1e-9; v += step) t.push(
|
| 83 |
return t;
|
| 84 |
}
|
| 85 |
|
|
@@ -92,10 +92,13 @@ function fillTicks(ticks, lo, hi) {
|
|
| 92 |
if (!(step > 0) || gaps.some(g => Math.abs(g / step - Math.round(g / step)) > 0.05)) return niceTicks(lo, hi, 5);
|
| 93 |
const out = [];
|
| 94 |
for (let v = t[0] + Math.ceil((lo - t[0]) / step - 1e-9) * step; v <= hi + step * 1e-9; v += step)
|
| 95 |
-
out.push(
|
| 96 |
return out;
|
| 97 |
}
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
function drawChart(host, plot) {
|
| 100 |
host.innerHTML = "";
|
| 101 |
const series = plot.series.filter(s => s.points.length);
|
|
@@ -135,7 +138,9 @@ function drawChart(host, plot) {
|
|
| 135 |
if (!(bars && !ylog && ylo === 0)) ylo -= ypad;
|
| 136 |
yhi += ypad;
|
| 137 |
}
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
|
| 140 |
// x scale
|
| 141 |
let X, xTicks, band = 0;
|
|
@@ -149,11 +154,20 @@ function drawChart(host, plot) {
|
|
| 149 |
let xlo = Math.min(...xv), xhi = Math.max(...xv);
|
| 150 |
if (plot.x.range) { xlo = Math.min(xlo, xt(plot.x.range[0])); xhi = Math.max(xhi, xt(plot.x.range[1])); }
|
| 151 |
else { const pad = (xhi - xlo || 1) * 0.04; xlo -= pad; xhi += pad; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
X = v => M.l + (xt(v) - xlo) / (xhi - xlo) * pw;
|
| 153 |
// The original chart's own tick values when known (gridlines line up), else nice ticks.
|
| 154 |
const own = (plot.x.ticks || []).filter(v => !xlog || v > 0);
|
| 155 |
xTicks = xlog ? [] : (own.length >= 3 ? fillTicks(own, xlo, xhi) : niceTicks(xlo, xhi, 6)).map(v => [v, fmt(v)]);
|
| 156 |
-
if (xlog
|
|
|
|
| 157 |
}
|
| 158 |
|
| 159 |
// grid + y ticks
|
|
@@ -182,7 +196,16 @@ function drawChart(host, plot) {
|
|
| 182 |
// marks
|
| 183 |
const hits = [];
|
| 184 |
if (bars) {
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
const pos = {}, neg = {};
|
| 187 |
series.forEach((s, si) => {
|
| 188 |
for (const [i, v] of s.points) {
|
|
@@ -190,11 +213,11 @@ function drawChart(host, plot) {
|
|
| 190 |
if (stacked) {
|
| 191 |
if (v >= 0) { y0 = pos[i] || 0; y1 = y0 + v; pos[i] = y1; } else { y0 = neg[i] || 0; y1 = y0 + v; neg[i] = y1; }
|
| 192 |
}
|
| 193 |
-
const x0 =
|
| 194 |
const top = ylog ? Y(v) : Math.min(Y(y0), Y(y1)), bot = ylog ? M.t + ph : Math.max(Y(y0), Y(y1));
|
| 195 |
$("rect", {x: x0 + 1, y: top, width: Math.max(sub - 2, 1), height: Math.max(bot - top, 0.5),
|
| 196 |
fill: s.color, rx: 2}, svg);
|
| 197 |
-
hits.push({x: x0 + sub / 2, y: top, s, label: plot.x.categories[i], v});
|
| 198 |
}
|
| 199 |
});
|
| 200 |
} else {
|
|
|
|
| 79 |
const raw = (hi - lo) / n, mag = Math.pow(10, Math.floor(Math.log10(raw)));
|
| 80 |
const step = [1, 2, 2.5, 5, 10].map(m => m * mag).find(s => s >= raw);
|
| 81 |
const t = [];
|
| 82 |
+
for (let v = Math.ceil(lo / step) * step; v <= hi + step * 1e-9; v += step) t.push(snapTick(v, step));
|
| 83 |
return t;
|
| 84 |
}
|
| 85 |
|
|
|
|
| 92 |
if (!(step > 0) || gaps.some(g => Math.abs(g / step - Math.round(g / step)) > 0.05)) return niceTicks(lo, hi, 5);
|
| 93 |
const out = [];
|
| 94 |
for (let v = t[0] + Math.ceil((lo - t[0]) / step - 1e-9) * step; v <= hi + step * 1e-9; v += step)
|
| 95 |
+
out.push(snapTick(v, step));
|
| 96 |
return out;
|
| 97 |
}
|
| 98 |
|
| 99 |
+
// Accumulated float error: 0.1 * 6 - 0.6 is 1.1e-16, not 0.
|
| 100 |
+
function snapTick(v, step) { return Math.abs(v) < step * 1e-6 ? 0 : +v.toPrecision(12); }
|
| 101 |
+
|
| 102 |
function drawChart(host, plot) {
|
| 103 |
host.innerHTML = "";
|
| 104 |
const series = plot.series.filter(s => s.points.length);
|
|
|
|
| 138 |
if (!(bars && !ylog && ylo === 0)) ylo -= ypad;
|
| 139 |
yhi += ypad;
|
| 140 |
}
|
| 141 |
+
// Uncalibrated y values are pixel rows, which grow downwards.
|
| 142 |
+
const Y = plot.y.pixels ? v => M.t + (tf(v) - ylo) / (yhi - ylo) * ph
|
| 143 |
+
: v => M.t + ph - (tf(v) - ylo) / (yhi - ylo) * ph;
|
| 144 |
|
| 145 |
// x scale
|
| 146 |
let X, xTicks, band = 0;
|
|
|
|
| 154 |
let xlo = Math.min(...xv), xhi = Math.max(...xv);
|
| 155 |
if (plot.x.range) { xlo = Math.min(xlo, xt(plot.x.range[0])); xhi = Math.max(xhi, xt(plot.x.range[1])); }
|
| 156 |
else { const pad = (xhi - xlo || 1) * 0.04; xlo -= pad; xhi += pad; }
|
| 157 |
+
if (bars) {
|
| 158 |
+
// Bars on a numeric axis: each x is a group slot as wide as the closest spacing;
|
| 159 |
+
// keep whole slots inside the frame.
|
| 160 |
+
const ux = [...new Set(xv)].sort((a, b) => a - b);
|
| 161 |
+
const gaps = ux.slice(1).map((v, i) => v - ux[i]).filter(d => d > 0);
|
| 162 |
+
const half = (gaps.length ? Math.min(...gaps) : (xhi - xlo || 1) * 0.5) / 2;
|
| 163 |
+
xlo = Math.min(xlo, ux[0] - half); xhi = Math.max(xhi, ux[ux.length - 1] + half);
|
| 164 |
+
}
|
| 165 |
X = v => M.l + (xt(v) - xlo) / (xhi - xlo) * pw;
|
| 166 |
// The original chart's own tick values when known (gridlines line up), else nice ticks.
|
| 167 |
const own = (plot.x.ticks || []).filter(v => !xlog || v > 0);
|
| 168 |
xTicks = xlog ? [] : (own.length >= 3 ? fillTicks(own, xlo, xhi) : niceTicks(xlo, xhi, 6)).map(v => [v, fmt(v)]);
|
| 169 |
+
if (xlog && own.length >= 2) xTicks = own.map(v => [v, fmt(v)]); // e.g. 16, 32, 64
|
| 170 |
+
else if (xlog) for (let e = Math.ceil(xlo); e <= Math.floor(xhi); e++) xTicks.push([10 ** e, "1e" + e]);
|
| 171 |
}
|
| 172 |
|
| 173 |
// grid + y ticks
|
|
|
|
| 196 |
// marks
|
| 197 |
const hits = [];
|
| 198 |
if (bars) {
|
| 199 |
+
// Slot centre and width per x: category bands, or numeric x positions spaced by
|
| 200 |
+
// the closest pair of groups.
|
| 201 |
+
let slotW = band;
|
| 202 |
+
if (!cat) {
|
| 203 |
+
const px = [...new Set(series.flatMap(s => s.points.map(p => X(p[0]))))].sort((a, b) => a - b);
|
| 204 |
+
const d = px.slice(1).map((v, i) => v - px[i]).filter(v => v > 0.5);
|
| 205 |
+
slotW = d.length ? Math.min(...d) : pw * 0.5;
|
| 206 |
+
}
|
| 207 |
+
const centre = v => cat ? M.l + band * (v + 0.5) : X(v);
|
| 208 |
+
const nS = series.length, inner = slotW * 0.8, sub = stacked ? inner : inner / nS;
|
| 209 |
const pos = {}, neg = {};
|
| 210 |
series.forEach((s, si) => {
|
| 211 |
for (const [i, v] of s.points) {
|
|
|
|
| 213 |
if (stacked) {
|
| 214 |
if (v >= 0) { y0 = pos[i] || 0; y1 = y0 + v; pos[i] = y1; } else { y0 = neg[i] || 0; y1 = y0 + v; neg[i] = y1; }
|
| 215 |
}
|
| 216 |
+
const x0 = centre(i) - inner / 2 + (stacked ? 0 : sub * si);
|
| 217 |
const top = ylog ? Y(v) : Math.min(Y(y0), Y(y1)), bot = ylog ? M.t + ph : Math.max(Y(y0), Y(y1));
|
| 218 |
$("rect", {x: x0 + 1, y: top, width: Math.max(sub - 2, 1), height: Math.max(bot - top, 0.5),
|
| 219 |
fill: s.color, rx: 2}, svg);
|
| 220 |
+
hits.push({x: x0 + sub / 2, y: top, s, label: cat ? plot.x.categories[i] : fmt(i), v});
|
| 221 |
}
|
| 222 |
});
|
| 223 |
} else {
|