@lobehub/charts 6.0.0 → 6.1.0
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/README.md +83 -74
- package/es/AccuracyBarChart/index.d.mts +7 -5
- package/es/AccuracyBarChart/index.mjs +36 -37
- package/es/AccuracyBarChart/index.mjs.map +1 -1
- package/es/AccuracyBarChart/styles.d.mts +1 -4
- package/es/AccuracyBarChart/styles.mjs +6 -7
- package/es/AccuracyBarChart/styles.mjs.map +1 -1
- package/es/AreaChart/index.d.mts +11 -7
- package/es/AreaChart/index.mjs +100 -60
- package/es/AreaChart/index.mjs.map +1 -1
- package/es/AreaChart/styles.d.mts +1 -4
- package/es/AreaChart/styles.mjs +1 -2
- package/es/BarChart/A2uiBarChart.d.mts +56 -0
- package/es/BarChart/A2uiBarChart.mjs +368 -0
- package/es/BarChart/A2uiBarChart.mjs.map +1 -0
- package/es/BarChart/index.d.mts +10 -6
- package/es/BarChart/index.mjs +70 -33
- package/es/BarChart/index.mjs.map +1 -1
- package/es/BarChart/renderShape.d.mts +1 -4
- package/es/BarChart/renderShape.mjs +5 -6
- package/es/BarChart/renderShape.mjs.map +1 -1
- package/es/BarChart/styles.d.mts +1 -4
- package/es/BarChart/styles.mjs +1 -2
- package/es/BarList/index.d.mts +8 -6
- package/es/BarList/index.mjs +13 -14
- package/es/BarList/index.mjs.map +1 -1
- package/es/BarList/styles.d.mts +1 -4
- package/es/BarList/styles.mjs +3 -5
- package/es/BarList/styles.mjs.map +1 -1
- package/es/BenchmarkColumnChart/index.d.mts +8 -6
- package/es/BenchmarkColumnChart/index.mjs +38 -39
- package/es/BenchmarkColumnChart/index.mjs.map +1 -1
- package/es/BenchmarkColumnChart/styles.d.mts +1 -4
- package/es/BenchmarkColumnChart/styles.mjs +6 -8
- package/es/BenchmarkColumnChart/styles.mjs.map +1 -1
- package/es/BenchmarkRankingChart/index.d.mts +8 -6
- package/es/BenchmarkRankingChart/index.mjs +16 -17
- package/es/BenchmarkRankingChart/index.mjs.map +1 -1
- package/es/BenchmarkRankingChart/styles.d.mts +1 -4
- package/es/BenchmarkRankingChart/styles.mjs +25 -27
- package/es/BenchmarkRankingChart/styles.mjs.map +1 -1
- package/es/BenchmarkScatterChart/index.d.mts +7 -5
- package/es/BenchmarkScatterChart/index.mjs +22 -23
- package/es/BenchmarkScatterChart/index.mjs.map +1 -1
- package/es/BenchmarkScatterChart/styles.d.mts +1 -4
- package/es/BenchmarkScatterChart/styles.mjs +3 -2
- package/es/BenchmarkScatterChart/styles.mjs.map +1 -1
- package/es/BenchmarkScatterChart/utils.d.mts +1 -4
- package/es/BenchmarkScatterChart/utils.mjs +4 -5
- package/es/BenchmarkScatterChart/utils.mjs.map +1 -1
- package/es/CandlestickChart/index.d.mts +52 -0
- package/es/CandlestickChart/index.mjs +205 -0
- package/es/CandlestickChart/index.mjs.map +1 -0
- package/es/CandlestickChart/styles.d.mts +5 -0
- package/es/CandlestickChart/styles.mjs +24 -0
- package/es/CandlestickChart/styles.mjs.map +1 -0
- package/es/CandlestickChart/utils.d.mts +17 -0
- package/es/CandlestickChart/utils.mjs +56 -0
- package/es/CandlestickChart/utils.mjs.map +1 -0
- package/es/ChartFrame/index.d.mts +40 -0
- package/es/ChartFrame/index.mjs +18 -0
- package/es/ChartFrame/index.mjs.map +1 -0
- package/es/ComposedChart/index.d.mts +7 -5
- package/es/ComposedChart/index.mjs +37 -38
- package/es/ComposedChart/index.mjs.map +1 -1
- package/es/ComposedChart/styles.d.mts +1 -4
- package/es/ComposedChart/styles.mjs +1 -2
- package/es/ComposedChart/types.d.mts +3 -6
- package/es/DataBars/CategoryBar.d.mts +8 -6
- package/es/DataBars/CategoryBar.mjs +13 -14
- package/es/DataBars/CategoryBar.mjs.map +1 -1
- package/es/DataBars/DeltaBar.d.mts +7 -5
- package/es/DataBars/DeltaBar.mjs +10 -12
- package/es/DataBars/DeltaBar.mjs.map +1 -1
- package/es/DataBars/MarkerBar.d.mts +7 -5
- package/es/DataBars/MarkerBar.mjs +10 -12
- package/es/DataBars/MarkerBar.mjs.map +1 -1
- package/es/DataBars/ProgressBar.d.mts +7 -5
- package/es/DataBars/ProgressBar.mjs +10 -12
- package/es/DataBars/ProgressBar.mjs.map +1 -1
- package/es/DataBars/styles.d.mts +1 -4
- package/es/DataBars/styles.mjs +2 -3
- package/es/DataBars/styles.mjs.map +1 -1
- package/es/DonutChart/A2uiDonutChart.d.mts +35 -0
- package/es/DonutChart/A2uiDonutChart.mjs +189 -0
- package/es/DonutChart/A2uiDonutChart.mjs.map +1 -0
- package/es/DonutChart/DonutChartTooltip.d.mts +2 -5
- package/es/DonutChart/DonutChartTooltip.mjs +3 -4
- package/es/DonutChart/DonutChartTooltip.mjs.map +1 -1
- package/es/DonutChart/a2uiStyles.d.mts +13 -0
- package/es/DonutChart/a2uiStyles.mjs +90 -0
- package/es/DonutChart/a2uiStyles.mjs.map +1 -0
- package/es/DonutChart/index.d.mts +42 -9
- package/es/DonutChart/index.mjs +48 -16
- package/es/DonutChart/index.mjs.map +1 -1
- package/es/DonutChart/inputParser.d.mts +1 -4
- package/es/DonutChart/inputParser.mjs +1 -2
- package/es/DonutChart/inputParser.mjs.map +1 -1
- package/es/DonutChart/renderInactiveShape.d.mts +1 -4
- package/es/DonutChart/renderInactiveShape.mjs +1 -2
- package/es/DonutChart/styles.d.mts +1 -4
- package/es/DonutChart/styles.mjs +1 -2
- package/es/FunnelChart/ArrowRightIcon.d.mts +1 -4
- package/es/FunnelChart/ArrowRightIcon.mjs +1 -2
- package/es/FunnelChart/index.d.mts +7 -5
- package/es/FunnelChart/index.mjs +43 -44
- package/es/FunnelChart/index.mjs.map +1 -1
- package/es/FunnelChart/styles.d.mts +1 -4
- package/es/FunnelChart/styles.mjs +1 -2
- package/es/FunnelChart/types.d.mts +1 -4
- package/es/GaugeChart/index.d.mts +49 -0
- package/es/GaugeChart/index.mjs +77 -0
- package/es/GaugeChart/index.mjs.map +1 -0
- package/es/GaugeChart/styles.d.mts +9 -0
- package/es/GaugeChart/styles.mjs +73 -0
- package/es/GaugeChart/styles.mjs.map +1 -0
- package/es/GaugeChart/utils.d.mts +5 -0
- package/es/GaugeChart/utils.mjs +32 -0
- package/es/GaugeChart/utils.mjs.map +1 -0
- package/es/HeatmapChart/index.d.mts +68 -0
- package/es/HeatmapChart/index.mjs +162 -0
- package/es/HeatmapChart/index.mjs.map +1 -0
- package/es/HeatmapChart/styles.d.mts +4 -0
- package/es/HeatmapChart/styles.mjs +24 -0
- package/es/HeatmapChart/styles.mjs.map +1 -0
- package/es/HeatmapChart/utils.d.mts +18 -0
- package/es/HeatmapChart/utils.mjs +40 -0
- package/es/HeatmapChart/utils.mjs.map +1 -0
- package/es/Heatmaps/Calendar.d.mts +2 -5
- package/es/Heatmaps/Calendar.mjs +8 -9
- package/es/Heatmaps/Calendar.mjs.map +1 -1
- package/es/Heatmaps/ChartLabels.d.mts +2 -5
- package/es/Heatmaps/ChartLabels.mjs +3 -4
- package/es/Heatmaps/ChartLabels.mjs.map +1 -1
- package/es/Heatmaps/Footer.d.mts +1 -4
- package/es/Heatmaps/Footer.mjs +3 -4
- package/es/Heatmaps/index.d.mts +10 -8
- package/es/Heatmaps/index.mjs +10 -11
- package/es/Heatmaps/index.mjs.map +1 -1
- package/es/Heatmaps/styles.d.mts +1 -4
- package/es/Heatmaps/styles.mjs +2 -3
- package/es/Heatmaps/styles.mjs.map +1 -1
- package/es/Legend/LegendItem.d.mts +1 -4
- package/es/Legend/LegendItem.mjs +7 -8
- package/es/Legend/LegendItem.mjs.map +1 -1
- package/es/Legend/ScrollButton.d.mts +1 -4
- package/es/Legend/ScrollButton.mjs +6 -7
- package/es/Legend/ScrollButton.mjs.map +1 -1
- package/es/Legend/index.d.mts +7 -5
- package/es/Legend/index.mjs +13 -18
- package/es/Legend/index.mjs.map +1 -1
- package/es/LineChart/index.d.mts +11 -7
- package/es/LineChart/index.mjs +91 -53
- package/es/LineChart/index.mjs.map +1 -1
- package/es/LineChart/styles.d.mts +1 -4
- package/es/LineChart/styles.mjs +1 -2
- package/es/RadarChart/A2uiRadarChart.d.mts +37 -0
- package/es/RadarChart/A2uiRadarChart.mjs +203 -0
- package/es/RadarChart/A2uiRadarChart.mjs.map +1 -0
- package/es/RadarChart/index.d.mts +11 -7
- package/es/RadarChart/index.mjs +51 -18
- package/es/RadarChart/index.mjs.map +1 -1
- package/es/RadarChart/styles.d.mts +1 -4
- package/es/RadarChart/styles.mjs +1 -2
- package/es/SankeyChart/index.d.mts +73 -0
- package/es/SankeyChart/index.mjs +156 -0
- package/es/SankeyChart/index.mjs.map +1 -0
- package/es/SankeyChart/utils.d.mts +54 -0
- package/es/SankeyChart/utils.mjs +270 -0
- package/es/SankeyChart/utils.mjs.map +1 -0
- package/es/ScatterChart/A2uiScatterChart.d.mts +41 -0
- package/es/ScatterChart/A2uiScatterChart.mjs +259 -0
- package/es/ScatterChart/A2uiScatterChart.mjs.map +1 -0
- package/es/ScatterChart/ScatterChartTooltip.d.mts +2 -5
- package/es/ScatterChart/ScatterChartTooltip.mjs +7 -8
- package/es/ScatterChart/ScatterChartTooltip.mjs.map +1 -1
- package/es/ScatterChart/ScatterChartTooltipRow.d.mts +1 -4
- package/es/ScatterChart/ScatterChartTooltipRow.mjs +3 -4
- package/es/ScatterChart/ScatterChartTooltipRow.mjs.map +1 -1
- package/es/ScatterChart/index.d.mts +15 -9
- package/es/ScatterChart/index.mjs +70 -33
- package/es/ScatterChart/index.mjs.map +1 -1
- package/es/ScatterChart/renderShape.d.mts +1 -4
- package/es/ScatterChart/renderShape.mjs +4 -5
- package/es/ScatterChart/renderShape.mjs.map +1 -1
- package/es/ScatterChart/styles.d.mts +1 -4
- package/es/ScatterChart/styles.mjs +1 -2
- package/es/SparkChart/A2uiSparkChart.d.mts +41 -0
- package/es/SparkChart/A2uiSparkChart.mjs +179 -0
- package/es/SparkChart/A2uiSparkChart.mjs.map +1 -0
- package/es/SparkChart/SparkAreaChart.d.mts +12 -7
- package/es/SparkChart/SparkAreaChart.mjs +47 -22
- package/es/SparkChart/SparkAreaChart.mjs.map +1 -1
- package/es/SparkChart/SparkBarChart.d.mts +11 -6
- package/es/SparkChart/SparkBarChart.mjs +39 -16
- package/es/SparkChart/SparkBarChart.mjs.map +1 -1
- package/es/SparkChart/SparkLineChart.d.mts +12 -7
- package/es/SparkChart/SparkLineChart.mjs +41 -16
- package/es/SparkChart/SparkLineChart.mjs.map +1 -1
- package/es/SparkChart/index.d.mts +2 -1
- package/es/Tracker/TrackerBlock.d.mts +5 -5
- package/es/Tracker/TrackerBlock.mjs +8 -9
- package/es/Tracker/TrackerBlock.mjs.map +1 -1
- package/es/Tracker/index.d.mts +8 -6
- package/es/Tracker/index.mjs +11 -13
- package/es/Tracker/index.mjs.map +1 -1
- package/es/Tracker/styles.d.mts +1 -4
- package/es/Tracker/styles.mjs +1 -2
- package/es/TreemapChart/index.d.mts +64 -0
- package/es/TreemapChart/index.mjs +140 -0
- package/es/TreemapChart/index.mjs.map +1 -0
- package/es/TreemapChart/utils.d.mts +23 -0
- package/es/TreemapChart/utils.mjs +175 -0
- package/es/TreemapChart/utils.mjs.map +1 -0
- package/es/common/BaseAnimationTimingProps.d.mts +1 -4
- package/es/common/BaseChartProps.d.mts +3 -6
- package/es/common/BaseSparkChartProps.d.mts +2 -5
- package/es/common/ChartLegend.d.mts +1 -4
- package/es/common/ChartLegend.mjs +4 -5
- package/es/common/ChartLegend.mjs.map +1 -1
- package/es/common/ChartTooltip/ChartTooltipFrame.d.mts +1 -4
- package/es/common/ChartTooltip/ChartTooltipFrame.mjs +1 -2
- package/es/common/ChartTooltip/ChartTooltipFrame.mjs.map +1 -1
- package/es/common/ChartTooltip/ChartTooltipRow.d.mts +1 -4
- package/es/common/ChartTooltip/ChartTooltipRow.mjs +5 -6
- package/es/common/ChartTooltip/ChartTooltipRow.mjs.map +1 -1
- package/es/common/ChartTooltip/index.d.mts +2 -5
- package/es/common/ChartTooltip/index.mjs +3 -4
- package/es/common/ChartTooltip/index.mjs.map +1 -1
- package/es/common/CustomTooltipProps.d.mts +3 -6
- package/es/common/CustomYAxisTick.d.mts +2 -5
- package/es/common/CustomYAxisTick.mjs +1 -2
- package/es/common/CustomYAxisTick.mjs.map +1 -1
- package/es/common/NoData.d.mts +1 -4
- package/es/common/NoData.mjs +2 -3
- package/es/common/NoData.mjs.map +1 -1
- package/es/common/a2ui/A2uiTrendChart.d.mts +43 -0
- package/es/common/a2ui/A2uiTrendChart.mjs +353 -0
- package/es/common/a2ui/A2uiTrendChart.mjs.map +1 -0
- package/es/common/a2ui/Frame.d.mts +20 -0
- package/es/common/a2ui/Frame.mjs +43 -0
- package/es/common/a2ui/Frame.mjs.map +1 -0
- package/es/common/a2ui/HoverTooltip.d.mts +28 -0
- package/es/common/a2ui/HoverTooltip.mjs +36 -0
- package/es/common/a2ui/HoverTooltip.mjs.map +1 -0
- package/es/common/a2ui/Legend.d.mts +11 -0
- package/es/common/a2ui/Legend.mjs +33 -0
- package/es/common/a2ui/Legend.mjs.map +1 -0
- package/es/common/a2ui/PlacedTooltip.d.mts +22 -0
- package/es/common/a2ui/PlacedTooltip.mjs +58 -0
- package/es/common/a2ui/PlacedTooltip.mjs.map +1 -0
- package/es/common/a2ui/Tooltip.d.mts +13 -0
- package/es/common/a2ui/Tooltip.mjs +33 -0
- package/es/common/a2ui/Tooltip.mjs.map +1 -0
- package/es/common/a2ui/axis.d.mts +38 -0
- package/es/common/a2ui/axis.mjs +48 -0
- package/es/common/a2ui/axis.mjs.map +1 -0
- package/es/common/a2ui/curve.d.mts +20 -0
- package/es/common/a2ui/curve.mjs +58 -0
- package/es/common/a2ui/curve.mjs.map +1 -0
- package/es/common/a2ui/format.d.mts +17 -0
- package/es/common/a2ui/format.mjs +56 -0
- package/es/common/a2ui/format.mjs.map +1 -0
- package/es/common/a2ui/index.d.mts +16 -0
- package/es/common/a2ui/index.mjs +15 -0
- package/es/common/a2ui/scale.d.mts +20 -0
- package/es/common/a2ui/scale.mjs +112 -0
- package/es/common/a2ui/scale.mjs.map +1 -0
- package/es/common/a2ui/styles.d.mts +35 -0
- package/es/common/a2ui/styles.mjs +254 -0
- package/es/common/a2ui/styles.mjs.map +1 -0
- package/es/common/a2ui/tokens.d.mts +20 -0
- package/es/common/a2ui/tokens.mjs +103 -0
- package/es/common/a2ui/tokens.mjs.map +1 -0
- package/es/common/a2ui/types.d.mts +25 -0
- package/es/common/a2ui/types.mjs +0 -0
- package/es/common/a2ui/useA2uiTheme.d.mts +2 -0
- package/es/common/a2ui/useA2uiTheme.mjs +11 -0
- package/es/common/a2ui/useA2uiTheme.mjs.map +1 -0
- package/es/common/a2ui/useElementWidth.d.mts +2 -0
- package/es/common/a2ui/useElementWidth.mjs +31 -0
- package/es/common/a2ui/useElementWidth.mjs.map +1 -0
- package/es/common/benchmark/types.d.mts +2 -5
- package/es/common/benchmark/utils.d.mts +1 -4
- package/es/common/benchmark/utils.mjs +1 -2
- package/es/common/utils.d.mts +1 -4
- package/es/common/utils.mjs +1 -2
- package/es/hooks/useOnWindowResize.d.mts +1 -4
- package/es/hooks/useOnWindowResize.mjs +2 -3
- package/es/hooks/usePrefersReducedMotion.d.mts +1 -4
- package/es/hooks/usePrefersReducedMotion.mjs +2 -3
- package/es/hooks/useThemeColorRange.d.mts +1 -4
- package/es/hooks/useThemeColorRange.mjs +1 -2
- package/es/index.d.mts +20 -9
- package/es/index.mjs +15 -9
- package/es/types/charts.d.mts +1 -4
- package/es/types/charts.mjs +1 -2
- package/es/utils/calendar.d.mts +2 -5
- package/es/utils/calendar.mjs +2 -3
- package/es/utils/getMaxLabelLength.d.mts +1 -4
- package/es/utils/getMaxLabelLength.mjs +1 -2
- package/es/utils/getMaxLabelLength.mjs.map +1 -1
- package/es/utils/index.d.mts +1 -4
- package/es/utils/index.mjs +1 -2
- package/es/utils/index.mjs.map +1 -1
- package/es/utils/theme.d.mts +1 -4
- package/es/utils/theme.mjs +1 -2
- package/package.json +33 -9
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
// src/common/a2ui/PlacedTooltip.tsx
|
|
4
|
+
const GAP = 10;
|
|
5
|
+
/** Room kept between the tooltip and the plot's top and bottom edges. */
|
|
6
|
+
const EDGE = 8;
|
|
7
|
+
/**
|
|
8
|
+
* Places a tooltip like the official charts: centred above the focused point when it fits, else below
|
|
9
|
+
* it, else to its right (or left); always clamped inside the plot.
|
|
10
|
+
*/
|
|
11
|
+
const A2uiPlacedTooltip = ({ x, y, width, height, prefer = "top", children }) => {
|
|
12
|
+
const ref = useRef(null);
|
|
13
|
+
const [size, setSize] = useState();
|
|
14
|
+
useLayoutEffect(() => {
|
|
15
|
+
const el = ref.current;
|
|
16
|
+
if (!el) return;
|
|
17
|
+
const { width: w, height: h } = el.getBoundingClientRect();
|
|
18
|
+
if (!size || Math.abs(size.w - w) > .5 || Math.abs(size.h - h) > .5) setSize({
|
|
19
|
+
h,
|
|
20
|
+
w
|
|
21
|
+
});
|
|
22
|
+
}, [children, size]);
|
|
23
|
+
let left = x;
|
|
24
|
+
let top = y;
|
|
25
|
+
if (size) {
|
|
26
|
+
const clamp = (v, min, max) => Math.max(min, Math.min(max, v));
|
|
27
|
+
const centred = clamp(x - size.w / 2, 0, Math.max(0, width - size.w));
|
|
28
|
+
const side = () => {
|
|
29
|
+
left = x + GAP + size.w <= width ? x + GAP : Math.max(0, x - GAP - size.w);
|
|
30
|
+
top = clamp(y - size.h / 2, 0, Math.max(0, height - size.h));
|
|
31
|
+
};
|
|
32
|
+
if (prefer === "side") side();
|
|
33
|
+
else if (y - GAP - size.h >= EDGE) {
|
|
34
|
+
left = centred;
|
|
35
|
+
top = y - GAP - size.h;
|
|
36
|
+
} else if (y + GAP + size.h <= height - EDGE) {
|
|
37
|
+
left = centred;
|
|
38
|
+
top = y + GAP;
|
|
39
|
+
} else side();
|
|
40
|
+
}
|
|
41
|
+
return /* @__PURE__ */ jsx("div", {
|
|
42
|
+
ref,
|
|
43
|
+
style: {
|
|
44
|
+
left,
|
|
45
|
+
pointerEvents: "none",
|
|
46
|
+
position: "absolute",
|
|
47
|
+
top,
|
|
48
|
+
visibility: size ? "visible" : "hidden",
|
|
49
|
+
whiteSpace: "nowrap",
|
|
50
|
+
width: "max-content",
|
|
51
|
+
zIndex: 1
|
|
52
|
+
},
|
|
53
|
+
children
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
export { A2uiPlacedTooltip as default };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=PlacedTooltip.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlacedTooltip.mjs","names":[],"sources":["../../../src/common/a2ui/PlacedTooltip.tsx"],"sourcesContent":["import type { ReactNode } from 'react';\nimport { useLayoutEffect, useRef, useState } from 'react';\n\nexport interface A2uiPlacedTooltipProps {\n children: ReactNode;\n /** Plot height, to keep the tooltip inside. */\n height: number;\n /**\n * Preferred side: `top` (then bottom, then the sides), or `side` (right, then left) as for points.\n * @default 'top'\n */\n prefer?: 'top' | 'side';\n /** Plot width, to keep the tooltip inside. */\n width: number;\n /** Focused point, in plot coordinates. */\n x: number;\n y: number;\n}\n\nconst GAP = 10;\n/** Room kept between the tooltip and the plot's top and bottom edges. */\nconst EDGE = 8;\n\n/**\n * Places a tooltip like the official charts: centred above the focused point when it fits, else below\n * it, else to its right (or left); always clamped inside the plot.\n */\nconst A2uiPlacedTooltip = ({\n x,\n y,\n width,\n height,\n prefer = 'top',\n children,\n}: A2uiPlacedTooltipProps) => {\n const ref = useRef<HTMLDivElement>(null);\n const [size, setSize] = useState<{ h: number; w: number } | undefined>();\n\n // Re-measure when the content (rows, values) changes with the focused point.\n useLayoutEffect(() => {\n const el = ref.current;\n if (!el) return;\n const { width: w, height: h } = el.getBoundingClientRect();\n if (!size || Math.abs(size.w - w) > 0.5 || Math.abs(size.h - h) > 0.5) setSize({ h, w });\n }, [children, size]);\n\n let left = x;\n let top = y;\n if (size) {\n const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v));\n const centred = clamp(x - size.w / 2, 0, Math.max(0, width - size.w));\n const side = () => {\n left = x + GAP + size.w <= width ? x + GAP : Math.max(0, x - GAP - size.w);\n top = clamp(y - size.h / 2, 0, Math.max(0, height - size.h));\n };\n if (prefer === 'side') side();\n else if (y - GAP - size.h >= EDGE) {\n left = centred;\n top = y - GAP - size.h;\n } else if (y + GAP + size.h <= height - EDGE) {\n left = centred;\n top = y + GAP;\n } else side();\n }\n\n return (\n <div\n ref={ref}\n style={{\n left,\n pointerEvents: 'none',\n position: 'absolute',\n top,\n visibility: size ? 'visible' : 'hidden',\n whiteSpace: 'nowrap',\n width: 'max-content',\n zIndex: 1,\n }}\n >\n {children}\n </div>\n );\n};\n\nexport default A2uiPlacedTooltip;\n"],"mappings":";;;AAmBA,MAAM,MAAM;;AAEZ,MAAM,OAAO;;;;;AAMb,MAAM,qBAAqB,EACzB,GACA,GACA,OACA,QACA,SAAS,OACT,eAC4B;CAC5B,MAAM,MAAM,OAAuB,IAAI;CACvC,MAAM,CAAC,MAAM,WAAW,SAA+C;CAGvE,sBAAsB;EACpB,MAAM,KAAK,IAAI;EACf,IAAI,CAAC,IAAI;EACT,MAAM,EAAE,OAAO,GAAG,QAAQ,MAAM,GAAG,sBAAsB;EACzD,IAAI,CAAC,QAAQ,KAAK,IAAI,KAAK,IAAI,CAAC,IAAI,MAAO,KAAK,IAAI,KAAK,IAAI,CAAC,IAAI,IAAK,QAAQ;GAAE;GAAG;EAAE,CAAC;CACzF,GAAG,CAAC,UAAU,IAAI,CAAC;CAEnB,IAAI,OAAO;CACX,IAAI,MAAM;CACV,IAAI,MAAM;EACR,MAAM,SAAS,GAAW,KAAa,QAAgB,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,CAAC,CAAC;EACrF,MAAM,UAAU,MAAM,IAAI,KAAK,IAAI,GAAG,GAAG,KAAK,IAAI,GAAG,QAAQ,KAAK,CAAC,CAAC;EACpE,MAAM,aAAa;GACjB,OAAO,IAAI,MAAM,KAAK,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,GAAG,IAAI,MAAM,KAAK,CAAC;GACzE,MAAM,MAAM,IAAI,KAAK,IAAI,GAAG,GAAG,KAAK,IAAI,GAAG,SAAS,KAAK,CAAC,CAAC;EAC7D;EACA,IAAI,WAAW,QAAQ,KAAK;OACvB,IAAI,IAAI,MAAM,KAAK,KAAK,MAAM;GACjC,OAAO;GACP,MAAM,IAAI,MAAM,KAAK;EACvB,OAAO,IAAI,IAAI,MAAM,KAAK,KAAK,SAAS,MAAM;GAC5C,OAAO;GACP,MAAM,IAAI;EACZ,OAAO,KAAK;CACd;CAEA,OACE,oBAAC,OAAD;EACO;EACL,OAAO;GACL;GACA,eAAe;GACf,UAAU;GACV;GACA,YAAY,OAAO,YAAY;GAC/B,YAAY;GACZ,OAAO;GACP,QAAQ;EACV;EAEC;CACE,CAAA;AAET"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
export interface A2uiTooltipRow {
|
|
3
|
+
active?: boolean;
|
|
4
|
+
color?: string;
|
|
5
|
+
label: ReactNode;
|
|
6
|
+
value: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
export interface A2uiTooltipProps {
|
|
9
|
+
rows: A2uiTooltipRow[];
|
|
10
|
+
title?: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
declare const A2uiTooltip: import("react").NamedExoticComponent<A2uiTooltipProps>;
|
|
13
|
+
export { A2uiTooltip as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { cx } from "@lobehub/ui";
|
|
2
|
+
import { memo } from "react";
|
|
3
|
+
import { styles } from "./styles.mjs";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
// src/common/a2ui/Tooltip.tsx
|
|
6
|
+
const A2uiTooltip = memo(({ title, rows }) => {
|
|
7
|
+
if (!title && rows.length === 0) return null;
|
|
8
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
9
|
+
className: styles.tooltip,
|
|
10
|
+
role: "status",
|
|
11
|
+
children: [title !== void 0 && title !== null && title !== "" ? /* @__PURE__ */ jsx("div", {
|
|
12
|
+
className: styles.tooltipTitle,
|
|
13
|
+
children: title
|
|
14
|
+
}) : null, /* @__PURE__ */ jsx("div", { children: rows.map((row, i) => /* @__PURE__ */ jsxs("div", {
|
|
15
|
+
className: cx(styles.tooltipRow, row.active && styles.tooltipRowActive),
|
|
16
|
+
children: [
|
|
17
|
+
row.color ? /* @__PURE__ */ jsx("span", {
|
|
18
|
+
className: styles.tooltipSwatch,
|
|
19
|
+
style: { background: row.color }
|
|
20
|
+
}) : /* @__PURE__ */ jsx("span", {}),
|
|
21
|
+
/* @__PURE__ */ jsx("span", { children: row.label }),
|
|
22
|
+
/* @__PURE__ */ jsx("span", {
|
|
23
|
+
className: styles.tooltipValue,
|
|
24
|
+
children: row.value
|
|
25
|
+
})
|
|
26
|
+
]
|
|
27
|
+
}, i)) })]
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
A2uiTooltip.displayName = "A2uiTooltip";
|
|
31
|
+
export { A2uiTooltip as default };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=Tooltip.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Tooltip.mjs","names":[],"sources":["../../../src/common/a2ui/Tooltip.tsx"],"sourcesContent":["import { cx } from '@lobehub/ui';\nimport type { ReactNode } from 'react';\nimport { memo } from 'react';\n\nimport { styles } from './styles';\n\nexport interface A2uiTooltipRow {\n active?: boolean;\n color?: string;\n label: ReactNode;\n value: ReactNode;\n}\n\nexport interface A2uiTooltipProps {\n rows: A2uiTooltipRow[];\n title?: ReactNode;\n}\n\n// Tooltip chrome of the official charts (TanStack tooltip with the a2ui variables).\nconst A2uiTooltip = memo<A2uiTooltipProps>(({ title, rows }) => {\n if (!title && rows.length === 0) return null;\n return (\n <div className={styles.tooltip} role=\"status\">\n {title !== undefined && title !== null && title !== '' ? (\n <div className={styles.tooltipTitle}>{title}</div>\n ) : null}\n <div>\n {rows.map((row, i) => (\n <div className={cx(styles.tooltipRow, row.active && styles.tooltipRowActive)} key={i}>\n {row.color ? (\n <span className={styles.tooltipSwatch} style={{ background: row.color }} />\n ) : (\n <span />\n )}\n <span>{row.label}</span>\n <span className={styles.tooltipValue}>{row.value}</span>\n </div>\n ))}\n </div>\n </div>\n );\n});\n\nA2uiTooltip.displayName = 'A2uiTooltip';\n\nexport default A2uiTooltip;\n"],"mappings":";;;;;AAmBA,MAAM,cAAc,MAAwB,EAAE,OAAO,WAAW;CAC9D,IAAI,CAAC,SAAS,KAAK,WAAW,GAAG,OAAO;CACxC,OACE,qBAAC,OAAD;EAAK,WAAW,OAAO;EAAS,MAAK;EAArC,UAAA,CACG,UAAU,KAAA,KAAa,UAAU,QAAQ,UAAU,KAClD,oBAAC,OAAD;GAAK,WAAW,OAAO;GAAe,UAAA;EAAW,CAAA,IAC/C,MACJ,oBAAC,OAAD,EAAA,UACG,KAAK,KAAK,KAAK,MACd,qBAAC,OAAD;GAAK,WAAW,GAAG,OAAO,YAAY,IAAI,UAAU,OAAO,gBAAgB;GAA3E,UAAA;IACG,IAAI,QACH,oBAAC,QAAD;KAAM,WAAW,OAAO;KAAe,OAAO,EAAE,YAAY,IAAI,MAAM;IAAI,CAAA,IAE1E,oBAAC,QAAD,CAAO,CAAA;IAET,oBAAC,QAAD,EAAA,UAAO,IAAI,MAAY,CAAA;IACvB,oBAAC,QAAD;KAAM,WAAW,OAAO;KAAe,UAAA,IAAI;IAAY,CAAA;GACpD;EAR8E,GAAA,CAQ9E,CACN,EACE,CAAA,CACF;;AAET,CAAC;AAED,YAAY,cAAc"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ValueFormatter } from "../../types/charts.mjs";
|
|
2
|
+
/** Distance from the plot edge to a y tick label, and from the plot bottom to an x tick baseline. */
|
|
3
|
+
export declare const Y_TICK_PADDING = 8;
|
|
4
|
+
export declare const X_TICK_BASELINE = 16.8;
|
|
5
|
+
/** Width of an 11px tick label, measured in `fontFamily` when known, otherwise estimated. */
|
|
6
|
+
export declare const tickLabelWidth: (label: string, fontFamily?: string) => number;
|
|
7
|
+
/**
|
|
8
|
+
* Right edge of the y tick labels (the plot starts `Y_TICK_PADDING` further right): the widest label plus
|
|
9
|
+
* a 4px gutter, plus `extra` for an axis title.
|
|
10
|
+
*/
|
|
11
|
+
export declare const yAxisWidthFor: (labels: string[], extra?: number, fontFamily?: string) => number;
|
|
12
|
+
/**
|
|
13
|
+
* Right margin that keeps the last x label inside the chart: half the label past the point it is centred
|
|
14
|
+
* on (`inset` from the plot's right edge), plus a 4px gutter; never less than 4px.
|
|
15
|
+
*/
|
|
16
|
+
export declare const rightMarginFor: (label: string, inset: number, fontFamily?: string) => number;
|
|
17
|
+
interface TickProps {
|
|
18
|
+
formatter?: ValueFormatter | ((v: any) => string);
|
|
19
|
+
payload?: {
|
|
20
|
+
value: any;
|
|
21
|
+
};
|
|
22
|
+
x?: number | string;
|
|
23
|
+
y?: number | string;
|
|
24
|
+
}
|
|
25
|
+
/** X tick: 11px muted label, baseline 16.8px under the plot. Render with `tickSize={0}`, `tickMargin={0}`. */
|
|
26
|
+
export declare const A2uiXTick: ({ x, y, payload, formatter }: TickProps) => import("react").JSX.Element;
|
|
27
|
+
/** Y tick: right-aligned, vertically centred. Render with `tickSize={0}`, `tickMargin={8}`. */
|
|
28
|
+
export declare const A2uiYTick: ({ x, y, payload, formatter }: TickProps) => import("react").JSX.Element;
|
|
29
|
+
/** Y tick for a category axis (horizontal bars). */
|
|
30
|
+
export declare const A2uiCategoryYTick: ({ x, y, payload, formatter }: TickProps) => import("react").JSX.Element;
|
|
31
|
+
/** Axis title, as official: 11px / 500 in the chart foreground at 76%. */
|
|
32
|
+
export declare const AXIS_TITLE: {
|
|
33
|
+
readonly fill: "var(--a2-chart-fg)";
|
|
34
|
+
readonly fillOpacity: 0.76;
|
|
35
|
+
readonly fontSize: 11;
|
|
36
|
+
readonly fontWeight: 500;
|
|
37
|
+
};
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { estimateLabelWidth, measureTextWidth } from "./scale.mjs";
|
|
2
|
+
import { A2UI_TICK } from "./tokens.mjs";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/common/a2ui/axis.tsx
|
|
5
|
+
/** Distance from the plot edge to a y tick label, and from the plot bottom to an x tick baseline. */
|
|
6
|
+
const Y_TICK_PADDING = 8;
|
|
7
|
+
const X_TICK_BASELINE = 16.8;
|
|
8
|
+
/** Width of an 11px tick label, measured in `fontFamily` when known, otherwise estimated. */
|
|
9
|
+
const tickLabelWidth = (label, fontFamily) => fontFamily ? measureTextWidth(label, `11px ${fontFamily}`, 11) : estimateLabelWidth(label) - 2;
|
|
10
|
+
/**
|
|
11
|
+
* Right edge of the y tick labels (the plot starts `Y_TICK_PADDING` further right): the widest label plus
|
|
12
|
+
* a 4px gutter, plus `extra` for an axis title.
|
|
13
|
+
*/
|
|
14
|
+
const yAxisWidthFor = (labels, extra = 0, fontFamily) => 4 + extra + Math.max(0, ...labels.map((l) => Math.ceil(tickLabelWidth(l, fontFamily))));
|
|
15
|
+
/**
|
|
16
|
+
* Right margin that keeps the last x label inside the chart: half the label past the point it is centred
|
|
17
|
+
* on (`inset` from the plot's right edge), plus a 4px gutter; never less than 4px.
|
|
18
|
+
*/
|
|
19
|
+
const rightMarginFor = (label, inset, fontFamily) => Math.max(4, tickLabelWidth(label, fontFamily) / 2 - inset + 4);
|
|
20
|
+
/** X tick: 11px muted label, baseline 16.8px under the plot. Render with `tickSize={0}`, `tickMargin={0}`. */
|
|
21
|
+
const A2uiXTick = ({ x, y, payload, formatter }) => /* @__PURE__ */ jsx("text", {
|
|
22
|
+
...A2UI_TICK,
|
|
23
|
+
textAnchor: "middle",
|
|
24
|
+
x: Number(x),
|
|
25
|
+
y: Number(y) + X_TICK_BASELINE,
|
|
26
|
+
children: formatter ? formatter(payload?.value) : String(payload?.value ?? "")
|
|
27
|
+
});
|
|
28
|
+
/** Y tick: right-aligned, vertically centred. Render with `tickSize={0}`, `tickMargin={8}`. */
|
|
29
|
+
const A2uiYTick = ({ x, y, payload, formatter }) => /* @__PURE__ */ jsx("text", {
|
|
30
|
+
...A2UI_TICK,
|
|
31
|
+
dominantBaseline: "middle",
|
|
32
|
+
textAnchor: "end",
|
|
33
|
+
x: Number(x),
|
|
34
|
+
y: Number(y),
|
|
35
|
+
children: formatter ? formatter(payload?.value) : String(payload?.value ?? "")
|
|
36
|
+
});
|
|
37
|
+
/** Y tick for a category axis (horizontal bars). */
|
|
38
|
+
const A2uiCategoryYTick = A2uiYTick;
|
|
39
|
+
/** Axis title, as official: 11px / 500 in the chart foreground at 76%. */
|
|
40
|
+
const AXIS_TITLE = {
|
|
41
|
+
fill: "var(--a2-chart-fg)",
|
|
42
|
+
fillOpacity: .76,
|
|
43
|
+
fontSize: 11,
|
|
44
|
+
fontWeight: 500
|
|
45
|
+
};
|
|
46
|
+
export { A2uiCategoryYTick, A2uiXTick, A2uiYTick, AXIS_TITLE, X_TICK_BASELINE, Y_TICK_PADDING, rightMarginFor, tickLabelWidth, yAxisWidthFor };
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=axis.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"axis.mjs","names":[],"sources":["../../../src/common/a2ui/axis.tsx"],"sourcesContent":["import type { ValueFormatter } from '@/types/charts';\n\nimport { estimateLabelWidth, measureTextWidth } from './scale';\nimport { A2UI_TICK } from './tokens';\n\n/** Distance from the plot edge to a y tick label, and from the plot bottom to an x tick baseline. */\nexport const Y_TICK_PADDING = 8;\nexport const X_TICK_BASELINE = 16.8;\n\n/** Width of an 11px tick label, measured in `fontFamily` when known, otherwise estimated. */\nexport const tickLabelWidth = (label: string, fontFamily?: string) =>\n fontFamily ? measureTextWidth(label, `11px ${fontFamily}`, 11) : estimateLabelWidth(label) - 2;\n\n/**\n * Right edge of the y tick labels (the plot starts `Y_TICK_PADDING` further right): the widest label plus\n * a 4px gutter, plus `extra` for an axis title.\n */\nexport const yAxisWidthFor = (labels: string[], extra = 0, fontFamily?: string) =>\n 4 + extra + Math.max(0, ...labels.map((l) => Math.ceil(tickLabelWidth(l, fontFamily))));\n\n/**\n * Right margin that keeps the last x label inside the chart: half the label past the point it is centred\n * on (`inset` from the plot's right edge), plus a 4px gutter; never less than 4px.\n */\nexport const rightMarginFor = (label: string, inset: number, fontFamily?: string) =>\n Math.max(4, tickLabelWidth(label, fontFamily) / 2 - inset + 4);\n\ninterface TickProps {\n formatter?: ValueFormatter | ((v: any) => string);\n payload?: { value: any };\n x?: number | string;\n y?: number | string;\n}\n\n/** X tick: 11px muted label, baseline 16.8px under the plot. Render with `tickSize={0}`, `tickMargin={0}`. */\nexport const A2uiXTick = ({ x, y, payload, formatter }: TickProps) => (\n <text {...A2UI_TICK} textAnchor=\"middle\" x={Number(x)} y={Number(y) + X_TICK_BASELINE}>\n {formatter ? formatter(payload?.value) : String(payload?.value ?? '')}\n </text>\n);\n\n/** Y tick: right-aligned, vertically centred. Render with `tickSize={0}`, `tickMargin={8}`. */\nexport const A2uiYTick = ({ x, y, payload, formatter }: TickProps) => (\n <text {...A2UI_TICK} dominantBaseline=\"middle\" textAnchor=\"end\" x={Number(x)} y={Number(y)}>\n {formatter ? formatter(payload?.value) : String(payload?.value ?? '')}\n </text>\n);\n\n/** Y tick for a category axis (horizontal bars). */\nexport const A2uiCategoryYTick = A2uiYTick;\n\n/** Axis title, as official: 11px / 500 in the chart foreground at 76%. */\nexport const AXIS_TITLE = {\n fill: 'var(--a2-chart-fg)',\n fillOpacity: 0.76,\n fontSize: 11,\n fontWeight: 500,\n} as const;\n"],"mappings":";;;;;AAMA,MAAa,iBAAiB;AAC9B,MAAa,kBAAkB;;AAG/B,MAAa,kBAAkB,OAAe,eAC5C,aAAa,iBAAiB,OAAO,QAAQ,cAAc,EAAE,IAAI,mBAAmB,KAAK,IAAI;;;;;AAM/F,MAAa,iBAAiB,QAAkB,QAAQ,GAAG,eACzD,IAAI,QAAQ,KAAK,IAAI,GAAG,GAAG,OAAO,KAAK,MAAM,KAAK,KAAK,eAAe,GAAG,UAAU,CAAC,CAAC,CAAC;;;;;AAMxF,MAAa,kBAAkB,OAAe,OAAe,eAC3D,KAAK,IAAI,GAAG,eAAe,OAAO,UAAU,IAAI,IAAI,QAAQ,CAAC;;AAU/D,MAAa,aAAa,EAAE,GAAG,GAAG,SAAS,gBACzC,oBAAC,QAAD;CAAM,GAAI;CAAW,YAAW;CAAS,GAAG,OAAO,CAAC;CAAG,GAAG,OAAO,CAAC,IAAI;CACnE,UAAA,YAAY,UAAU,SAAS,KAAK,IAAI,OAAO,SAAS,SAAS,EAAE;AAChE,CAAA;;AAIR,MAAa,aAAa,EAAE,GAAG,GAAG,SAAS,gBACzC,oBAAC,QAAD;CAAM,GAAI;CAAW,kBAAiB;CAAS,YAAW;CAAM,GAAG,OAAO,CAAC;CAAG,GAAG,OAAO,CAAC;CACtF,UAAA,YAAY,UAAU,SAAS,KAAK,IAAI,OAAO,SAAS,SAAS,EAAE;AAChE,CAAA;;AAIR,MAAa,oBAAoB;;AAGjC,MAAa,aAAa;CACxB,MAAM;CACN,aAAa;CACb,UAAU;CACV,YAAY;AACd"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface PathContext {
|
|
2
|
+
bezierCurveTo: (x1: number, y1: number, x2: number, y2: number, x: number, y: number) => void;
|
|
3
|
+
closePath: () => void;
|
|
4
|
+
lineTo: (x: number, y: number) => void;
|
|
5
|
+
moveTo: (x: number, y: number) => void;
|
|
6
|
+
}
|
|
7
|
+
declare class MonotoneCurve {
|
|
8
|
+
private context;
|
|
9
|
+
private line;
|
|
10
|
+
private points;
|
|
11
|
+
constructor(context: PathContext);
|
|
12
|
+
areaStart(): void;
|
|
13
|
+
areaEnd(): void;
|
|
14
|
+
lineStart(): void;
|
|
15
|
+
lineEnd(): void;
|
|
16
|
+
point(x: number, y: number): void;
|
|
17
|
+
}
|
|
18
|
+
/** Pass as recharts `type` on `Line` / `Area`. */
|
|
19
|
+
export declare const curveA2uiMonotone: (context: PathContext) => MonotoneCurve;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/common/a2ui/curve.ts
|
|
2
|
+
const drawMonotone = (context, points, move) => {
|
|
3
|
+
const n = points.length;
|
|
4
|
+
if (n === 0) return;
|
|
5
|
+
const [x0, y0] = points[0];
|
|
6
|
+
if (move) context.moveTo(x0, y0);
|
|
7
|
+
else context.lineTo(x0, y0);
|
|
8
|
+
if (n === 1) return;
|
|
9
|
+
if (n === 2) {
|
|
10
|
+
context.lineTo(points[1][0], points[1][1]);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const dx = [];
|
|
14
|
+
const m = [];
|
|
15
|
+
for (let i = 0; i < n - 1; i++) {
|
|
16
|
+
const h = points[i + 1][0] - points[i][0];
|
|
17
|
+
dx.push(h);
|
|
18
|
+
m.push(h === 0 ? 0 : (points[i + 1][1] - points[i][1]) / h);
|
|
19
|
+
}
|
|
20
|
+
const t = [m[0]];
|
|
21
|
+
for (let i = 1; i < n - 1; i++) t.push(m[i - 1] * m[i] <= 0 ? 0 : 3 * (dx[i - 1] + dx[i]) / ((2 * dx[i] + dx[i - 1]) / m[i - 1] + (dx[i] + 2 * dx[i - 1]) / m[i]));
|
|
22
|
+
t.push(m[n - 2]);
|
|
23
|
+
for (let i = 0; i < n - 1; i++) {
|
|
24
|
+
const [xa, ya] = points[i];
|
|
25
|
+
const [xb, yb] = points[i + 1];
|
|
26
|
+
const h = dx[i] / 3;
|
|
27
|
+
context.bezierCurveTo(xa + h, ya + t[i] * h, xb - h, yb - t[i + 1] * h, xb, yb);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var MonotoneCurve = class {
|
|
31
|
+
constructor(context) {
|
|
32
|
+
this.line = NaN;
|
|
33
|
+
this.points = [];
|
|
34
|
+
this.context = context;
|
|
35
|
+
}
|
|
36
|
+
areaStart() {
|
|
37
|
+
this.line = 0;
|
|
38
|
+
}
|
|
39
|
+
areaEnd() {
|
|
40
|
+
this.line = NaN;
|
|
41
|
+
}
|
|
42
|
+
lineStart() {
|
|
43
|
+
this.points = [];
|
|
44
|
+
}
|
|
45
|
+
lineEnd() {
|
|
46
|
+
drawMonotone(this.context, this.points, this.line !== 1);
|
|
47
|
+
if (this.line === 1) this.context.closePath();
|
|
48
|
+
this.line = 1 - this.line;
|
|
49
|
+
}
|
|
50
|
+
point(x, y) {
|
|
51
|
+
this.points.push([+x, +y]);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/** Pass as recharts `type` on `Line` / `Area`. */
|
|
55
|
+
const curveA2uiMonotone = (context) => new MonotoneCurve(context);
|
|
56
|
+
export { curveA2uiMonotone };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=curve.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve.mjs","names":[],"sources":["../../../src/common/a2ui/curve.ts"],"sourcesContent":["// Monotone cubic (Fritsch-Carlson) curve as a d3-shape CurveFactory, so recharts draws the same\n// smooth-without-overshoot lines and areas as the official charts.\n// Adapted from CopilotKit a2ui-playground (MIT): src/a2ui/catalog/renderers/chart-kit.tsx `monotonePath`.\n\ntype Point = [number, number];\n\ninterface PathContext {\n bezierCurveTo: (x1: number, y1: number, x2: number, y2: number, x: number, y: number) => void;\n closePath: () => void;\n lineTo: (x: number, y: number) => void;\n moveTo: (x: number, y: number) => void;\n}\n\nconst drawMonotone = (context: PathContext, points: Point[], move: boolean) => {\n const n = points.length;\n if (n === 0) return;\n const [x0, y0] = points[0];\n if (move) context.moveTo(x0, y0);\n else context.lineTo(x0, y0);\n if (n === 1) return;\n if (n === 2) {\n context.lineTo(points[1][0], points[1][1]);\n return;\n }\n const dx: number[] = [];\n const m: number[] = [];\n for (let i = 0; i < n - 1; i++) {\n const h = points[i + 1][0] - points[i][0];\n dx.push(h);\n m.push(h === 0 ? 0 : (points[i + 1][1] - points[i][1]) / h);\n }\n const t: number[] = [m[0]];\n for (let i = 1; i < n - 1; i++)\n t.push(\n m[i - 1] * m[i] <= 0\n ? 0\n : (3 * (dx[i - 1] + dx[i])) /\n ((2 * dx[i] + dx[i - 1]) / m[i - 1] + (dx[i] + 2 * dx[i - 1]) / m[i]),\n );\n t.push(m[n - 2]);\n for (let i = 0; i < n - 1; i++) {\n const [xa, ya] = points[i];\n const [xb, yb] = points[i + 1];\n const h = dx[i] / 3;\n context.bezierCurveTo(xa + h, ya + t[i] * h, xb - h, yb - t[i + 1] * h, xb, yb);\n }\n};\n\nclass MonotoneCurve {\n private context: PathContext;\n private line = Number.NaN;\n private points: Point[] = [];\n\n constructor(context: PathContext) {\n this.context = context;\n }\n\n areaStart() {\n this.line = 0;\n }\n\n areaEnd() {\n this.line = Number.NaN;\n }\n\n lineStart() {\n this.points = [];\n }\n\n lineEnd() {\n // In an area the second line (the baseline, reversed) continues the first one.\n drawMonotone(this.context, this.points, this.line !== 1);\n if (this.line === 1) this.context.closePath();\n this.line = 1 - this.line;\n }\n\n point(x: number, y: number) {\n this.points.push([+x, +y]);\n }\n}\n\n/** Pass as recharts `type` on `Line` / `Area`. */\nexport const curveA2uiMonotone = (context: PathContext) => new MonotoneCurve(context);\n"],"mappings":";AAaA,MAAM,gBAAgB,SAAsB,QAAiB,SAAkB;CAC7E,MAAM,IAAI,OAAO;CACjB,IAAI,MAAM,GAAG;CACb,MAAM,CAAC,IAAI,MAAM,OAAO;CACxB,IAAI,MAAM,QAAQ,OAAO,IAAI,EAAE;MAC1B,QAAQ,OAAO,IAAI,EAAE;CAC1B,IAAI,MAAM,GAAG;CACb,IAAI,MAAM,GAAG;EACX,QAAQ,OAAO,OAAO,EAAE,CAAC,IAAI,OAAO,EAAE,CA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KAAK,QAAQ,UAAU;EAC5C,KAAK,OAAO,IAAI,KAAK;CACvB;CAEA,MAAM,GAAW,GAAW;EAC1B,KAAK,OAAO,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;CAC3B;AACF;;AAGA,MAAa,qBAAqB,YAAyB,IAAI,cAAc,OAAO"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type ChartNumberFormat = 'number' | 'compact' | 'currency' | 'percent';
|
|
2
|
+
export interface A2uiFormatOptions {
|
|
3
|
+
currency?: string;
|
|
4
|
+
format?: ChartNumberFormat;
|
|
5
|
+
unit?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Full-precision value for tooltips, centers and labels. `percent` expects 0-100. */
|
|
8
|
+
export declare const formatNumber: (value: unknown, format?: ChartNumberFormat, unit?: string, currency?: string) => string;
|
|
9
|
+
/** Currency symbol for an ISO code, e.g. `USD` -> `$`. */
|
|
10
|
+
export declare const currencySymbol: (currency?: string) => string;
|
|
11
|
+
/** Value formatter for tooltips and labels. */
|
|
12
|
+
export declare const createValueFormatter: ({ format, unit, currency }?: A2uiFormatOptions) => (v: unknown) => string;
|
|
13
|
+
/** Compact axis ticks: $1.2K, 34%, 1.4M ms. */
|
|
14
|
+
export declare const createTickFormatter: ({ format, unit, currency }?: A2uiFormatOptions) => (v: number) => string;
|
|
15
|
+
/** Format used for big summary numbers (donut centre, legend values). */
|
|
16
|
+
export declare const summaryFormat: (format?: ChartNumberFormat) => ChartNumberFormat;
|
|
17
|
+
export declare const shortLabel: (v: unknown, max?: number) => string;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/common/a2ui/format.ts
|
|
2
|
+
const isFiniteNumber = (v) => typeof v === "number" && Number.isFinite(v);
|
|
3
|
+
/** Full-precision value for tooltips, centers and labels. `percent` expects 0-100. */
|
|
4
|
+
const formatNumber = (value, format = "number", unit, currency = "USD") => {
|
|
5
|
+
if (!isFiniteNumber(value)) return value === null || value === void 0 ? "" : String(value);
|
|
6
|
+
let out;
|
|
7
|
+
switch (format) {
|
|
8
|
+
case "currency":
|
|
9
|
+
out = new Intl.NumberFormat("en-US", {
|
|
10
|
+
currency,
|
|
11
|
+
maximumFractionDigits: Math.abs(value) >= 1e3 ? 0 : 2,
|
|
12
|
+
style: "currency"
|
|
13
|
+
}).format(value);
|
|
14
|
+
break;
|
|
15
|
+
case "percent":
|
|
16
|
+
out = `${new Intl.NumberFormat("en-US", { maximumFractionDigits: 1 }).format(value)}%`;
|
|
17
|
+
break;
|
|
18
|
+
case "compact":
|
|
19
|
+
out = new Intl.NumberFormat("en-US", {
|
|
20
|
+
maximumFractionDigits: 1,
|
|
21
|
+
notation: "compact"
|
|
22
|
+
}).format(value);
|
|
23
|
+
break;
|
|
24
|
+
default: out = new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 }).format(value);
|
|
25
|
+
}
|
|
26
|
+
if (format === "percent" && unit?.trim() === "%") unit = void 0;
|
|
27
|
+
return unit ? `${out}${unit.startsWith("%") || unit.startsWith("°") ? "" : " "}${unit}` : out;
|
|
28
|
+
};
|
|
29
|
+
/** Currency symbol for an ISO code, e.g. `USD` -> `$`. */
|
|
30
|
+
const currencySymbol = (currency = "USD") => {
|
|
31
|
+
try {
|
|
32
|
+
return new Intl.NumberFormat("en-US", {
|
|
33
|
+
currency,
|
|
34
|
+
style: "currency"
|
|
35
|
+
}).formatToParts(0).find((p) => p.type === "currency")?.value ?? currency;
|
|
36
|
+
} catch {
|
|
37
|
+
return currency;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/** Value formatter for tooltips and labels. */
|
|
41
|
+
const createValueFormatter = ({ format, unit, currency } = {}) => (v) => formatNumber(v, format ?? "number", unit, currency);
|
|
42
|
+
/** Compact axis ticks: $1.2K, 34%, 1.4M ms. */
|
|
43
|
+
const createTickFormatter = ({ format, unit, currency } = {}) => (v) => {
|
|
44
|
+
if (format === "currency") return `${v < 0 ? "-" : ""}${currencySymbol(currency)}${formatNumber(Math.abs(v), "compact")}`;
|
|
45
|
+
if (format === "percent") return formatNumber(v, "percent");
|
|
46
|
+
return formatNumber(v, Math.abs(v) >= 1e3 ? "compact" : "number", unit);
|
|
47
|
+
};
|
|
48
|
+
/** Format used for big summary numbers (donut centre, legend values). */
|
|
49
|
+
const summaryFormat = (format) => format === "currency" ? "currency" : format === "percent" ? "percent" : "compact";
|
|
50
|
+
const shortLabel = (v, max = 14) => {
|
|
51
|
+
const s = String(v ?? "");
|
|
52
|
+
return s.length > max ? `${s.slice(0, max - 1)}…` : s;
|
|
53
|
+
};
|
|
54
|
+
export { createTickFormatter, createValueFormatter, currencySymbol, formatNumber, shortLabel, summaryFormat };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=format.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.mjs","names":[],"sources":["../../../src/common/a2ui/format.ts"],"sourcesContent":["// Number formatting for the a2ui look.\n// Adapted from CopilotKit a2ui-playground (MIT): src/a2ui/catalog/renderers/format.ts + chart-kit.tsx\n\nexport type ChartNumberFormat = 'number' | 'compact' | 'currency' | 'percent';\n\nexport interface A2uiFormatOptions {\n currency?: string;\n format?: ChartNumberFormat;\n unit?: string;\n}\n\nconst isFiniteNumber = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);\n\n/** Full-precision value for tooltips, centers and labels. `percent` expects 0-100. */\nexport const formatNumber = (\n value: unknown,\n format: ChartNumberFormat = 'number',\n unit?: string,\n currency = 'USD',\n): string => {\n if (!isFiniteNumber(value)) return value === null || value === undefined ? '' : String(value);\n let out: string;\n switch (format) {\n case 'currency': {\n out = new Intl.NumberFormat('en-US', {\n currency,\n maximumFractionDigits: Math.abs(value) >= 1000 ? 0 : 2,\n style: 'currency',\n }).format(value);\n break;\n }\n case 'percent': {\n out = `${new Intl.NumberFormat('en-US', { maximumFractionDigits: 1 }).format(value)}%`;\n break;\n }\n case 'compact': {\n out = new Intl.NumberFormat('en-US', {\n maximumFractionDigits: 1,\n notation: 'compact',\n }).format(value);\n break;\n }\n default: {\n out = new Intl.NumberFormat('en-US', { maximumFractionDigits: 2 }).format(value);\n }\n }\n if (format === 'percent' && unit?.trim() === '%') unit = undefined;\n return unit ? `${out}${unit.startsWith('%') || unit.startsWith('°') ? '' : ' '}${unit}` : out;\n};\n\n/** Currency symbol for an ISO code, e.g. `USD` -> `$`. */\nexport const currencySymbol = (currency = 'USD'): string => {\n try {\n return (\n new Intl.NumberFormat('en-US', { currency, style: 'currency' })\n .formatToParts(0)\n .find((p) => p.type === 'currency')?.value ?? currency\n );\n } catch {\n return currency;\n }\n};\n\n/** Value formatter for tooltips and labels. */\nexport const createValueFormatter =\n ({ format, unit, currency }: A2uiFormatOptions = {}) =>\n (v: unknown) =>\n formatNumber(v, format ?? 'number', unit, currency);\n\n/** Compact axis ticks: $1.2K, 34%, 1.4M ms. */\nexport const createTickFormatter =\n ({ format, unit, currency }: A2uiFormatOptions = {}) =>\n (v: number) => {\n if (format === 'currency')\n return `${v < 0 ? '-' : ''}${currencySymbol(currency)}${formatNumber(Math.abs(v), 'compact')}`;\n if (format === 'percent') return formatNumber(v, 'percent');\n return formatNumber(v, Math.abs(v) >= 1000 ? 'compact' : 'number', unit);\n };\n\n/** Format used for big summary numbers (donut centre, legend values). */\nexport const summaryFormat = (format?: ChartNumberFormat): ChartNumberFormat =>\n format === 'currency' ? 'currency' : format === 'percent' ? 'percent' : 'compact';\n\nexport const shortLabel = (v: unknown, max = 14) => {\n const s = String(v ?? '');\n return s.length > max ? `${s.slice(0, max - 1)}…` : s;\n};\n"],"mappings":";AAWA,MAAM,kBAAkB,MAA4B,OAAO,MAAM,YAAY,OAAO,SAAS,CAAC;;AAG9F,MAAa,gBACX,OACA,SAA4B,UAC5B,MACA,WAAW,UACA;CACX,IAAI,CAAC,eAAe,KAAK,GAAG,OAAO,UAAU,QAAQ,UAAU,KAAA,IAAY,KAAK,OAAO,KAAK;CAC5F,IAAI;CACJ,QAAQ,QAAR;EACE,KAAK;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AC,KAAK,MAAO,YAAY,UAAU,IAAI;AACzE;;AAGF,MAAa,iBAAiB,WAC5B,WAAW,aAAa,aAAa,WAAW,YAAY,YAAY;AAE1E,MAAa,cAAc,GAAY,MAAM,OAAO;CAClD,MAAM,IAAI,OAAO,KAAK,EAAE;CACxB,OAAO,EAAE,SAAS,MAAM,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,EAAE,KAAK;AACtD"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import A2uiTrendChart, { A2uiTrendChartProps } from "./A2uiTrendChart.mjs";
|
|
2
|
+
import { A2uiCategoryYTick, A2uiXTick, A2uiYTick, AXIS_TITLE, X_TICK_BASELINE, Y_TICK_PADDING, rightMarginFor, tickLabelWidth, yAxisWidthFor } from "./axis.mjs";
|
|
3
|
+
import { curveA2uiMonotone } from "./curve.mjs";
|
|
4
|
+
import { A2uiFormatOptions, ChartNumberFormat, createTickFormatter, createValueFormatter, currencySymbol, formatNumber, shortLabel, summaryFormat } from "./format.mjs";
|
|
5
|
+
import A2uiFrame, { A2uiEmpty, A2uiFrameProps } from "./Frame.mjs";
|
|
6
|
+
import { A2uiFloatingTooltip, A2uiFloatingTooltipProps, A2uiHoverState, useA2uiHover } from "./HoverTooltip.mjs";
|
|
7
|
+
import A2uiLegend, { ChartFrameLegendItem } from "./Legend.mjs";
|
|
8
|
+
import A2uiPlacedTooltip, { A2uiPlacedTooltipProps } from "./PlacedTooltip.mjs";
|
|
9
|
+
import { estimateLabelWidth, fontFamilyOf, measureTextWidth, niceDomain, niceScale, ticks } from "./scale.mjs";
|
|
10
|
+
import { sparkStyles, styles } from "./styles.mjs";
|
|
11
|
+
import { A2UI_DARK_VARS, A2UI_GRID, A2UI_LIGHT_VARS, A2UI_NEGATIVE, A2UI_PALETTE, A2UI_POSITIVE, A2UI_SURFACE, A2UI_TICK, a2uiPaletteAt } from "./tokens.mjs";
|
|
12
|
+
import A2uiTooltip, { A2uiTooltipRow } from "./Tooltip.mjs";
|
|
13
|
+
import { A2uiChartProps, ChartVariant } from "./types.mjs";
|
|
14
|
+
import { useA2uiThemeClass } from "./useA2uiTheme.mjs";
|
|
15
|
+
import { useElementWidth } from "./useElementWidth.mjs";
|
|
16
|
+
export { A2UI_DARK_VARS, A2UI_GRID, A2UI_LIGHT_VARS, A2UI_NEGATIVE, A2UI_PALETTE, A2UI_POSITIVE, A2UI_SURFACE, A2UI_TICK, A2uiCategoryYTick, A2uiChartProps, A2uiEmpty, A2uiFloatingTooltip, type A2uiFloatingTooltipProps, A2uiFormatOptions, A2uiFrame, type A2uiFrameProps, type A2uiHoverState, A2uiLegend, A2uiPlacedTooltip, type A2uiPlacedTooltipProps, A2uiTooltip, type A2uiTooltipRow, A2uiTrendChart, type A2uiTrendChartProps, A2uiXTick, A2uiYTick, AXIS_TITLE, type ChartFrameLegendItem, ChartNumberFormat, ChartVariant, X_TICK_BASELINE, Y_TICK_PADDING, a2uiPaletteAt, sparkStyles as a2uiSparkStyles, styles as a2uiStyles, createTickFormatter, createValueFormatter, currencySymbol, curveA2uiMonotone, estimateLabelWidth, fontFamilyOf, formatNumber, measureTextWidth, niceDomain, niceScale, rightMarginFor, shortLabel, summaryFormat, tickLabelWidth, ticks, useA2uiHover, useA2uiThemeClass, useElementWidth, yAxisWidthFor };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import A2uiTrendChart from "./A2uiTrendChart.mjs";
|
|
2
|
+
import { A2uiCategoryYTick, A2uiXTick, A2uiYTick, AXIS_TITLE, X_TICK_BASELINE, Y_TICK_PADDING, rightMarginFor, tickLabelWidth, yAxisWidthFor } from "./axis.mjs";
|
|
3
|
+
import { curveA2uiMonotone } from "./curve.mjs";
|
|
4
|
+
import { createTickFormatter, createValueFormatter, currencySymbol, formatNumber, shortLabel, summaryFormat } from "./format.mjs";
|
|
5
|
+
import A2uiFrame, { A2uiEmpty } from "./Frame.mjs";
|
|
6
|
+
import { A2uiFloatingTooltip, useA2uiHover } from "./HoverTooltip.mjs";
|
|
7
|
+
import A2uiLegend from "./Legend.mjs";
|
|
8
|
+
import A2uiPlacedTooltip from "./PlacedTooltip.mjs";
|
|
9
|
+
import { estimateLabelWidth, fontFamilyOf, measureTextWidth, niceDomain, niceScale, ticks } from "./scale.mjs";
|
|
10
|
+
import { sparkStyles, styles } from "./styles.mjs";
|
|
11
|
+
import { A2UI_DARK_VARS, A2UI_GRID, A2UI_LIGHT_VARS, A2UI_NEGATIVE, A2UI_PALETTE, A2UI_POSITIVE, A2UI_SURFACE, A2UI_TICK, a2uiPaletteAt } from "./tokens.mjs";
|
|
12
|
+
import A2uiTooltip from "./Tooltip.mjs";
|
|
13
|
+
import { useA2uiThemeClass } from "./useA2uiTheme.mjs";
|
|
14
|
+
import { useElementWidth } from "./useElementWidth.mjs";
|
|
15
|
+
export { A2UI_DARK_VARS, A2UI_GRID, A2UI_LIGHT_VARS, A2UI_NEGATIVE, A2UI_PALETTE, A2UI_POSITIVE, A2UI_SURFACE, A2UI_TICK, A2uiCategoryYTick, A2uiEmpty, A2uiFloatingTooltip, A2uiFrame, A2uiLegend, A2uiPlacedTooltip, A2uiTooltip, A2uiTrendChart, A2uiXTick, A2uiYTick, AXIS_TITLE, X_TICK_BASELINE, Y_TICK_PADDING, a2uiPaletteAt, sparkStyles as a2uiSparkStyles, styles as a2uiStyles, createTickFormatter, createValueFormatter, currencySymbol, curveA2uiMonotone, estimateLabelWidth, fontFamilyOf, formatNumber, measureTextWidth, niceDomain, niceScale, rightMarginFor, shortLabel, summaryFormat, tickLabelWidth, ticks, useA2uiHover, useA2uiThemeClass, useElementWidth, yAxisWidthFor };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const ticks: (start: number, stop: number, count: number) => number[];
|
|
2
|
+
/** d3-scale `linear().domain([min, max]).nice(count)`. */
|
|
3
|
+
export declare const niceDomain: (min: number, max: number, count?: number) => [number, number];
|
|
4
|
+
/** Nice domain and its ticks for a set of values. */
|
|
5
|
+
export declare const niceScale: (values: number[], { includeZero, count }?: {
|
|
6
|
+
includeZero?: boolean | undefined;
|
|
7
|
+
count?: number | undefined;
|
|
8
|
+
}) => {
|
|
9
|
+
domain: [number, number];
|
|
10
|
+
ticks: number[];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Width reserved for an 11px tick label. The official axes reserve a fixed advance per character,
|
|
14
|
+
* so the plot starts at the same place regardless of the host font.
|
|
15
|
+
*/
|
|
16
|
+
export declare const estimateLabelWidth: (text: string, fontSize?: number) => number;
|
|
17
|
+
/** Text width with the given CSS font, via canvas when available, otherwise an estimate. */
|
|
18
|
+
export declare const measureTextWidth: (text: string, font: string, fontSize: number) => number;
|
|
19
|
+
/** Font family an element renders with, for text measurement. */
|
|
20
|
+
export declare const fontFamilyOf: (el?: Element | null) => string;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// src/common/a2ui/scale.ts
|
|
2
|
+
const e10 = Math.sqrt(50);
|
|
3
|
+
const e5 = Math.sqrt(10);
|
|
4
|
+
const e2 = Math.sqrt(2);
|
|
5
|
+
const tickSpec = (start, stop, count) => {
|
|
6
|
+
const step = (stop - start) / Math.max(0, count);
|
|
7
|
+
const power = Math.floor(Math.log10(step));
|
|
8
|
+
const error = step / 10 ** power;
|
|
9
|
+
const factor = error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1;
|
|
10
|
+
let i1, i2, inc;
|
|
11
|
+
if (power < 0) {
|
|
12
|
+
inc = 10 ** -power / factor;
|
|
13
|
+
i1 = Math.round(start * inc);
|
|
14
|
+
i2 = Math.round(stop * inc);
|
|
15
|
+
if (i1 / inc < start) ++i1;
|
|
16
|
+
if (i2 / inc > stop) --i2;
|
|
17
|
+
inc = -inc;
|
|
18
|
+
} else {
|
|
19
|
+
inc = 10 ** power * factor;
|
|
20
|
+
i1 = Math.round(start / inc);
|
|
21
|
+
i2 = Math.round(stop / inc);
|
|
22
|
+
if (i1 * inc < start) ++i1;
|
|
23
|
+
if (i2 * inc > stop) --i2;
|
|
24
|
+
}
|
|
25
|
+
if (i2 < i1 && count >= .5 && count < 2) return tickSpec(start, stop, count * 2);
|
|
26
|
+
return [
|
|
27
|
+
i1,
|
|
28
|
+
i2,
|
|
29
|
+
inc
|
|
30
|
+
];
|
|
31
|
+
};
|
|
32
|
+
const tickIncrement = (start, stop, count) => tickSpec(start, stop, count)[2];
|
|
33
|
+
const ticks = (start, stop, count) => {
|
|
34
|
+
if (!(count > 0) || start === stop) return [start];
|
|
35
|
+
const reverse = stop < start;
|
|
36
|
+
const [i1, i2, inc] = reverse ? tickSpec(stop, start, count) : tickSpec(start, stop, count);
|
|
37
|
+
if (!(i2 >= i1)) return [];
|
|
38
|
+
const n = i2 - i1 + 1;
|
|
39
|
+
const out = Array.from({ length: n }, () => 0);
|
|
40
|
+
if (reverse) {
|
|
41
|
+
if (inc < 0) for (let i = 0; i < n; ++i) out[i] = (i2 - i) / -inc;
|
|
42
|
+
else for (let i = 0; i < n; ++i) out[i] = (i2 - i) * inc;
|
|
43
|
+
} else if (inc < 0) for (let i = 0; i < n; ++i) out[i] = (i1 + i) / -inc;
|
|
44
|
+
else for (let i = 0; i < n; ++i) out[i] = (i1 + i) * inc;
|
|
45
|
+
return out;
|
|
46
|
+
};
|
|
47
|
+
/** d3-scale `linear().domain([min, max]).nice(count)`. */
|
|
48
|
+
const niceDomain = (min, max, count = 5) => {
|
|
49
|
+
let start = min;
|
|
50
|
+
let stop = max;
|
|
51
|
+
if (stop < start) [start, stop] = [stop, start];
|
|
52
|
+
if (start === stop) {
|
|
53
|
+
if (start === 0) return [0, 1];
|
|
54
|
+
const pad = Math.abs(start) * .1;
|
|
55
|
+
start -= pad;
|
|
56
|
+
stop += pad;
|
|
57
|
+
}
|
|
58
|
+
let prestep;
|
|
59
|
+
for (let i = 0; i < 10; i++) {
|
|
60
|
+
const step = tickIncrement(start, stop, count);
|
|
61
|
+
if (step === prestep) break;
|
|
62
|
+
if (step > 0) {
|
|
63
|
+
start = Math.floor(start / step) * step;
|
|
64
|
+
stop = Math.ceil(stop / step) * step;
|
|
65
|
+
} else if (step < 0) {
|
|
66
|
+
start = Math.ceil(start * step) / step;
|
|
67
|
+
stop = Math.floor(stop * step) / step;
|
|
68
|
+
} else break;
|
|
69
|
+
prestep = step;
|
|
70
|
+
}
|
|
71
|
+
return [start, stop];
|
|
72
|
+
};
|
|
73
|
+
/** Nice domain and its ticks for a set of values. */
|
|
74
|
+
const niceScale = (values, { includeZero = false, count = 5 } = {}) => {
|
|
75
|
+
const finite = values.filter((v) => Number.isFinite(v));
|
|
76
|
+
let min = finite.length > 0 ? Math.min(...finite) : 0;
|
|
77
|
+
let max = finite.length > 0 ? Math.max(...finite) : 1;
|
|
78
|
+
if (includeZero) {
|
|
79
|
+
min = Math.min(0, min);
|
|
80
|
+
max = Math.max(0, max);
|
|
81
|
+
}
|
|
82
|
+
const domain = niceDomain(min, max, count);
|
|
83
|
+
return {
|
|
84
|
+
domain,
|
|
85
|
+
ticks: ticks(domain[0], domain[1], count)
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Width reserved for an 11px tick label. The official axes reserve a fixed advance per character,
|
|
90
|
+
* so the plot starts at the same place regardless of the host font.
|
|
91
|
+
*/
|
|
92
|
+
const estimateLabelWidth = (text, fontSize = 11) => Math.ceil(String(text).length * fontSize * (8 / 11));
|
|
93
|
+
let measureContext;
|
|
94
|
+
/** Text width with the given CSS font, via canvas when available, otherwise an estimate. */
|
|
95
|
+
const measureTextWidth = (text, font, fontSize) => {
|
|
96
|
+
if (measureContext === void 0) try {
|
|
97
|
+
measureContext = typeof document === "undefined" ? null : document.createElement("canvas").getContext("2d");
|
|
98
|
+
} catch {
|
|
99
|
+
measureContext = null;
|
|
100
|
+
}
|
|
101
|
+
if (measureContext) {
|
|
102
|
+
measureContext.font = font;
|
|
103
|
+
const w = measureContext.measureText(text).width;
|
|
104
|
+
if (w > 0) return w;
|
|
105
|
+
}
|
|
106
|
+
return text.length * fontSize * .6;
|
|
107
|
+
};
|
|
108
|
+
/** Font family an element renders with, for text measurement. */
|
|
109
|
+
const fontFamilyOf = (el) => el && typeof getComputedStyle === "function" ? getComputedStyle(el).fontFamily : "sans-serif";
|
|
110
|
+
export { estimateLabelWidth, fontFamilyOf, measureTextWidth, niceDomain, niceScale, ticks };
|
|
111
|
+
|
|
112
|
+
//# sourceMappingURL=scale.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scale.mjs","names":[],"sources":["../../../src/common/a2ui/scale.ts"],"sourcesContent":["// Linear \"nice\" ticks and the label-width estimate the a2ui axes use.\n// Tick math adapted from d3-array `ticks` / d3-scale `nice` (ISC, Mike Bostock).\n\nconst e10 = Math.sqrt(50);\nconst e5 = Math.sqrt(10);\nconst e2 = Math.sqrt(2);\n\nconst tickSpec = (start: number, stop: number, count: number): [number, number, number] => {\n const step = (stop - start) / Math.max(0, count);\n const power = Math.floor(Math.log10(step));\n const error = step / 10 ** power;\n const factor = error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1;\n let i1: number, i2: number, inc: number;\n if (power < 0) {\n inc = 10 ** -power / factor;\n i1 = Math.round(start * inc);\n i2 = Math.round(stop * inc);\n if (i1 / inc < start) ++i1;\n if (i2 / inc > stop) --i2;\n inc = -inc;\n } else {\n inc = 10 ** power * factor;\n i1 = Math.round(start / inc);\n i2 = Math.round(stop / inc);\n if (i1 * inc < start) ++i1;\n if (i2 * inc > stop) --i2;\n }\n if (i2 < i1 && count >= 0.5 && count < 2) return tickSpec(start, stop, count * 2);\n return [i1, i2, inc];\n};\n\nconst tickIncrement = (start: number, stop: number, count: number) =>\n tickSpec(start, stop, count)[2];\n\nexport const ticks = (start: number, stop: number, count: number): number[] => {\n if (!(count > 0) || start === stop) return [start];\n const reverse = stop < start;\n const [i1, i2, inc] = reverse ? tickSpec(stop, start, count) : tickSpec(start, stop, count);\n if (!(i2 >= i1)) return [];\n const n = i2 - i1 + 1;\n const out = Array.from({ length: n }, () => 0);\n if (reverse) {\n if (inc < 0) for (let i = 0; i < n; ++i) out[i] = (i2 - i) / -inc;\n else for (let i = 0; i < n; ++i) out[i] = (i2 - i) * inc;\n } else if (inc < 0) for (let i = 0; i < n; ++i) out[i] = (i1 + i) / -inc;\n else for (let i = 0; i < n; ++i) out[i] = (i1 + i) * inc;\n return out;\n};\n\n/** d3-scale `linear().domain([min, max]).nice(count)`. */\nexport const niceDomain = (min: number, max: number, count = 5): [number, number] => {\n let start = min;\n let stop = max;\n if (stop < start) [start, stop] = [stop, start];\n if (start === stop) {\n if (start === 0) return [0, 1];\n const pad = Math.abs(start) * 0.1;\n start -= pad;\n stop += pad;\n }\n let prestep: number | undefined;\n for (let i = 0; i < 10; i++) {\n const step = tickIncrement(start, stop, count);\n if (step === prestep) break;\n if (step > 0) {\n start = Math.floor(start / step) * step;\n stop = Math.ceil(stop / step) * step;\n } else if (step < 0) {\n start = Math.ceil(start * step) / step;\n stop = Math.floor(stop * step) / step;\n } else break;\n prestep = step;\n }\n return [start, stop];\n};\n\n/** Nice domain and its ticks for a set of values. */\nexport const niceScale = (values: number[], { includeZero = false, count = 5 } = {}) => {\n const finite = values.filter((v) => Number.isFinite(v));\n let min = finite.length > 0 ? Math.min(...finite) : 0;\n let max = finite.length > 0 ? Math.max(...finite) : 1;\n if (includeZero) {\n min = Math.min(0, min);\n max = Math.max(0, max);\n }\n const domain = niceDomain(min, max, count);\n return { domain, ticks: ticks(domain[0], domain[1], count) };\n};\n\n/**\n * Width reserved for an 11px tick label. The official axes reserve a fixed advance per character,\n * so the plot starts at the same place regardless of the host font.\n */\nexport const estimateLabelWidth = (text: string, fontSize = 11) =>\n Math.ceil(String(text).length * fontSize * (8 / 11));\n\nlet measureContext: CanvasRenderingContext2D | null | undefined;\n\n/** Text width with the given CSS font, via canvas when available, otherwise an estimate. */\nexport const measureTextWidth = (text: string, font: string, fontSize: number) => {\n if (measureContext === undefined) {\n try {\n measureContext =\n typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');\n } catch {\n measureContext = null;\n }\n }\n if (measureContext) {\n measureContext.font = font;\n const w = measureContext.measureText(text).width;\n if (w > 0) return w;\n }\n return text.length * fontSize * 0.6;\n};\n\n/** Font family an element renders with, for text measurement. */\nexport const fontFamilyOf = (el?: Element | null) =>\n el && typeof getComputedStyle === 'function' ? getComputedStyle(el).fontFamily : 'sans-serif';\n"],"mappings":";AAGA,MAAM,MAAM,KAAK,KAAK,EAAE;AACxB,MAAM,KAAK,KAAK,KAAK,EAAE;AACvB,MAAM,KAAK,KAAK,KAAK,CAAC;AAEtB,MAAM,YAAY,OAAe,MAAc,UAA4C;CACzF,MAAM,QAAQ,OAAO,SAAS,KAAK,IAAI,GAAG,KAAK;CAC/C,MAAM,QAAQ,KAAK,MAAM,KAAK,MAAM,IAAI,CAAC;CACzC,MAAM,QAAQ,OAAO,MAAM;CAC3B,MAAM,SAAS,SAAS,MAAM,KAAK,SAAS,KAAK,IAAI,SAAS,KAAK,IAAI;CACvE,IAAI,IAAY,IAAY;CAC5B,IAAI,QAAQ,GAAG;EACb,MAAM,MAAM,CAAC,QAAQ;EACrB,KAAK,KAAK,MAAM,QAAQ,GAAG;EAC3B,KAAK,KAAK,MAAM,OAAO,GAAG;EAC1B,IAAI,KAAK,MAAM,OAAO,EAAE;EACxB,IAAI,KAAK,MAAM,MAAM,EAAE;EACvB,MAAM,CAAC;CACT,OAAO;EACL,MAAM,MAAM,QAAQ;EACpB,KAAK,KAAK,MAAM,QAAQ,GAAG;EAC3B,KAAK,KAAK,MAAM,OAAO,GAAG;EAC1B,IAAI,KAAK,MAAM,OAAO,EAAE;EACxB,IAAI,KAAK,MAAM,MAAM,EAAE;CACzB;CACA,IAAI,KAAK,MAAM,SAAS,MAAO,QAAQ,GAAG,OAAO,SAAS,OAAO,MAAM,QAAQ,CAAC;CAChF,OAAO;EAAC;EAAI;EAAI;CAAG;AACrB;AAEA,MAAM,iBAAiB,OAAe,MAAc,UAClD,SAAS,OAA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AC3B,MAAM,OAAO,qBAAqB,aAAa,iBAAiB,EAAE,CAAC,CAAC,aAAa"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare const styles: {
|
|
2
|
+
card: string;
|
|
3
|
+
dark: string;
|
|
4
|
+
empty: string;
|
|
5
|
+
frame: string;
|
|
6
|
+
header: string;
|
|
7
|
+
headerText: string;
|
|
8
|
+
legend: string;
|
|
9
|
+
legendItem: string;
|
|
10
|
+
legendLabel: string;
|
|
11
|
+
legendValue: string;
|
|
12
|
+
light: string;
|
|
13
|
+
plot: string;
|
|
14
|
+
subtitle: string;
|
|
15
|
+
swatch: string;
|
|
16
|
+
title: string;
|
|
17
|
+
tooltip: string;
|
|
18
|
+
tooltipRow: string;
|
|
19
|
+
tooltipRowActive: string;
|
|
20
|
+
tooltipSwatch: string;
|
|
21
|
+
tooltipTitle: string;
|
|
22
|
+
tooltipValue: string;
|
|
23
|
+
};
|
|
24
|
+
export declare const sparkStyles: {
|
|
25
|
+
chart: string;
|
|
26
|
+
chip: string;
|
|
27
|
+
chipNegative: string;
|
|
28
|
+
chipNeutral: string;
|
|
29
|
+
chipPositive: string;
|
|
30
|
+
label: string;
|
|
31
|
+
meta: string;
|
|
32
|
+
root: string;
|
|
33
|
+
value: string;
|
|
34
|
+
valueRow: string;
|
|
35
|
+
};
|