@ggterm/core 0.2.10 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli-plot.js +965 -1
- package/dist/cli.js +920 -0
- package/dist/geoms/beeswarm.d.ts +60 -0
- package/dist/geoms/beeswarm.d.ts.map +1 -0
- package/dist/geoms/braille.d.ts +39 -0
- package/dist/geoms/braille.d.ts.map +1 -0
- package/dist/geoms/bullet.d.ts +37 -0
- package/dist/geoms/bullet.d.ts.map +1 -0
- package/dist/geoms/dumbbell.d.ts +36 -0
- package/dist/geoms/dumbbell.d.ts.map +1 -0
- package/dist/geoms/index.d.ts +7 -0
- package/dist/geoms/index.d.ts.map +1 -1
- package/dist/geoms/lollipop.d.ts +35 -0
- package/dist/geoms/lollipop.d.ts.map +1 -0
- package/dist/geoms/sparkline.d.ts +36 -0
- package/dist/geoms/sparkline.d.ts.map +1 -0
- package/dist/geoms/waffle.d.ts +36 -0
- package/dist/geoms/waffle.d.ts.map +1 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +948 -0
- package/dist/pipeline/pipeline.d.ts.map +1 -1
- package/dist/pipeline/render-geoms.d.ts +36 -0
- package/dist/pipeline/render-geoms.d.ts.map +1 -1
- package/dist/stats/beeswarm.d.ts +38 -0
- package/dist/stats/beeswarm.d.ts.map +1 -0
- package/dist/stats/index.d.ts +2 -0
- package/dist/stats/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -2309,6 +2309,521 @@ function parseColor(color) {
|
|
|
2309
2309
|
}
|
|
2310
2310
|
return { r: 128, g: 128, b: 128, a: 1 };
|
|
2311
2311
|
}
|
|
2312
|
+
function renderGeomBeeswarm(data, geom, aes, scales, canvas) {
|
|
2313
|
+
const alpha = geom.params.alpha ?? 1;
|
|
2314
|
+
const fixedColor = geom.params.color;
|
|
2315
|
+
const shape = getPointShape(geom.params.shape);
|
|
2316
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2317
|
+
const plotRight = Math.round(scales.x.range[1]);
|
|
2318
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2319
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2320
|
+
const defaultColors = [
|
|
2321
|
+
{ r: 79, g: 169, b: 238, a: 1 },
|
|
2322
|
+
{ r: 238, g: 136, b: 102, a: 1 },
|
|
2323
|
+
{ r: 102, g: 204, b: 153, a: 1 },
|
|
2324
|
+
{ r: 204, g: 102, b: 204, a: 1 },
|
|
2325
|
+
{ r: 255, g: 200, b: 87, a: 1 },
|
|
2326
|
+
{ r: 138, g: 201, b: 222, a: 1 },
|
|
2327
|
+
{ r: 255, g: 153, b: 153, a: 1 },
|
|
2328
|
+
{ r: 170, g: 170, b: 170, a: 1 }
|
|
2329
|
+
];
|
|
2330
|
+
const categories = new Set;
|
|
2331
|
+
for (const row of data) {
|
|
2332
|
+
categories.add(String(row.xOriginal ?? row[aes.x] ?? "default"));
|
|
2333
|
+
}
|
|
2334
|
+
const categoryList = [...categories];
|
|
2335
|
+
for (const row of data) {
|
|
2336
|
+
const xVal = row.x;
|
|
2337
|
+
const yVal = row.y;
|
|
2338
|
+
if (xVal === null || xVal === undefined || yVal === null || yVal === undefined) {
|
|
2339
|
+
continue;
|
|
2340
|
+
}
|
|
2341
|
+
const numGroups = categoryList.length;
|
|
2342
|
+
const xRange = plotRight - plotLeft;
|
|
2343
|
+
const xNormalized = (Number(xVal) + 0.5) / numGroups;
|
|
2344
|
+
const cx = Math.round(plotLeft + xNormalized * xRange);
|
|
2345
|
+
const cy = Math.round(scales.y.map(yVal));
|
|
2346
|
+
let color;
|
|
2347
|
+
if (fixedColor) {
|
|
2348
|
+
color = parseColorToRgba(fixedColor);
|
|
2349
|
+
} else if (scales.color && aes.color) {
|
|
2350
|
+
color = getPointColor(row, aes, scales.color);
|
|
2351
|
+
} else {
|
|
2352
|
+
const category = String(row.xOriginal ?? row[aes.x] ?? "default");
|
|
2353
|
+
const categoryIdx = categoryList.indexOf(category);
|
|
2354
|
+
color = defaultColors[categoryIdx % defaultColors.length];
|
|
2355
|
+
}
|
|
2356
|
+
if (alpha < 1) {
|
|
2357
|
+
color = { ...color, a: alpha };
|
|
2358
|
+
}
|
|
2359
|
+
if (cx >= plotLeft && cx <= plotRight && cy >= plotTop && cy <= plotBottom) {
|
|
2360
|
+
canvas.drawChar(cx, cy, shape, color);
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
function renderGeomDumbbell(data, geom, aes, scales, canvas) {
|
|
2365
|
+
const lineColor = parseColorToRgba(geom.params.lineColor ?? "#666666");
|
|
2366
|
+
const alpha = geom.params.alpha ?? 1;
|
|
2367
|
+
const shape = getPointShape(geom.params.shape);
|
|
2368
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2369
|
+
const plotRight = Math.round(scales.x.range[1]);
|
|
2370
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2371
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2372
|
+
const defaultColors = [
|
|
2373
|
+
{ r: 79, g: 169, b: 238, a: 1 },
|
|
2374
|
+
{ r: 238, g: 136, b: 102, a: 1 },
|
|
2375
|
+
{ r: 102, g: 204, b: 153, a: 1 },
|
|
2376
|
+
{ r: 204, g: 102, b: 204, a: 1 }
|
|
2377
|
+
];
|
|
2378
|
+
for (let i = 0;i < data.length; i++) {
|
|
2379
|
+
const row = data[i];
|
|
2380
|
+
const xVal = row[aes.x];
|
|
2381
|
+
const xendVal = row["xend"] ?? row[aes.x];
|
|
2382
|
+
const yVal = row[aes.y];
|
|
2383
|
+
if (xVal === null || xVal === undefined || yVal === null || yVal === undefined) {
|
|
2384
|
+
continue;
|
|
2385
|
+
}
|
|
2386
|
+
const x1 = Math.round(scales.x.map(xVal));
|
|
2387
|
+
const x2 = Math.round(scales.x.map(xendVal));
|
|
2388
|
+
const cy = Math.round(scales.y.map(yVal));
|
|
2389
|
+
let startColor;
|
|
2390
|
+
let endColor;
|
|
2391
|
+
if (geom.params.color) {
|
|
2392
|
+
startColor = parseColorToRgba(geom.params.color);
|
|
2393
|
+
} else if (scales.color && aes.color) {
|
|
2394
|
+
startColor = getPointColor(row, aes, scales.color);
|
|
2395
|
+
} else {
|
|
2396
|
+
startColor = defaultColors[0];
|
|
2397
|
+
}
|
|
2398
|
+
if (geom.params.colorEnd) {
|
|
2399
|
+
endColor = parseColorToRgba(geom.params.colorEnd);
|
|
2400
|
+
} else {
|
|
2401
|
+
endColor = geom.params.color ? startColor : defaultColors[1];
|
|
2402
|
+
}
|
|
2403
|
+
if (alpha < 1) {
|
|
2404
|
+
startColor = { ...startColor, a: alpha };
|
|
2405
|
+
endColor = { ...endColor, a: alpha };
|
|
2406
|
+
}
|
|
2407
|
+
if (cy >= plotTop && cy <= plotBottom) {
|
|
2408
|
+
const left = Math.max(plotLeft, Math.min(x1, x2));
|
|
2409
|
+
const right = Math.min(plotRight, Math.max(x1, x2));
|
|
2410
|
+
for (let x = left;x <= right; x++) {
|
|
2411
|
+
canvas.drawChar(x, cy, "─", lineColor);
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
if (x1 >= plotLeft && x1 <= plotRight && cy >= plotTop && cy <= plotBottom) {
|
|
2415
|
+
canvas.drawChar(x1, cy, shape, startColor);
|
|
2416
|
+
}
|
|
2417
|
+
if (x2 >= plotLeft && x2 <= plotRight && cy >= plotTop && cy <= plotBottom) {
|
|
2418
|
+
canvas.drawChar(x2, cy, shape, endColor);
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
function renderGeomLollipop(data, geom, aes, scales, canvas) {
|
|
2423
|
+
const alpha = geom.params.alpha ?? 1;
|
|
2424
|
+
const baseline = geom.params.baseline ?? 0;
|
|
2425
|
+
const direction = geom.params.direction ?? "vertical";
|
|
2426
|
+
const shape = getPointShape(geom.params.shape);
|
|
2427
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2428
|
+
const plotRight = Math.round(scales.x.range[1]);
|
|
2429
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2430
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2431
|
+
const defaultColors = [
|
|
2432
|
+
{ r: 79, g: 169, b: 238, a: 1 },
|
|
2433
|
+
{ r: 238, g: 136, b: 102, a: 1 },
|
|
2434
|
+
{ r: 102, g: 204, b: 153, a: 1 },
|
|
2435
|
+
{ r: 204, g: 102, b: 204, a: 1 },
|
|
2436
|
+
{ r: 255, g: 200, b: 87, a: 1 },
|
|
2437
|
+
{ r: 138, g: 201, b: 222, a: 1 }
|
|
2438
|
+
];
|
|
2439
|
+
const xValues = [...new Set(data.map((row) => row[aes.x]))];
|
|
2440
|
+
for (let i = 0;i < data.length; i++) {
|
|
2441
|
+
const row = data[i];
|
|
2442
|
+
const xVal = row[aes.x];
|
|
2443
|
+
const yVal = row[aes.y];
|
|
2444
|
+
if (xVal === null || xVal === undefined || yVal === null || yVal === undefined) {
|
|
2445
|
+
continue;
|
|
2446
|
+
}
|
|
2447
|
+
const cx = Math.round(scales.x.map(xVal));
|
|
2448
|
+
const cy = Math.round(scales.y.map(yVal));
|
|
2449
|
+
let color;
|
|
2450
|
+
if (geom.params.color) {
|
|
2451
|
+
color = parseColorToRgba(geom.params.color);
|
|
2452
|
+
} else if (scales.color && aes.color) {
|
|
2453
|
+
color = getPointColor(row, aes, scales.color);
|
|
2454
|
+
} else {
|
|
2455
|
+
const categoryIdx = xValues.indexOf(xVal);
|
|
2456
|
+
color = defaultColors[categoryIdx % defaultColors.length];
|
|
2457
|
+
}
|
|
2458
|
+
let lineColor;
|
|
2459
|
+
if (geom.params.lineColor) {
|
|
2460
|
+
lineColor = parseColorToRgba(geom.params.lineColor);
|
|
2461
|
+
} else {
|
|
2462
|
+
lineColor = {
|
|
2463
|
+
r: Math.round(color.r * 0.7),
|
|
2464
|
+
g: Math.round(color.g * 0.7),
|
|
2465
|
+
b: Math.round(color.b * 0.7),
|
|
2466
|
+
a: color.a
|
|
2467
|
+
};
|
|
2468
|
+
}
|
|
2469
|
+
if (alpha < 1) {
|
|
2470
|
+
color = { ...color, a: alpha };
|
|
2471
|
+
lineColor = { ...lineColor, a: alpha };
|
|
2472
|
+
}
|
|
2473
|
+
if (direction === "vertical") {
|
|
2474
|
+
let baselineY = Math.round(scales.y.map(baseline));
|
|
2475
|
+
baselineY = Math.max(plotTop, Math.min(plotBottom, baselineY));
|
|
2476
|
+
if (cx >= plotLeft && cx <= plotRight) {
|
|
2477
|
+
const top = Math.min(cy, baselineY);
|
|
2478
|
+
const bottom = Math.max(cy, baselineY);
|
|
2479
|
+
for (let y = top;y <= bottom; y++) {
|
|
2480
|
+
if (y >= plotTop && y <= plotBottom) {
|
|
2481
|
+
canvas.drawChar(cx, y, "│", lineColor);
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
if (cx >= plotLeft && cx <= plotRight && cy >= plotTop && cy <= plotBottom) {
|
|
2486
|
+
canvas.drawChar(cx, cy, shape, color);
|
|
2487
|
+
}
|
|
2488
|
+
} else {
|
|
2489
|
+
let baselineX = Math.round(scales.x.map(baseline));
|
|
2490
|
+
baselineX = Math.max(plotLeft, Math.min(plotRight, baselineX));
|
|
2491
|
+
if (cy >= plotTop && cy <= plotBottom) {
|
|
2492
|
+
const left = Math.min(cx, baselineX);
|
|
2493
|
+
const right = Math.max(cx, baselineX);
|
|
2494
|
+
for (let x = left;x <= right; x++) {
|
|
2495
|
+
if (x >= plotLeft && x <= plotRight) {
|
|
2496
|
+
canvas.drawChar(x, cy, "─", lineColor);
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
if (cx >= plotLeft && cx <= plotRight && cy >= plotTop && cy <= plotBottom) {
|
|
2501
|
+
canvas.drawChar(cx, cy, shape, color);
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
function renderGeomWaffle(data, geom, aes, scales, canvas) {
|
|
2507
|
+
const rows = geom.params.rows ?? 10;
|
|
2508
|
+
const cols = geom.params.cols ?? 10;
|
|
2509
|
+
const fillChar = geom.params.fill_char ?? "█";
|
|
2510
|
+
const emptyChar = geom.params.empty_char ?? "░";
|
|
2511
|
+
const showLegend = geom.params.show_legend ?? true;
|
|
2512
|
+
const flip = geom.params.flip ?? false;
|
|
2513
|
+
const gap = geom.params.gap ?? 0;
|
|
2514
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2515
|
+
const plotRight = Math.round(scales.x.range[1]);
|
|
2516
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2517
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2518
|
+
const defaultColors = [
|
|
2519
|
+
{ r: 79, g: 169, b: 238, a: 1 },
|
|
2520
|
+
{ r: 238, g: 136, b: 102, a: 1 },
|
|
2521
|
+
{ r: 102, g: 204, b: 153, a: 1 },
|
|
2522
|
+
{ r: 204, g: 102, b: 204, a: 1 },
|
|
2523
|
+
{ r: 255, g: 200, b: 87, a: 1 },
|
|
2524
|
+
{ r: 138, g: 201, b: 222, a: 1 },
|
|
2525
|
+
{ r: 255, g: 153, b: 153, a: 1 },
|
|
2526
|
+
{ r: 170, g: 170, b: 170, a: 1 }
|
|
2527
|
+
];
|
|
2528
|
+
const fillField = aes.fill || aes.color || "category";
|
|
2529
|
+
const valueField = aes.y || "value";
|
|
2530
|
+
const categories = new Map;
|
|
2531
|
+
let totalValue = 0;
|
|
2532
|
+
for (const row of data) {
|
|
2533
|
+
const cat = String(row[fillField] ?? "default");
|
|
2534
|
+
const val = Number(row[valueField]) || 1;
|
|
2535
|
+
categories.set(cat, (categories.get(cat) ?? 0) + val);
|
|
2536
|
+
totalValue += val;
|
|
2537
|
+
}
|
|
2538
|
+
const cellsPerCategory = [];
|
|
2539
|
+
const categoryList = [...categories.keys()];
|
|
2540
|
+
let cellsAssigned = 0;
|
|
2541
|
+
for (let i = 0;i < categoryList.length; i++) {
|
|
2542
|
+
const cat = categoryList[i];
|
|
2543
|
+
const val = categories.get(cat);
|
|
2544
|
+
const proportion = val / totalValue;
|
|
2545
|
+
const cells = Math.round(proportion * rows * cols);
|
|
2546
|
+
const color = scales.color?.map(cat) ?? defaultColors[i % defaultColors.length];
|
|
2547
|
+
cellsPerCategory.push({ category: cat, cells, color });
|
|
2548
|
+
cellsAssigned += cells;
|
|
2549
|
+
}
|
|
2550
|
+
if (cellsAssigned < rows * cols && cellsPerCategory.length > 0) {
|
|
2551
|
+
cellsPerCategory[0].cells += rows * cols - cellsAssigned;
|
|
2552
|
+
}
|
|
2553
|
+
const grid = [];
|
|
2554
|
+
for (const { cells, color } of cellsPerCategory) {
|
|
2555
|
+
for (let i = 0;i < cells; i++) {
|
|
2556
|
+
grid.push({ char: fillChar, color });
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
const emptyColor = { r: 80, g: 80, b: 80, a: 0.3 };
|
|
2560
|
+
while (grid.length < rows * cols) {
|
|
2561
|
+
grid.push({ char: emptyChar, color: emptyColor });
|
|
2562
|
+
}
|
|
2563
|
+
const availableWidth = plotRight - plotLeft - (showLegend ? 15 : 0);
|
|
2564
|
+
const availableHeight = plotBottom - plotTop;
|
|
2565
|
+
const cellWidth = Math.max(1, Math.floor(availableWidth / cols)) + gap;
|
|
2566
|
+
const cellHeight = Math.max(1, Math.floor(availableHeight / rows)) + gap;
|
|
2567
|
+
for (let row = 0;row < rows; row++) {
|
|
2568
|
+
for (let col = 0;col < cols; col++) {
|
|
2569
|
+
let idx;
|
|
2570
|
+
if (flip) {
|
|
2571
|
+
idx = row * cols + col;
|
|
2572
|
+
} else {
|
|
2573
|
+
idx = col * rows + (rows - 1 - row);
|
|
2574
|
+
}
|
|
2575
|
+
if (idx >= grid.length)
|
|
2576
|
+
continue;
|
|
2577
|
+
const cell = grid[idx];
|
|
2578
|
+
const x = plotLeft + col * cellWidth;
|
|
2579
|
+
const y = plotTop + row * cellHeight;
|
|
2580
|
+
if (x >= plotLeft && x < plotRight - (showLegend ? 15 : 0) && y >= plotTop && y <= plotBottom) {
|
|
2581
|
+
canvas.drawChar(x, y, cell.char, cell.color);
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
if (showLegend) {
|
|
2586
|
+
const legendX = plotRight - 12;
|
|
2587
|
+
let legendY = plotTop;
|
|
2588
|
+
for (let i = 0;i < cellsPerCategory.length && legendY < plotBottom; i++) {
|
|
2589
|
+
const { category, cells, color } = cellsPerCategory[i];
|
|
2590
|
+
const pct = Math.round(cells / (rows * cols) * 100);
|
|
2591
|
+
const label = `${category.slice(0, 6)} ${pct}%`;
|
|
2592
|
+
canvas.drawChar(legendX, legendY, "█", color);
|
|
2593
|
+
canvas.drawString(legendX + 2, legendY, label, { r: 180, g: 180, b: 180, a: 1 });
|
|
2594
|
+
legendY += 2;
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
function renderGeomSparkline(data, geom, aes, scales, canvas) {
|
|
2599
|
+
const sparkType = geom.params.sparkType ?? "bar";
|
|
2600
|
+
const width = geom.params.width ?? 20;
|
|
2601
|
+
const showMinmax = geom.params.show_minmax ?? false;
|
|
2602
|
+
const normalize = geom.params.normalize ?? true;
|
|
2603
|
+
const minColor = parseColorToRgba(geom.params.min_color ?? "#e74c3c");
|
|
2604
|
+
const maxColor = parseColorToRgba(geom.params.max_color ?? "#2ecc71");
|
|
2605
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2606
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2607
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2608
|
+
const SPARK_CHARS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
|
|
2609
|
+
const defaultColor = { r: 79, g: 169, b: 238, a: 1 };
|
|
2610
|
+
const groupField = aes.group || aes.color;
|
|
2611
|
+
const groups = new Map;
|
|
2612
|
+
if (groupField) {
|
|
2613
|
+
for (const row of data) {
|
|
2614
|
+
const key = String(row[groupField] ?? "default");
|
|
2615
|
+
if (!groups.has(key))
|
|
2616
|
+
groups.set(key, []);
|
|
2617
|
+
groups.get(key).push(row);
|
|
2618
|
+
}
|
|
2619
|
+
} else {
|
|
2620
|
+
groups.set("default", [...data]);
|
|
2621
|
+
}
|
|
2622
|
+
let currentY = plotTop;
|
|
2623
|
+
for (const [groupKey, groupData] of groups) {
|
|
2624
|
+
const sorted = aes.x ? [...groupData].sort((a, b) => Number(a[aes.x]) - Number(b[aes.x])) : groupData;
|
|
2625
|
+
const values = sorted.map((row) => Number(row[aes.y]) || 0);
|
|
2626
|
+
if (values.length === 0)
|
|
2627
|
+
continue;
|
|
2628
|
+
const minVal = Math.min(...values);
|
|
2629
|
+
const maxVal = Math.max(...values);
|
|
2630
|
+
const minIdx = values.indexOf(minVal);
|
|
2631
|
+
const maxIdx = values.indexOf(maxVal);
|
|
2632
|
+
const range = maxVal - minVal || 1;
|
|
2633
|
+
const color = scales.color?.map(groupKey) ?? defaultColor;
|
|
2634
|
+
const sparkValues = [];
|
|
2635
|
+
if (values.length <= width) {
|
|
2636
|
+
sparkValues.push(...values);
|
|
2637
|
+
} else {
|
|
2638
|
+
for (let i = 0;i < width; i++) {
|
|
2639
|
+
const idx = Math.floor(i * values.length / width);
|
|
2640
|
+
sparkValues.push(values[idx]);
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
for (let i = 0;i < sparkValues.length; i++) {
|
|
2644
|
+
const val = sparkValues[i];
|
|
2645
|
+
const normalized = normalize ? (val - minVal) / range : val / (maxVal || 1);
|
|
2646
|
+
const charIdx = Math.min(7, Math.max(0, Math.floor(normalized * 8)));
|
|
2647
|
+
const char = sparkType === "dot" ? "•" : SPARK_CHARS[charIdx];
|
|
2648
|
+
const x = plotLeft + i;
|
|
2649
|
+
const y = currentY;
|
|
2650
|
+
let pointColor = color;
|
|
2651
|
+
if (showMinmax) {
|
|
2652
|
+
const origIdx = Math.floor(i * values.length / sparkValues.length);
|
|
2653
|
+
if (origIdx === minIdx)
|
|
2654
|
+
pointColor = minColor;
|
|
2655
|
+
else if (origIdx === maxIdx)
|
|
2656
|
+
pointColor = maxColor;
|
|
2657
|
+
}
|
|
2658
|
+
if (x < plotLeft + width && y >= plotTop && y <= plotBottom) {
|
|
2659
|
+
canvas.drawChar(x, y, char, pointColor);
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
if (groupField && groups.size > 1) {
|
|
2663
|
+
const labelX = plotLeft + width + 1;
|
|
2664
|
+
canvas.drawString(labelX, currentY, groupKey.slice(0, 8), { r: 180, g: 180, b: 180, a: 1 });
|
|
2665
|
+
}
|
|
2666
|
+
currentY += 2;
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
function renderGeomBullet(data, geom, aes, scales, canvas) {
|
|
2670
|
+
const width = geom.params.width ?? 40;
|
|
2671
|
+
const targetChar = geom.params.target_char ?? "│";
|
|
2672
|
+
const barChar = geom.params.bar_char ?? "█";
|
|
2673
|
+
const rangeChars = geom.params.range_chars ?? ["░", "▒", "▓"];
|
|
2674
|
+
const showValues = geom.params.show_values ?? true;
|
|
2675
|
+
const targetColor = parseColorToRgba(geom.params.target_color ?? "#e74c3c");
|
|
2676
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2677
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2678
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2679
|
+
const defaultColors = [
|
|
2680
|
+
{ r: 79, g: 169, b: 238, a: 1 },
|
|
2681
|
+
{ r: 238, g: 136, b: 102, a: 1 },
|
|
2682
|
+
{ r: 102, g: 204, b: 153, a: 1 }
|
|
2683
|
+
];
|
|
2684
|
+
const rangeColors = [
|
|
2685
|
+
{ r: 60, g: 60, b: 60, a: 1 },
|
|
2686
|
+
{ r: 90, g: 90, b: 90, a: 1 },
|
|
2687
|
+
{ r: 120, g: 120, b: 120, a: 1 }
|
|
2688
|
+
];
|
|
2689
|
+
let currentY = plotTop;
|
|
2690
|
+
for (let i = 0;i < data.length; i++) {
|
|
2691
|
+
const row = data[i];
|
|
2692
|
+
const label = aes.x ? String(row[aes.x]).slice(0, 10) : `Item ${i + 1}`;
|
|
2693
|
+
const value = Number(row[aes.y]) || 0;
|
|
2694
|
+
const target = Number(row["target"]) || null;
|
|
2695
|
+
const maxValue = Number(row["max"]) || Math.max(value, target || 0) * 1.2;
|
|
2696
|
+
const ranges = row["ranges"] ?? [maxValue * 0.6, maxValue * 0.8, maxValue];
|
|
2697
|
+
const color = scales.color?.map(label) ?? defaultColors[i % defaultColors.length];
|
|
2698
|
+
const labelWidth = 12;
|
|
2699
|
+
canvas.drawString(plotLeft, currentY, label.padEnd(labelWidth), { r: 180, g: 180, b: 180, a: 1 });
|
|
2700
|
+
const barStart = plotLeft + labelWidth;
|
|
2701
|
+
const barWidth = Math.min(width, scales.x.range[1] - barStart - (showValues ? 8 : 0));
|
|
2702
|
+
for (let r = ranges.length - 1;r >= 0; r--) {
|
|
2703
|
+
const rangeWidth = Math.round(ranges[r] / maxValue * barWidth);
|
|
2704
|
+
for (let x = 0;x < rangeWidth; x++) {
|
|
2705
|
+
if (barStart + x <= scales.x.range[1]) {
|
|
2706
|
+
canvas.drawChar(barStart + x, currentY, rangeChars[r], rangeColors[r]);
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
}
|
|
2710
|
+
const valueWidth = Math.round(value / maxValue * barWidth);
|
|
2711
|
+
for (let x = 0;x < valueWidth; x++) {
|
|
2712
|
+
if (barStart + x <= scales.x.range[1]) {
|
|
2713
|
+
canvas.drawChar(barStart + x, currentY, barChar, color);
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
if (target !== null) {
|
|
2717
|
+
const targetX = barStart + Math.round(target / maxValue * barWidth);
|
|
2718
|
+
if (targetX >= barStart && targetX <= barStart + barWidth) {
|
|
2719
|
+
canvas.drawChar(targetX, currentY, targetChar, targetColor);
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
if (showValues) {
|
|
2723
|
+
const valueStr = value.toFixed(0);
|
|
2724
|
+
canvas.drawString(barStart + barWidth + 2, currentY, valueStr, { r: 180, g: 180, b: 180, a: 1 });
|
|
2725
|
+
}
|
|
2726
|
+
currentY += 2;
|
|
2727
|
+
if (currentY > plotBottom)
|
|
2728
|
+
break;
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
function renderGeomBraille(data, geom, aes, scales, canvas) {
|
|
2732
|
+
const brailleType = geom.params.brailleType ?? "point";
|
|
2733
|
+
const fill = geom.params.fill ?? false;
|
|
2734
|
+
const alpha = geom.params.alpha ?? 1;
|
|
2735
|
+
const BRAILLE_BASE = 10240;
|
|
2736
|
+
const DOTS = [
|
|
2737
|
+
[1, 2, 4, 64],
|
|
2738
|
+
[8, 16, 32, 128]
|
|
2739
|
+
];
|
|
2740
|
+
const plotLeft = Math.round(scales.x.range[0]);
|
|
2741
|
+
const plotRight = Math.round(scales.x.range[1]);
|
|
2742
|
+
const plotTop = Math.round(Math.min(scales.y.range[0], scales.y.range[1]));
|
|
2743
|
+
const plotBottom = Math.round(Math.max(scales.y.range[0], scales.y.range[1]));
|
|
2744
|
+
const plotWidth = plotRight - plotLeft;
|
|
2745
|
+
const plotHeight = plotBottom - plotTop;
|
|
2746
|
+
const brailleWidth = plotWidth;
|
|
2747
|
+
const brailleHeight = plotHeight;
|
|
2748
|
+
const buffer = [];
|
|
2749
|
+
for (let y = 0;y < brailleHeight; y++) {
|
|
2750
|
+
buffer[y] = new Array(brailleWidth).fill(0);
|
|
2751
|
+
}
|
|
2752
|
+
const defaultColor = { r: 79, g: 169, b: 238, a: 1 };
|
|
2753
|
+
const color = geom.params.color ? parseColorToRgba(geom.params.color) : defaultColor;
|
|
2754
|
+
const finalColor = alpha < 1 ? { ...color, a: alpha } : color;
|
|
2755
|
+
const sorted = aes.x ? [...data].sort((a, b) => Number(a[aes.x]) - Number(b[aes.x])) : data;
|
|
2756
|
+
const setDot = (canvasX, canvasY) => {
|
|
2757
|
+
const subX = (canvasX - plotLeft) * 2;
|
|
2758
|
+
const subY = (canvasY - plotTop) * 4;
|
|
2759
|
+
const cellX = Math.floor(subX / 2);
|
|
2760
|
+
const cellY = Math.floor(subY / 4);
|
|
2761
|
+
const dotCol = subX % 2;
|
|
2762
|
+
const dotRow = subY % 4;
|
|
2763
|
+
if (cellX >= 0 && cellX < brailleWidth && cellY >= 0 && cellY < brailleHeight) {
|
|
2764
|
+
if (dotCol >= 0 && dotCol < 2 && dotRow >= 0 && dotRow < 4) {
|
|
2765
|
+
buffer[cellY][cellX] |= DOTS[dotCol][dotRow];
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
};
|
|
2769
|
+
let prevCx = null;
|
|
2770
|
+
let prevCy = null;
|
|
2771
|
+
for (const row of sorted) {
|
|
2772
|
+
const xVal = row[aes.x];
|
|
2773
|
+
const yVal = row[aes.y];
|
|
2774
|
+
if (xVal === null || xVal === undefined || yVal === null || yVal === undefined) {
|
|
2775
|
+
prevCx = null;
|
|
2776
|
+
prevCy = null;
|
|
2777
|
+
continue;
|
|
2778
|
+
}
|
|
2779
|
+
const cx = Math.round(scales.x.map(xVal));
|
|
2780
|
+
const cy = Math.round(scales.y.map(yVal));
|
|
2781
|
+
if (brailleType === "line" && prevCx !== null && prevCy !== null) {
|
|
2782
|
+
const dx = Math.abs(cx - prevCx);
|
|
2783
|
+
const dy = Math.abs(cy - prevCy);
|
|
2784
|
+
const sx = prevCx < cx ? 1 : -1;
|
|
2785
|
+
const sy = prevCy < cy ? 1 : -1;
|
|
2786
|
+
let err = dx - dy;
|
|
2787
|
+
let x = prevCx;
|
|
2788
|
+
let y = prevCy;
|
|
2789
|
+
while (true) {
|
|
2790
|
+
if (x >= plotLeft && x < plotRight && y >= plotTop && y < plotBottom) {
|
|
2791
|
+
setDot(x, y);
|
|
2792
|
+
if (fill) {
|
|
2793
|
+
for (let fy = y;fy < plotBottom; fy++) {
|
|
2794
|
+
setDot(x, fy);
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
if (x === cx && y === cy)
|
|
2799
|
+
break;
|
|
2800
|
+
const e2 = 2 * err;
|
|
2801
|
+
if (e2 > -dy) {
|
|
2802
|
+
err -= dy;
|
|
2803
|
+
x += sx;
|
|
2804
|
+
}
|
|
2805
|
+
if (e2 < dx) {
|
|
2806
|
+
err += dx;
|
|
2807
|
+
y += sy;
|
|
2808
|
+
}
|
|
2809
|
+
}
|
|
2810
|
+
} else {
|
|
2811
|
+
if (cx >= plotLeft && cx < plotRight && cy >= plotTop && cy < plotBottom) {
|
|
2812
|
+
setDot(cx, cy);
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
prevCx = cx;
|
|
2816
|
+
prevCy = cy;
|
|
2817
|
+
}
|
|
2818
|
+
for (let y = 0;y < brailleHeight; y++) {
|
|
2819
|
+
for (let x = 0;x < brailleWidth; x++) {
|
|
2820
|
+
if (buffer[y][x] > 0) {
|
|
2821
|
+
const char = String.fromCharCode(BRAILLE_BASE + buffer[y][x]);
|
|
2822
|
+
canvas.drawChar(plotLeft + x, plotTop + y, char, finalColor);
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2312
2827
|
function renderGeom(data, geom, aes, scales, canvas, coordType) {
|
|
2313
2828
|
switch (geom.type) {
|
|
2314
2829
|
case "point":
|
|
@@ -2390,6 +2905,28 @@ function renderGeom(data, geom, aes, scales, canvas, coordType) {
|
|
|
2390
2905
|
case "smooth":
|
|
2391
2906
|
renderGeomSmooth(data, geom, aes, scales, canvas);
|
|
2392
2907
|
break;
|
|
2908
|
+
case "beeswarm":
|
|
2909
|
+
case "quasirandom":
|
|
2910
|
+
renderGeomBeeswarm(data, geom, aes, scales, canvas);
|
|
2911
|
+
break;
|
|
2912
|
+
case "dumbbell":
|
|
2913
|
+
renderGeomDumbbell(data, geom, aes, scales, canvas);
|
|
2914
|
+
break;
|
|
2915
|
+
case "lollipop":
|
|
2916
|
+
renderGeomLollipop(data, geom, aes, scales, canvas);
|
|
2917
|
+
break;
|
|
2918
|
+
case "waffle":
|
|
2919
|
+
renderGeomWaffle(data, geom, aes, scales, canvas);
|
|
2920
|
+
break;
|
|
2921
|
+
case "sparkline":
|
|
2922
|
+
renderGeomSparkline(data, geom, aes, scales, canvas);
|
|
2923
|
+
break;
|
|
2924
|
+
case "bullet":
|
|
2925
|
+
renderGeomBullet(data, geom, aes, scales, canvas);
|
|
2926
|
+
break;
|
|
2927
|
+
case "braille":
|
|
2928
|
+
renderGeomBraille(data, geom, aes, scales, canvas);
|
|
2929
|
+
break;
|
|
2393
2930
|
default:
|
|
2394
2931
|
break;
|
|
2395
2932
|
}
|
|
@@ -3517,6 +4054,224 @@ function stat_xdensity(params = {}) {
|
|
|
3517
4054
|
};
|
|
3518
4055
|
}
|
|
3519
4056
|
|
|
4057
|
+
// src/stats/beeswarm.ts
|
|
4058
|
+
function swarmArrange(yValues, params) {
|
|
4059
|
+
const n = yValues.length;
|
|
4060
|
+
if (n === 0)
|
|
4061
|
+
return { offsets: [], indices: [] };
|
|
4062
|
+
const cex = params.cex ?? 1;
|
|
4063
|
+
const spacing = params.spacing ?? 1;
|
|
4064
|
+
const side = params.side ?? 0;
|
|
4065
|
+
const yRange = Math.max(...yValues) - Math.min(...yValues);
|
|
4066
|
+
const pointSize = yRange / Math.max(n, 10) * cex * spacing;
|
|
4067
|
+
let indices = yValues.map((_, i) => i);
|
|
4068
|
+
const priority = params.priority ?? "ascending";
|
|
4069
|
+
switch (priority) {
|
|
4070
|
+
case "ascending":
|
|
4071
|
+
indices.sort((a, b) => yValues[a] - yValues[b]);
|
|
4072
|
+
break;
|
|
4073
|
+
case "descending":
|
|
4074
|
+
indices.sort((a, b) => yValues[b] - yValues[a]);
|
|
4075
|
+
break;
|
|
4076
|
+
case "random":
|
|
4077
|
+
for (let i = indices.length - 1;i > 0; i--) {
|
|
4078
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
4079
|
+
[indices[i], indices[j]] = [indices[j], indices[i]];
|
|
4080
|
+
}
|
|
4081
|
+
break;
|
|
4082
|
+
case "density":
|
|
4083
|
+
const median = yValues.slice().sort((a, b) => a - b)[Math.floor(n / 2)];
|
|
4084
|
+
indices.sort((a, b) => Math.abs(yValues[a] - median) - Math.abs(yValues[b] - median));
|
|
4085
|
+
break;
|
|
4086
|
+
}
|
|
4087
|
+
const placed = [];
|
|
4088
|
+
const offsets = new Array(n).fill(0);
|
|
4089
|
+
for (const idx of indices) {
|
|
4090
|
+
const y = yValues[idx];
|
|
4091
|
+
let bestOffset = 0;
|
|
4092
|
+
if (placed.length > 0) {
|
|
4093
|
+
const nearby = placed.filter((p) => Math.abs(p.y - y) < pointSize * 2);
|
|
4094
|
+
if (nearby.length > 0) {
|
|
4095
|
+
const maxOffset = nearby.length * pointSize;
|
|
4096
|
+
let foundSpot = false;
|
|
4097
|
+
for (let tryOffset = 0;tryOffset <= maxOffset && !foundSpot; tryOffset += pointSize * 0.5) {
|
|
4098
|
+
if (side >= 0) {
|
|
4099
|
+
let collision = false;
|
|
4100
|
+
for (const p of nearby) {
|
|
4101
|
+
const dx = tryOffset - p.x;
|
|
4102
|
+
const dy = y - p.y;
|
|
4103
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
4104
|
+
if (dist < pointSize) {
|
|
4105
|
+
collision = true;
|
|
4106
|
+
break;
|
|
4107
|
+
}
|
|
4108
|
+
}
|
|
4109
|
+
if (!collision) {
|
|
4110
|
+
bestOffset = tryOffset;
|
|
4111
|
+
foundSpot = true;
|
|
4112
|
+
break;
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
4115
|
+
if (side <= 0 && tryOffset > 0 && !foundSpot) {
|
|
4116
|
+
let collision = false;
|
|
4117
|
+
for (const p of nearby) {
|
|
4118
|
+
const dx = -tryOffset - p.x;
|
|
4119
|
+
const dy = y - p.y;
|
|
4120
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
4121
|
+
if (dist < pointSize) {
|
|
4122
|
+
collision = true;
|
|
4123
|
+
break;
|
|
4124
|
+
}
|
|
4125
|
+
}
|
|
4126
|
+
if (!collision) {
|
|
4127
|
+
bestOffset = -tryOffset;
|
|
4128
|
+
foundSpot = true;
|
|
4129
|
+
break;
|
|
4130
|
+
}
|
|
4131
|
+
}
|
|
4132
|
+
}
|
|
4133
|
+
}
|
|
4134
|
+
}
|
|
4135
|
+
offsets[idx] = bestOffset;
|
|
4136
|
+
placed.push({ y, x: bestOffset });
|
|
4137
|
+
}
|
|
4138
|
+
const maxAbsOffset = Math.max(...offsets.map(Math.abs), 0.001);
|
|
4139
|
+
const dodge = params.dodge ?? 0.8;
|
|
4140
|
+
const scale = dodge * 0.5 / maxAbsOffset;
|
|
4141
|
+
for (let i = 0;i < offsets.length; i++) {
|
|
4142
|
+
offsets[i] *= scale;
|
|
4143
|
+
}
|
|
4144
|
+
return { offsets, indices };
|
|
4145
|
+
}
|
|
4146
|
+
function centerArrange(yValues, params) {
|
|
4147
|
+
const n = yValues.length;
|
|
4148
|
+
if (n === 0)
|
|
4149
|
+
return { offsets: [], indices: [] };
|
|
4150
|
+
const dodge = params.dodge ?? 0.8;
|
|
4151
|
+
const side = params.side ?? 0;
|
|
4152
|
+
const indices = yValues.map((_, i) => i);
|
|
4153
|
+
indices.sort((a, b) => yValues[a] - yValues[b]);
|
|
4154
|
+
const offsets = new Array(n).fill(0);
|
|
4155
|
+
const maxOffset = dodge * 0.4;
|
|
4156
|
+
for (let i = 0;i < indices.length; i++) {
|
|
4157
|
+
const idx = indices[i];
|
|
4158
|
+
const layer = Math.floor(i / 2) + 1;
|
|
4159
|
+
const offset = layer / Math.ceil(n / 2) * maxOffset;
|
|
4160
|
+
if (side === 0) {
|
|
4161
|
+
offsets[idx] = i % 2 === 0 ? offset : -offset;
|
|
4162
|
+
} else {
|
|
4163
|
+
offsets[idx] = side * offset;
|
|
4164
|
+
}
|
|
4165
|
+
}
|
|
4166
|
+
return { offsets, indices };
|
|
4167
|
+
}
|
|
4168
|
+
function squareArrange(yValues, params) {
|
|
4169
|
+
const n = yValues.length;
|
|
4170
|
+
if (n === 0)
|
|
4171
|
+
return { offsets: [], indices: [] };
|
|
4172
|
+
const dodge = params.dodge ?? 0.8;
|
|
4173
|
+
const side = params.side ?? 0;
|
|
4174
|
+
const indices = yValues.map((_, i) => i);
|
|
4175
|
+
indices.sort((a, b) => yValues[a] - yValues[b]);
|
|
4176
|
+
const offsets = new Array(n).fill(0);
|
|
4177
|
+
const yRange = Math.max(...yValues) - Math.min(...yValues);
|
|
4178
|
+
const binSize = yRange / Math.max(Math.sqrt(n), 3);
|
|
4179
|
+
const bins = new Map;
|
|
4180
|
+
for (let i = 0;i < indices.length; i++) {
|
|
4181
|
+
const idx = indices[i];
|
|
4182
|
+
const y = yValues[idx];
|
|
4183
|
+
const binKey = Math.floor(y / binSize);
|
|
4184
|
+
if (!bins.has(binKey)) {
|
|
4185
|
+
bins.set(binKey, []);
|
|
4186
|
+
}
|
|
4187
|
+
bins.get(binKey).push(idx);
|
|
4188
|
+
}
|
|
4189
|
+
for (const binIndices of bins.values()) {
|
|
4190
|
+
const binN = binIndices.length;
|
|
4191
|
+
const maxOffset = dodge * 0.4;
|
|
4192
|
+
for (let i = 0;i < binN; i++) {
|
|
4193
|
+
const idx = binIndices[i];
|
|
4194
|
+
const offset = (i - (binN - 1) / 2) * (maxOffset * 2 / Math.max(binN - 1, 1));
|
|
4195
|
+
if (side === 0) {
|
|
4196
|
+
offsets[idx] = offset;
|
|
4197
|
+
} else if (side > 0) {
|
|
4198
|
+
offsets[idx] = Math.abs(offset);
|
|
4199
|
+
} else {
|
|
4200
|
+
offsets[idx] = -Math.abs(offset);
|
|
4201
|
+
}
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
return { offsets, indices };
|
|
4205
|
+
}
|
|
4206
|
+
function computeBeeswarm(data, _xField, yField, groupKey, groupIndex, params = {}) {
|
|
4207
|
+
const yValues = [];
|
|
4208
|
+
const originalRows = [];
|
|
4209
|
+
for (const row of data) {
|
|
4210
|
+
const yVal = row[yField];
|
|
4211
|
+
if (yVal === null || yVal === undefined)
|
|
4212
|
+
continue;
|
|
4213
|
+
const numY = Number(yVal);
|
|
4214
|
+
if (isNaN(numY))
|
|
4215
|
+
continue;
|
|
4216
|
+
yValues.push(numY);
|
|
4217
|
+
originalRows.push(row);
|
|
4218
|
+
}
|
|
4219
|
+
if (yValues.length === 0)
|
|
4220
|
+
return [];
|
|
4221
|
+
const method = params.method ?? "swarm";
|
|
4222
|
+
let result;
|
|
4223
|
+
switch (method) {
|
|
4224
|
+
case "center":
|
|
4225
|
+
result = centerArrange(yValues, params);
|
|
4226
|
+
break;
|
|
4227
|
+
case "square":
|
|
4228
|
+
result = squareArrange(yValues, params);
|
|
4229
|
+
break;
|
|
4230
|
+
case "swarm":
|
|
4231
|
+
default:
|
|
4232
|
+
result = swarmArrange(yValues, params);
|
|
4233
|
+
}
|
|
4234
|
+
const output = [];
|
|
4235
|
+
for (let i = 0;i < yValues.length; i++) {
|
|
4236
|
+
const originalRow = originalRows[i];
|
|
4237
|
+
output.push({
|
|
4238
|
+
x: groupIndex + result.offsets[i],
|
|
4239
|
+
y: yValues[i],
|
|
4240
|
+
xOriginal: groupKey,
|
|
4241
|
+
yOriginal: yValues[i],
|
|
4242
|
+
xOffset: result.offsets[i],
|
|
4243
|
+
...originalRow
|
|
4244
|
+
});
|
|
4245
|
+
}
|
|
4246
|
+
return output;
|
|
4247
|
+
}
|
|
4248
|
+
function stat_beeswarm(params = {}) {
|
|
4249
|
+
return {
|
|
4250
|
+
type: "beeswarm",
|
|
4251
|
+
compute(data, aes) {
|
|
4252
|
+
const groups = new Map;
|
|
4253
|
+
const groupOrder = [];
|
|
4254
|
+
for (const row of data) {
|
|
4255
|
+
const groupKey = String(row[aes.x] ?? "default");
|
|
4256
|
+
if (!groups.has(groupKey)) {
|
|
4257
|
+
groups.set(groupKey, []);
|
|
4258
|
+
groupOrder.push(groupKey);
|
|
4259
|
+
}
|
|
4260
|
+
groups.get(groupKey).push(row);
|
|
4261
|
+
}
|
|
4262
|
+
const result = [];
|
|
4263
|
+
let groupIndex = 0;
|
|
4264
|
+
for (const groupKey of groupOrder) {
|
|
4265
|
+
const groupData = groups.get(groupKey);
|
|
4266
|
+
const swarmResult = computeBeeswarm(groupData, aes.x, aes.y, groupKey, groupIndex, params);
|
|
4267
|
+
result.push(...swarmResult);
|
|
4268
|
+
groupIndex++;
|
|
4269
|
+
}
|
|
4270
|
+
return result;
|
|
4271
|
+
}
|
|
4272
|
+
};
|
|
4273
|
+
}
|
|
4274
|
+
|
|
3520
4275
|
// src/stats/smooth.ts
|
|
3521
4276
|
function linearRegression(xs, ys) {
|
|
3522
4277
|
const n = xs.length;
|
|
@@ -4396,6 +5151,15 @@ function applyStatTransform(data, geom, aes) {
|
|
|
4396
5151
|
adjust: geom.params.adjust
|
|
4397
5152
|
});
|
|
4398
5153
|
return xdensityStat.compute(data, aes);
|
|
5154
|
+
} else if (geom.stat === "beeswarm") {
|
|
5155
|
+
const beeswarmStat = stat_beeswarm({
|
|
5156
|
+
method: geom.params.method,
|
|
5157
|
+
cex: geom.params.cex,
|
|
5158
|
+
side: geom.params.side,
|
|
5159
|
+
priority: geom.params.priority,
|
|
5160
|
+
dodge: geom.params.dodge
|
|
5161
|
+
});
|
|
5162
|
+
return beeswarmStat.compute(data, aes);
|
|
4399
5163
|
} else if (geom.stat === "smooth") {
|
|
4400
5164
|
const smoothStat = stat_smooth({
|
|
4401
5165
|
method: geom.params.method,
|
|
@@ -4522,6 +5286,10 @@ function renderToCanvas(spec, options) {
|
|
|
4522
5286
|
scaleData = applyStatTransform(spec.data, geom, spec.aes);
|
|
4523
5287
|
scaleAes = { ...spec.aes, x: "x", y: "y" };
|
|
4524
5288
|
break;
|
|
5289
|
+
} else if (geom.stat === "beeswarm") {
|
|
5290
|
+
scaleData = spec.data;
|
|
5291
|
+
scaleAes = spec.aes;
|
|
5292
|
+
break;
|
|
4525
5293
|
}
|
|
4526
5294
|
}
|
|
4527
5295
|
scaleData = applyCoordTransform(scaleData, scaleAes, spec.coord);
|
|
@@ -4562,6 +5330,8 @@ function renderToCanvas(spec, options) {
|
|
|
4562
5330
|
geomAes = { ...spec.aes, x: "x", y: "y" };
|
|
4563
5331
|
} else if (geom.stat === "xdensity") {
|
|
4564
5332
|
geomAes = { ...spec.aes, x: "x", y: "y" };
|
|
5333
|
+
} else if (geom.stat === "beeswarm") {
|
|
5334
|
+
geomAes = { ...spec.aes, x: "x", y: "y" };
|
|
4565
5335
|
}
|
|
4566
5336
|
geomData = applyCoordTransform(geomData, geomAes, spec.coord);
|
|
4567
5337
|
}
|
|
@@ -5501,9 +6271,159 @@ var init_ridgeline = __esm(() => {
|
|
|
5501
6271
|
geom_joy = geom_ridgeline;
|
|
5502
6272
|
});
|
|
5503
6273
|
|
|
6274
|
+
// src/geoms/beeswarm.ts
|
|
6275
|
+
function geom_beeswarm(options = {}) {
|
|
6276
|
+
return {
|
|
6277
|
+
type: "beeswarm",
|
|
6278
|
+
stat: "beeswarm",
|
|
6279
|
+
position: "identity",
|
|
6280
|
+
params: {
|
|
6281
|
+
method: options.method ?? "swarm",
|
|
6282
|
+
size: options.size ?? 1,
|
|
6283
|
+
cex: options.cex ?? 1,
|
|
6284
|
+
alpha: options.alpha ?? 1,
|
|
6285
|
+
color: options.color,
|
|
6286
|
+
shape: options.shape ?? "circle",
|
|
6287
|
+
side: options.side ?? 0,
|
|
6288
|
+
priority: options.priority ?? "ascending",
|
|
6289
|
+
dodge: options.dodge ?? 0.8
|
|
6290
|
+
}
|
|
6291
|
+
};
|
|
6292
|
+
}
|
|
6293
|
+
function geom_quasirandom(options = {}) {
|
|
6294
|
+
return geom_beeswarm({ ...options, method: "center" });
|
|
6295
|
+
}
|
|
6296
|
+
|
|
6297
|
+
// src/geoms/dumbbell.ts
|
|
6298
|
+
function geom_dumbbell(options = {}) {
|
|
6299
|
+
return {
|
|
6300
|
+
type: "dumbbell",
|
|
6301
|
+
stat: "identity",
|
|
6302
|
+
position: "identity",
|
|
6303
|
+
params: {
|
|
6304
|
+
size: options.size ?? 2,
|
|
6305
|
+
sizeEnd: options.sizeEnd ?? options.size ?? 2,
|
|
6306
|
+
color: options.color,
|
|
6307
|
+
colorEnd: options.colorEnd ?? options.color,
|
|
6308
|
+
lineColor: options.lineColor ?? "#666666",
|
|
6309
|
+
lineWidth: options.lineWidth ?? 1,
|
|
6310
|
+
alpha: options.alpha ?? 1,
|
|
6311
|
+
shape: options.shape ?? "circle"
|
|
6312
|
+
}
|
|
6313
|
+
};
|
|
6314
|
+
}
|
|
6315
|
+
|
|
6316
|
+
// src/geoms/lollipop.ts
|
|
6317
|
+
function geom_lollipop(options = {}) {
|
|
6318
|
+
return {
|
|
6319
|
+
type: "lollipop",
|
|
6320
|
+
stat: "identity",
|
|
6321
|
+
position: "identity",
|
|
6322
|
+
params: {
|
|
6323
|
+
size: options.size ?? 2,
|
|
6324
|
+
color: options.color,
|
|
6325
|
+
lineColor: options.lineColor,
|
|
6326
|
+
lineWidth: options.lineWidth ?? 1,
|
|
6327
|
+
alpha: options.alpha ?? 1,
|
|
6328
|
+
shape: options.shape ?? "circle",
|
|
6329
|
+
direction: options.direction ?? "vertical",
|
|
6330
|
+
baseline: options.baseline ?? 0
|
|
6331
|
+
}
|
|
6332
|
+
};
|
|
6333
|
+
}
|
|
6334
|
+
|
|
6335
|
+
// src/geoms/waffle.ts
|
|
6336
|
+
function geom_waffle(options = {}) {
|
|
6337
|
+
return {
|
|
6338
|
+
type: "waffle",
|
|
6339
|
+
stat: "identity",
|
|
6340
|
+
position: "identity",
|
|
6341
|
+
params: {
|
|
6342
|
+
rows: options.rows ?? 10,
|
|
6343
|
+
cols: options.cols ?? 10,
|
|
6344
|
+
n_total: options.n_total ?? 100,
|
|
6345
|
+
fill_char: options.fill_char ?? "█",
|
|
6346
|
+
empty_char: options.empty_char ?? "░",
|
|
6347
|
+
alpha: options.alpha ?? 1,
|
|
6348
|
+
show_legend: options.show_legend ?? true,
|
|
6349
|
+
flip: options.flip ?? false,
|
|
6350
|
+
gap: options.gap ?? 0
|
|
6351
|
+
}
|
|
6352
|
+
};
|
|
6353
|
+
}
|
|
6354
|
+
|
|
6355
|
+
// src/geoms/sparkline.ts
|
|
6356
|
+
function geom_sparkline(options = {}) {
|
|
6357
|
+
return {
|
|
6358
|
+
type: "sparkline",
|
|
6359
|
+
stat: "identity",
|
|
6360
|
+
position: "identity",
|
|
6361
|
+
params: {
|
|
6362
|
+
sparkType: options.type ?? "bar",
|
|
6363
|
+
width: options.width ?? 20,
|
|
6364
|
+
height: options.height ?? 1,
|
|
6365
|
+
show_minmax: options.show_minmax ?? false,
|
|
6366
|
+
color: options.color,
|
|
6367
|
+
min_color: options.min_color ?? "#e74c3c",
|
|
6368
|
+
max_color: options.max_color ?? "#2ecc71",
|
|
6369
|
+
normalize: options.normalize ?? true
|
|
6370
|
+
}
|
|
6371
|
+
};
|
|
6372
|
+
}
|
|
6373
|
+
var SPARK_BARS, SPARK_DOTS;
|
|
6374
|
+
var init_sparkline = __esm(() => {
|
|
6375
|
+
SPARK_BARS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
|
|
6376
|
+
SPARK_DOTS = ["⠀", "⢀", "⢠", "⢰", "⢸", "⣸", "⣾", "⣿"];
|
|
6377
|
+
});
|
|
6378
|
+
|
|
6379
|
+
// src/geoms/bullet.ts
|
|
6380
|
+
function geom_bullet(options = {}) {
|
|
6381
|
+
return {
|
|
6382
|
+
type: "bullet",
|
|
6383
|
+
stat: "identity",
|
|
6384
|
+
position: "identity",
|
|
6385
|
+
params: {
|
|
6386
|
+
width: options.width ?? 40,
|
|
6387
|
+
height: options.height ?? 1,
|
|
6388
|
+
target_char: options.target_char ?? "│",
|
|
6389
|
+
bar_char: options.bar_char ?? "█",
|
|
6390
|
+
range_chars: options.range_chars ?? ["░", "▒", "▓"],
|
|
6391
|
+
show_values: options.show_values ?? true,
|
|
6392
|
+
color: options.color,
|
|
6393
|
+
target_color: options.target_color ?? "#e74c3c",
|
|
6394
|
+
orientation: options.orientation ?? "horizontal"
|
|
6395
|
+
}
|
|
6396
|
+
};
|
|
6397
|
+
}
|
|
6398
|
+
|
|
6399
|
+
// src/geoms/braille.ts
|
|
6400
|
+
function geom_braille(options = {}) {
|
|
6401
|
+
return {
|
|
6402
|
+
type: "braille",
|
|
6403
|
+
stat: "identity",
|
|
6404
|
+
position: "identity",
|
|
6405
|
+
params: {
|
|
6406
|
+
brailleType: options.type ?? "point",
|
|
6407
|
+
color: options.color,
|
|
6408
|
+
fill: options.fill ?? false,
|
|
6409
|
+
alpha: options.alpha ?? 1,
|
|
6410
|
+
dot_size: options.dot_size ?? 1
|
|
6411
|
+
}
|
|
6412
|
+
};
|
|
6413
|
+
}
|
|
6414
|
+
var BRAILLE_BASE = 10240, BRAILLE_DOTS;
|
|
6415
|
+
var init_braille = __esm(() => {
|
|
6416
|
+
BRAILLE_DOTS = [
|
|
6417
|
+
[1, 2, 4, 64],
|
|
6418
|
+
[8, 16, 32, 128]
|
|
6419
|
+
];
|
|
6420
|
+
});
|
|
6421
|
+
|
|
5504
6422
|
// src/geoms/index.ts
|
|
5505
6423
|
var init_geoms = __esm(() => {
|
|
5506
6424
|
init_ridgeline();
|
|
6425
|
+
init_sparkline();
|
|
6426
|
+
init_braille();
|
|
5507
6427
|
});
|
|
5508
6428
|
|
|
5509
6429
|
// src/scales/continuous.ts
|