@constela/runtime 2.0.4 → 2.0.6
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/index.js +3 -543
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -376,6 +376,7 @@ function createTypedStateStore(definitions) {
|
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
// src/expression/evaluator.ts
|
|
379
|
+
import { CHART_HELPERS } from "@constela/core";
|
|
379
380
|
var SAFE_ARRAY_METHODS = /* @__PURE__ */ new Set([
|
|
380
381
|
"length",
|
|
381
382
|
"at",
|
|
@@ -958,27 +959,8 @@ var GLOBAL_FUNCTIONS = {
|
|
|
958
959
|
// Virtual scroll helpers
|
|
959
960
|
getVisibleRange: (scrollTop, itemHeight, containerHeight, overscan) => getVisibleRange(scrollTop, itemHeight, containerHeight, overscan),
|
|
960
961
|
getTotalHeight: (itemCount, itemHeight) => getTotalHeight(itemCount, itemHeight),
|
|
961
|
-
// Chart helpers
|
|
962
|
-
|
|
963
|
-
scaleValue: (value, domainMin, domainMax, rangeMin, rangeMax) => scaleValue(value, domainMin, domainMax, rangeMin, rangeMax),
|
|
964
|
-
getBarDimensions: (data, index, width, height, gap, orientation) => getBarDimensions(data, index, width, height, gap, orientation),
|
|
965
|
-
// Chart helpers - Path generation
|
|
966
|
-
getLinePath: (points, curved) => getLinePath(points, curved),
|
|
967
|
-
getAreaPath: (points, baseline, curved) => getAreaPath(points, baseline, curved),
|
|
968
|
-
getArcPath: (cx, cy, radius, startAngle, endAngle) => getArcPath(cx, cy, radius, startAngle, endAngle),
|
|
969
|
-
// Chart helpers - Pie/Donut
|
|
970
|
-
getPieSlices: (data, valueKey) => getPieSlices(data, valueKey),
|
|
971
|
-
getDonutSlices: (data, valueKey, innerRadius) => getDonutSlices(data, valueKey, innerRadius),
|
|
972
|
-
// Chart helpers - Radar
|
|
973
|
-
getRadarPoints: (data, valueKey, cx, cy, radius, maxValue) => getRadarPoints(data, valueKey, cx, cy, radius, maxValue),
|
|
974
|
-
getRadarAxes: (labels, cx, cy, radius) => getRadarAxes(labels, cx, cy, radius),
|
|
975
|
-
// Chart helpers - Utilities
|
|
976
|
-
getChartBounds: (data, valueKey) => getChartBounds(data, valueKey),
|
|
977
|
-
generateTicks: (min, max, count) => generateTicks(min, max, count),
|
|
978
|
-
// Chart helpers - Data aggregation
|
|
979
|
-
binData: (data, valueKey, binCount) => binData(data, valueKey, binCount),
|
|
980
|
-
aggregateData: (data, groupKey, valueKey, aggregation) => aggregateData(data, groupKey, valueKey, aggregation),
|
|
981
|
-
downsample: (data, targetCount, method) => downsample(data, targetCount, method)
|
|
962
|
+
// Chart helpers (from @constela/core)
|
|
963
|
+
...CHART_HELPERS
|
|
982
964
|
};
|
|
983
965
|
function callGlobalFunction(method, args) {
|
|
984
966
|
const fn = GLOBAL_FUNCTIONS[method];
|
|
@@ -1311,528 +1293,6 @@ function evaluateBinary(op, left, right, ctx) {
|
|
|
1311
1293
|
throw new Error("Unknown binary operator: " + op);
|
|
1312
1294
|
}
|
|
1313
1295
|
}
|
|
1314
|
-
function normalizeValue(value, min, max) {
|
|
1315
|
-
if (typeof value !== "number" || typeof min !== "number" || typeof max !== "number") {
|
|
1316
|
-
return void 0;
|
|
1317
|
-
}
|
|
1318
|
-
if (max === min) {
|
|
1319
|
-
return 0;
|
|
1320
|
-
}
|
|
1321
|
-
return (value - min) / (max - min);
|
|
1322
|
-
}
|
|
1323
|
-
function scaleValue(value, domainMin, domainMax, rangeMin, rangeMax) {
|
|
1324
|
-
if (typeof value !== "number" || typeof domainMin !== "number" || typeof domainMax !== "number" || typeof rangeMin !== "number" || typeof rangeMax !== "number") {
|
|
1325
|
-
return void 0;
|
|
1326
|
-
}
|
|
1327
|
-
if (domainMax === domainMin) {
|
|
1328
|
-
return rangeMin;
|
|
1329
|
-
}
|
|
1330
|
-
const normalized = (value - domainMin) / (domainMax - domainMin);
|
|
1331
|
-
return rangeMin + normalized * (rangeMax - rangeMin);
|
|
1332
|
-
}
|
|
1333
|
-
function getBarDimensions(data, index, width, height, gap, orientation) {
|
|
1334
|
-
if (!Array.isArray(data) || data.length === 0) {
|
|
1335
|
-
return void 0;
|
|
1336
|
-
}
|
|
1337
|
-
if (typeof index !== "number" || index < 0 || index >= data.length) {
|
|
1338
|
-
return void 0;
|
|
1339
|
-
}
|
|
1340
|
-
if (typeof width !== "number" || typeof height !== "number" || typeof gap !== "number") {
|
|
1341
|
-
return void 0;
|
|
1342
|
-
}
|
|
1343
|
-
const isVertical = orientation === "vertical";
|
|
1344
|
-
const barCount = data.length;
|
|
1345
|
-
const values = data.map((d2) => typeof d2 === "number" ? d2 : 0);
|
|
1346
|
-
const maxValue = Math.max(...values);
|
|
1347
|
-
if (isVertical) {
|
|
1348
|
-
const totalGap = gap * (barCount + 1);
|
|
1349
|
-
const barWidth = (width - totalGap) / barCount;
|
|
1350
|
-
const barX = gap + index * (barWidth + gap);
|
|
1351
|
-
const value = values[index] ?? 0;
|
|
1352
|
-
const barHeight = maxValue > 0 ? value / maxValue * height : 0;
|
|
1353
|
-
const barY = height - barHeight;
|
|
1354
|
-
return {
|
|
1355
|
-
x: barX,
|
|
1356
|
-
y: barY,
|
|
1357
|
-
width: barWidth,
|
|
1358
|
-
height: barHeight
|
|
1359
|
-
};
|
|
1360
|
-
} else {
|
|
1361
|
-
const totalGap = gap * barCount;
|
|
1362
|
-
const barHeight = (height - totalGap) / barCount;
|
|
1363
|
-
const barY = gap + index * (barHeight + gap);
|
|
1364
|
-
const value = values[index] ?? 0;
|
|
1365
|
-
const barWidth = maxValue > 0 ? value / maxValue * width : 0;
|
|
1366
|
-
return {
|
|
1367
|
-
x: 0,
|
|
1368
|
-
y: barY,
|
|
1369
|
-
width: barWidth,
|
|
1370
|
-
height: barHeight
|
|
1371
|
-
};
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
function getCurvedPath(points) {
|
|
1375
|
-
if (points.length < 2) {
|
|
1376
|
-
return points.length === 1 ? `M${points[0].x},${points[0].y}` : "";
|
|
1377
|
-
}
|
|
1378
|
-
if (points.length === 2) {
|
|
1379
|
-
return `M${points[0].x},${points[0].y} L${points[1].x},${points[1].y}`;
|
|
1380
|
-
}
|
|
1381
|
-
const pathParts = [`M${points[0].x},${points[0].y}`];
|
|
1382
|
-
for (let i = 0; i < points.length - 1; i++) {
|
|
1383
|
-
const p0 = points[Math.max(0, i - 1)];
|
|
1384
|
-
const p1 = points[i];
|
|
1385
|
-
const p2 = points[i + 1];
|
|
1386
|
-
const p3 = points[Math.min(points.length - 1, i + 2)];
|
|
1387
|
-
const cp1x = p1.x + (p2.x - p0.x) / 6;
|
|
1388
|
-
const cp1y = p1.y + (p2.y - p0.y) / 6;
|
|
1389
|
-
const cp2x = p2.x - (p3.x - p1.x) / 6;
|
|
1390
|
-
const cp2y = p2.y - (p3.y - p1.y) / 6;
|
|
1391
|
-
pathParts.push(`C${cp1x},${cp1y} ${cp2x},${cp2y} ${p2.x},${p2.y}`);
|
|
1392
|
-
}
|
|
1393
|
-
return pathParts.join(" ");
|
|
1394
|
-
}
|
|
1395
|
-
function getLinePath(points, curved) {
|
|
1396
|
-
if (!Array.isArray(points)) {
|
|
1397
|
-
return void 0;
|
|
1398
|
-
}
|
|
1399
|
-
if (points.length === 0) {
|
|
1400
|
-
return "";
|
|
1401
|
-
}
|
|
1402
|
-
for (const point of points) {
|
|
1403
|
-
if (typeof point !== "object" || point === null || typeof point.x !== "number" || typeof point.y !== "number") {
|
|
1404
|
-
return void 0;
|
|
1405
|
-
}
|
|
1406
|
-
}
|
|
1407
|
-
const validPoints = points;
|
|
1408
|
-
if (validPoints.length === 1) {
|
|
1409
|
-
return `M${validPoints[0].x},${validPoints[0].y}`;
|
|
1410
|
-
}
|
|
1411
|
-
if (curved !== true) {
|
|
1412
|
-
const pathParts = validPoints.map((point, i) => {
|
|
1413
|
-
const command = i === 0 ? "M" : "L";
|
|
1414
|
-
return `${command}${point.x},${point.y}`;
|
|
1415
|
-
});
|
|
1416
|
-
return pathParts.join(" ");
|
|
1417
|
-
}
|
|
1418
|
-
return getCurvedPath(validPoints);
|
|
1419
|
-
}
|
|
1420
|
-
function getAreaPath(points, baseline, curved) {
|
|
1421
|
-
if (!Array.isArray(points)) {
|
|
1422
|
-
return void 0;
|
|
1423
|
-
}
|
|
1424
|
-
if (typeof baseline !== "number") {
|
|
1425
|
-
return void 0;
|
|
1426
|
-
}
|
|
1427
|
-
if (points.length === 0) {
|
|
1428
|
-
return "";
|
|
1429
|
-
}
|
|
1430
|
-
for (const point of points) {
|
|
1431
|
-
if (typeof point !== "object" || point === null || typeof point.x !== "number" || typeof point.y !== "number") {
|
|
1432
|
-
return void 0;
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
const validPoints = points;
|
|
1436
|
-
let upperPath;
|
|
1437
|
-
if (curved === true && validPoints.length > 2) {
|
|
1438
|
-
upperPath = getCurvedPath(validPoints);
|
|
1439
|
-
} else {
|
|
1440
|
-
upperPath = validPoints.map((p2, i) => (i === 0 ? "M" : "L") + `${p2.x},${p2.y}`).join(" ");
|
|
1441
|
-
}
|
|
1442
|
-
const lastPoint = validPoints[validPoints.length - 1];
|
|
1443
|
-
const firstPoint = validPoints[0];
|
|
1444
|
-
return `${upperPath} L${lastPoint.x},${baseline} L${firstPoint.x},${baseline} Z`;
|
|
1445
|
-
}
|
|
1446
|
-
function getArcPath(cx, cy, radius, startAngle, endAngle) {
|
|
1447
|
-
if (typeof cx !== "number" || typeof cy !== "number" || typeof radius !== "number" || typeof startAngle !== "number" || typeof endAngle !== "number") {
|
|
1448
|
-
return void 0;
|
|
1449
|
-
}
|
|
1450
|
-
if (radius <= 0) {
|
|
1451
|
-
return void 0;
|
|
1452
|
-
}
|
|
1453
|
-
const x1 = cx + radius * Math.cos(startAngle);
|
|
1454
|
-
const y1 = cy + radius * Math.sin(startAngle);
|
|
1455
|
-
const x2 = cx + radius * Math.cos(endAngle);
|
|
1456
|
-
const y2 = cy + radius * Math.sin(endAngle);
|
|
1457
|
-
const angleDiff = endAngle - startAngle;
|
|
1458
|
-
const largeArcFlag = Math.abs(angleDiff) > Math.PI ? 1 : 0;
|
|
1459
|
-
return `M${x1},${y1} A${radius},${radius} 0 ${largeArcFlag},1 ${x2},${y2}`;
|
|
1460
|
-
}
|
|
1461
|
-
function getPieSlices(data, valueKey) {
|
|
1462
|
-
if (!Array.isArray(data)) {
|
|
1463
|
-
return void 0;
|
|
1464
|
-
}
|
|
1465
|
-
if (typeof valueKey !== "string") {
|
|
1466
|
-
return void 0;
|
|
1467
|
-
}
|
|
1468
|
-
if (data.length === 0) {
|
|
1469
|
-
return [];
|
|
1470
|
-
}
|
|
1471
|
-
const values = data.map((item) => {
|
|
1472
|
-
if (typeof item !== "object" || item === null) return 0;
|
|
1473
|
-
const val = item[valueKey];
|
|
1474
|
-
return typeof val === "number" ? val : 0;
|
|
1475
|
-
});
|
|
1476
|
-
const total = values.reduce((sum, val) => sum + val, 0);
|
|
1477
|
-
const slices = [];
|
|
1478
|
-
let currentAngle = 0;
|
|
1479
|
-
for (let i = 0; i < values.length; i++) {
|
|
1480
|
-
const value = values[i];
|
|
1481
|
-
const percentage = total > 0 ? value / total * 100 : 0;
|
|
1482
|
-
const angleSpan = total > 0 ? value / total * Math.PI * 2 : 0;
|
|
1483
|
-
slices.push({
|
|
1484
|
-
startAngle: currentAngle,
|
|
1485
|
-
endAngle: currentAngle + angleSpan,
|
|
1486
|
-
value,
|
|
1487
|
-
percentage
|
|
1488
|
-
});
|
|
1489
|
-
currentAngle += angleSpan;
|
|
1490
|
-
}
|
|
1491
|
-
return slices;
|
|
1492
|
-
}
|
|
1493
|
-
function getDonutSlices(data, valueKey, innerRadius) {
|
|
1494
|
-
if (typeof innerRadius !== "number" || innerRadius < 0) {
|
|
1495
|
-
return void 0;
|
|
1496
|
-
}
|
|
1497
|
-
const pieSlices = getPieSlices(data, valueKey);
|
|
1498
|
-
if (pieSlices === void 0) {
|
|
1499
|
-
return void 0;
|
|
1500
|
-
}
|
|
1501
|
-
const outerRadius = 100;
|
|
1502
|
-
return pieSlices.map((slice) => ({
|
|
1503
|
-
...slice,
|
|
1504
|
-
outerRadius,
|
|
1505
|
-
innerRadius
|
|
1506
|
-
}));
|
|
1507
|
-
}
|
|
1508
|
-
function getRadarPoints(data, valueKey, cx, cy, radius, maxValue) {
|
|
1509
|
-
if (!Array.isArray(data)) {
|
|
1510
|
-
return void 0;
|
|
1511
|
-
}
|
|
1512
|
-
if (typeof valueKey !== "string") {
|
|
1513
|
-
return void 0;
|
|
1514
|
-
}
|
|
1515
|
-
if (typeof cx !== "number" || typeof cy !== "number" || typeof radius !== "number" || typeof maxValue !== "number") {
|
|
1516
|
-
return void 0;
|
|
1517
|
-
}
|
|
1518
|
-
if (maxValue <= 0) {
|
|
1519
|
-
return void 0;
|
|
1520
|
-
}
|
|
1521
|
-
if (data.length === 0) {
|
|
1522
|
-
return [];
|
|
1523
|
-
}
|
|
1524
|
-
const points = [];
|
|
1525
|
-
const angleStep = Math.PI * 2 / data.length;
|
|
1526
|
-
for (let i = 0; i < data.length; i++) {
|
|
1527
|
-
const item = data[i];
|
|
1528
|
-
const value = typeof item === "object" && item !== null ? item[valueKey] : 0;
|
|
1529
|
-
const numValue = typeof value === "number" ? value : 0;
|
|
1530
|
-
const scaledRadius = numValue / maxValue * radius;
|
|
1531
|
-
const angle = -Math.PI / 2 + i * angleStep;
|
|
1532
|
-
const x2 = cx + scaledRadius * Math.cos(angle);
|
|
1533
|
-
const y2 = cy + scaledRadius * Math.sin(angle);
|
|
1534
|
-
points.push({ x: x2, y: y2 });
|
|
1535
|
-
}
|
|
1536
|
-
return points;
|
|
1537
|
-
}
|
|
1538
|
-
function getRadarAxes(labels, cx, cy, radius) {
|
|
1539
|
-
if (!Array.isArray(labels)) {
|
|
1540
|
-
return void 0;
|
|
1541
|
-
}
|
|
1542
|
-
if (typeof cx !== "number" || typeof cy !== "number" || typeof radius !== "number") {
|
|
1543
|
-
return void 0;
|
|
1544
|
-
}
|
|
1545
|
-
if (radius < 0) {
|
|
1546
|
-
return void 0;
|
|
1547
|
-
}
|
|
1548
|
-
if (labels.length === 0) {
|
|
1549
|
-
return [];
|
|
1550
|
-
}
|
|
1551
|
-
const axes = [];
|
|
1552
|
-
const angleStep = Math.PI * 2 / labels.length;
|
|
1553
|
-
for (let i = 0; i < labels.length; i++) {
|
|
1554
|
-
const label = String(labels[i]);
|
|
1555
|
-
const angle = -Math.PI / 2 + i * angleStep;
|
|
1556
|
-
const x2 = cx + radius * Math.cos(angle);
|
|
1557
|
-
const y2 = cy + radius * Math.sin(angle);
|
|
1558
|
-
axes.push({
|
|
1559
|
-
x1: cx,
|
|
1560
|
-
y1: cy,
|
|
1561
|
-
x2,
|
|
1562
|
-
y2,
|
|
1563
|
-
label,
|
|
1564
|
-
angle
|
|
1565
|
-
});
|
|
1566
|
-
}
|
|
1567
|
-
return axes;
|
|
1568
|
-
}
|
|
1569
|
-
function getChartBounds(data, valueKey) {
|
|
1570
|
-
if (!Array.isArray(data) || data.length === 0) {
|
|
1571
|
-
return void 0;
|
|
1572
|
-
}
|
|
1573
|
-
if (typeof valueKey !== "string") {
|
|
1574
|
-
return void 0;
|
|
1575
|
-
}
|
|
1576
|
-
const values = [];
|
|
1577
|
-
for (const item of data) {
|
|
1578
|
-
if (typeof item !== "object" || item === null) {
|
|
1579
|
-
continue;
|
|
1580
|
-
}
|
|
1581
|
-
const val = item[valueKey];
|
|
1582
|
-
if (typeof val === "number") {
|
|
1583
|
-
values.push(val);
|
|
1584
|
-
}
|
|
1585
|
-
}
|
|
1586
|
-
if (values.length === 0) {
|
|
1587
|
-
return void 0;
|
|
1588
|
-
}
|
|
1589
|
-
return {
|
|
1590
|
-
min: Math.min(...values),
|
|
1591
|
-
max: Math.max(...values)
|
|
1592
|
-
};
|
|
1593
|
-
}
|
|
1594
|
-
function generateTicks(min, max, count) {
|
|
1595
|
-
if (typeof min !== "number" || typeof max !== "number" || typeof count !== "number") {
|
|
1596
|
-
return [];
|
|
1597
|
-
}
|
|
1598
|
-
if (count <= 0) {
|
|
1599
|
-
return [];
|
|
1600
|
-
}
|
|
1601
|
-
if (min === max) {
|
|
1602
|
-
return [min];
|
|
1603
|
-
}
|
|
1604
|
-
if (count === 1) {
|
|
1605
|
-
return [min];
|
|
1606
|
-
}
|
|
1607
|
-
if (count === 2) {
|
|
1608
|
-
return [min, max];
|
|
1609
|
-
}
|
|
1610
|
-
const range = max - min;
|
|
1611
|
-
const rawStep = range / (count - 1);
|
|
1612
|
-
const niceStep = getNiceStep(rawStep);
|
|
1613
|
-
const niceMin = Math.floor(min / niceStep) * niceStep;
|
|
1614
|
-
const ticks = [];
|
|
1615
|
-
for (let i = 0; i < count; i++) {
|
|
1616
|
-
const tick = niceMin + i * niceStep;
|
|
1617
|
-
ticks.push(Math.round(tick * 1e10) / 1e10);
|
|
1618
|
-
}
|
|
1619
|
-
return ticks;
|
|
1620
|
-
}
|
|
1621
|
-
function getNiceStep(rawStep) {
|
|
1622
|
-
const magnitude = Math.pow(10, Math.floor(Math.log10(rawStep)));
|
|
1623
|
-
const normalized = rawStep / magnitude;
|
|
1624
|
-
let niceNormalized;
|
|
1625
|
-
if (normalized <= 1) {
|
|
1626
|
-
niceNormalized = 1;
|
|
1627
|
-
} else if (normalized <= 2) {
|
|
1628
|
-
niceNormalized = 2;
|
|
1629
|
-
} else if (normalized <= 2.5) {
|
|
1630
|
-
niceNormalized = 2.5;
|
|
1631
|
-
} else if (normalized <= 5) {
|
|
1632
|
-
niceNormalized = 5;
|
|
1633
|
-
} else {
|
|
1634
|
-
niceNormalized = 10;
|
|
1635
|
-
}
|
|
1636
|
-
return niceNormalized * magnitude;
|
|
1637
|
-
}
|
|
1638
|
-
function binData(data, valueKey, binCount) {
|
|
1639
|
-
if (!Array.isArray(data)) {
|
|
1640
|
-
return void 0;
|
|
1641
|
-
}
|
|
1642
|
-
if (typeof valueKey !== "string") {
|
|
1643
|
-
return void 0;
|
|
1644
|
-
}
|
|
1645
|
-
if (typeof binCount !== "number" || binCount <= 0) {
|
|
1646
|
-
return [];
|
|
1647
|
-
}
|
|
1648
|
-
if (data.length === 0) {
|
|
1649
|
-
return [];
|
|
1650
|
-
}
|
|
1651
|
-
const values = [];
|
|
1652
|
-
for (const item of data) {
|
|
1653
|
-
if (typeof item !== "object" || item === null) continue;
|
|
1654
|
-
const val = item[valueKey];
|
|
1655
|
-
if (typeof val === "number") {
|
|
1656
|
-
values.push(val);
|
|
1657
|
-
}
|
|
1658
|
-
}
|
|
1659
|
-
if (values.length === 0) {
|
|
1660
|
-
return [];
|
|
1661
|
-
}
|
|
1662
|
-
const minVal = Math.min(...values);
|
|
1663
|
-
const maxVal = Math.max(...values);
|
|
1664
|
-
const binWidth = maxVal === minVal ? 1 : (maxVal - minVal) / binCount;
|
|
1665
|
-
const bins = [];
|
|
1666
|
-
for (let i = 0; i < binCount; i++) {
|
|
1667
|
-
bins.push({
|
|
1668
|
-
binStart: minVal + i * binWidth,
|
|
1669
|
-
binEnd: minVal + (i + 1) * binWidth,
|
|
1670
|
-
count: 0,
|
|
1671
|
-
values: []
|
|
1672
|
-
});
|
|
1673
|
-
}
|
|
1674
|
-
for (const val of values) {
|
|
1675
|
-
let binIndex = Math.floor((val - minVal) / binWidth);
|
|
1676
|
-
if (binIndex >= binCount) {
|
|
1677
|
-
binIndex = binCount - 1;
|
|
1678
|
-
}
|
|
1679
|
-
bins[binIndex].count++;
|
|
1680
|
-
bins[binIndex].values.push(val);
|
|
1681
|
-
}
|
|
1682
|
-
return bins;
|
|
1683
|
-
}
|
|
1684
|
-
function aggregateData(data, groupKey, valueKey, aggregation) {
|
|
1685
|
-
if (!Array.isArray(data)) {
|
|
1686
|
-
return void 0;
|
|
1687
|
-
}
|
|
1688
|
-
if (typeof groupKey !== "string" || typeof valueKey !== "string") {
|
|
1689
|
-
return void 0;
|
|
1690
|
-
}
|
|
1691
|
-
const validAggregations = /* @__PURE__ */ new Set(["sum", "avg", "min", "max", "count"]);
|
|
1692
|
-
if (typeof aggregation !== "string" || !validAggregations.has(aggregation)) {
|
|
1693
|
-
return void 0;
|
|
1694
|
-
}
|
|
1695
|
-
if (data.length === 0) {
|
|
1696
|
-
return [];
|
|
1697
|
-
}
|
|
1698
|
-
const groups = /* @__PURE__ */ new Map();
|
|
1699
|
-
for (const item of data) {
|
|
1700
|
-
if (typeof item !== "object" || item === null) continue;
|
|
1701
|
-
const obj = item;
|
|
1702
|
-
const group = String(obj[groupKey] ?? "");
|
|
1703
|
-
const val = obj[valueKey];
|
|
1704
|
-
const numVal = typeof val === "number" ? val : 0;
|
|
1705
|
-
if (!groups.has(group)) {
|
|
1706
|
-
groups.set(group, []);
|
|
1707
|
-
}
|
|
1708
|
-
groups.get(group).push(numVal);
|
|
1709
|
-
}
|
|
1710
|
-
const result = [];
|
|
1711
|
-
for (const [group, values] of groups) {
|
|
1712
|
-
let aggregatedValue;
|
|
1713
|
-
switch (aggregation) {
|
|
1714
|
-
case "sum":
|
|
1715
|
-
aggregatedValue = values.reduce((sum, v2) => sum + v2, 0);
|
|
1716
|
-
break;
|
|
1717
|
-
case "avg":
|
|
1718
|
-
aggregatedValue = values.reduce((sum, v2) => sum + v2, 0) / values.length;
|
|
1719
|
-
break;
|
|
1720
|
-
case "min":
|
|
1721
|
-
aggregatedValue = Math.min(...values);
|
|
1722
|
-
break;
|
|
1723
|
-
case "max":
|
|
1724
|
-
aggregatedValue = Math.max(...values);
|
|
1725
|
-
break;
|
|
1726
|
-
case "count":
|
|
1727
|
-
aggregatedValue = values.length;
|
|
1728
|
-
break;
|
|
1729
|
-
default:
|
|
1730
|
-
aggregatedValue = 0;
|
|
1731
|
-
}
|
|
1732
|
-
result.push({ group, value: aggregatedValue });
|
|
1733
|
-
}
|
|
1734
|
-
return result;
|
|
1735
|
-
}
|
|
1736
|
-
function downsample(data, targetCount, method) {
|
|
1737
|
-
if (!Array.isArray(data)) {
|
|
1738
|
-
return void 0;
|
|
1739
|
-
}
|
|
1740
|
-
if (typeof targetCount !== "number" || targetCount <= 0) {
|
|
1741
|
-
return void 0;
|
|
1742
|
-
}
|
|
1743
|
-
const validMethods = /* @__PURE__ */ new Set(["uniform", "lttb"]);
|
|
1744
|
-
if (typeof method !== "string" || !validMethods.has(method)) {
|
|
1745
|
-
return void 0;
|
|
1746
|
-
}
|
|
1747
|
-
if (data.length === 0) {
|
|
1748
|
-
return [];
|
|
1749
|
-
}
|
|
1750
|
-
if (targetCount >= data.length) {
|
|
1751
|
-
return data;
|
|
1752
|
-
}
|
|
1753
|
-
if (method === "uniform") {
|
|
1754
|
-
return downsampleUniform(data, targetCount);
|
|
1755
|
-
} else {
|
|
1756
|
-
return downsampleLTTB(data, targetCount);
|
|
1757
|
-
}
|
|
1758
|
-
}
|
|
1759
|
-
function downsampleUniform(data, targetCount) {
|
|
1760
|
-
if (targetCount === 1) {
|
|
1761
|
-
return [data[0]];
|
|
1762
|
-
}
|
|
1763
|
-
if (targetCount === 2) {
|
|
1764
|
-
return [data[0], data[data.length - 1]];
|
|
1765
|
-
}
|
|
1766
|
-
const result = [];
|
|
1767
|
-
const step = (data.length - 1) / (targetCount - 1);
|
|
1768
|
-
for (let i = 0; i < targetCount; i++) {
|
|
1769
|
-
const index = Math.round(i * step);
|
|
1770
|
-
result.push(data[index]);
|
|
1771
|
-
}
|
|
1772
|
-
return result;
|
|
1773
|
-
}
|
|
1774
|
-
function downsampleLTTB(data, targetCount) {
|
|
1775
|
-
if (targetCount === 1) {
|
|
1776
|
-
return [data[0]];
|
|
1777
|
-
}
|
|
1778
|
-
if (targetCount === 2) {
|
|
1779
|
-
return [data[0], data[data.length - 1]];
|
|
1780
|
-
}
|
|
1781
|
-
const getXY = (point) => {
|
|
1782
|
-
if (typeof point !== "object" || point === null) {
|
|
1783
|
-
return { x: 0, y: 0 };
|
|
1784
|
-
}
|
|
1785
|
-
const obj = point;
|
|
1786
|
-
const x2 = typeof obj["x"] === "number" ? obj["x"] : typeof obj["timestamp"] === "number" ? obj["timestamp"] : 0;
|
|
1787
|
-
const y2 = typeof obj["y"] === "number" ? obj["y"] : typeof obj["value"] === "number" ? obj["value"] : 0;
|
|
1788
|
-
return { x: x2, y: y2 };
|
|
1789
|
-
};
|
|
1790
|
-
const result = [];
|
|
1791
|
-
result.push(data[0]);
|
|
1792
|
-
const numBuckets = targetCount - 2;
|
|
1793
|
-
const middleData = data.length - 2;
|
|
1794
|
-
const bucketSize = middleData / numBuckets;
|
|
1795
|
-
let prevSelectedIndex = 0;
|
|
1796
|
-
for (let bucketIndex = 0; bucketIndex < numBuckets; bucketIndex++) {
|
|
1797
|
-
const bucketStart = Math.floor(bucketIndex * bucketSize) + 1;
|
|
1798
|
-
const bucketEnd = Math.floor((bucketIndex + 1) * bucketSize) + 1;
|
|
1799
|
-
const nextBucketStart = bucketEnd;
|
|
1800
|
-
const nextBucketEnd = bucketIndex < numBuckets - 1 ? Math.floor((bucketIndex + 2) * bucketSize) + 1 : data.length;
|
|
1801
|
-
let avgX = 0;
|
|
1802
|
-
let avgY = 0;
|
|
1803
|
-
const nextLen = nextBucketEnd - nextBucketStart;
|
|
1804
|
-
if (nextLen > 0) {
|
|
1805
|
-
for (let j2 = nextBucketStart; j2 < nextBucketEnd; j2++) {
|
|
1806
|
-
const point = getXY(data[j2]);
|
|
1807
|
-
avgX += point.x;
|
|
1808
|
-
avgY += point.y;
|
|
1809
|
-
}
|
|
1810
|
-
avgX /= nextLen;
|
|
1811
|
-
avgY /= nextLen;
|
|
1812
|
-
} else {
|
|
1813
|
-
const lastPoint = getXY(data[data.length - 1]);
|
|
1814
|
-
avgX = lastPoint.x;
|
|
1815
|
-
avgY = lastPoint.y;
|
|
1816
|
-
}
|
|
1817
|
-
const pointA = getXY(data[prevSelectedIndex]);
|
|
1818
|
-
let maxArea = -1;
|
|
1819
|
-
let maxAreaIndex = bucketStart;
|
|
1820
|
-
for (let j2 = bucketStart; j2 < bucketEnd; j2++) {
|
|
1821
|
-
const pointB = getXY(data[j2]);
|
|
1822
|
-
const area = Math.abs(
|
|
1823
|
-
pointA.x * (pointB.y - avgY) + pointB.x * (avgY - pointA.y) + avgX * (pointA.y - pointB.y)
|
|
1824
|
-
);
|
|
1825
|
-
if (area > maxArea) {
|
|
1826
|
-
maxArea = area;
|
|
1827
|
-
maxAreaIndex = j2;
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
result.push(data[maxAreaIndex]);
|
|
1831
|
-
prevSelectedIndex = maxAreaIndex;
|
|
1832
|
-
}
|
|
1833
|
-
result.push(data[data.length - 1]);
|
|
1834
|
-
return result;
|
|
1835
|
-
}
|
|
1836
1296
|
function evaluateStyle(expr, ctx) {
|
|
1837
1297
|
const preset = ctx.styles?.[expr.name];
|
|
1838
1298
|
if (!preset) return void 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constela/runtime",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "Runtime DOM renderer for Constela UI framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"dompurify": "^3.3.1",
|
|
19
19
|
"marked": "^17.0.1",
|
|
20
20
|
"shiki": "^3.20.0",
|
|
21
|
-
"@constela/
|
|
22
|
-
"@constela/
|
|
21
|
+
"@constela/core": "0.18.3",
|
|
22
|
+
"@constela/compiler": "0.15.9"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/dompurify": "^3.2.0",
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"tsup": "^8.0.0",
|
|
30
30
|
"typescript": "^5.3.0",
|
|
31
31
|
"vitest": "^2.0.0",
|
|
32
|
-
"@constela/server": "14.0.
|
|
32
|
+
"@constela/server": "14.0.4"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@constela/ai": "3.0.
|
|
35
|
+
"@constela/ai": "3.0.3"
|
|
36
36
|
},
|
|
37
37
|
"peerDependenciesMeta": {
|
|
38
38
|
"@constela/ai": {
|