@terreno/ui 57.3.0 → 57.4.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/dist/AnnouncementBanner.d.ts +12 -0
- package/dist/AnnouncementBanner.js +61 -0
- package/dist/AnnouncementBanner.js.map +1 -0
- package/dist/AnnouncementNavigator.d.ts +11 -0
- package/dist/AnnouncementNavigator.js +189 -0
- package/dist/AnnouncementNavigator.js.map +1 -0
- package/dist/AnnouncementScreen.d.ts +12 -0
- package/dist/AnnouncementScreen.js +28 -0
- package/dist/AnnouncementScreen.js.map +1 -0
- package/dist/AreaChart.d.ts +5 -0
- package/dist/AreaChart.js +69 -0
- package/dist/AreaChart.js.map +1 -0
- package/dist/BarChart.d.ts +5 -0
- package/dist/BarChart.js +89 -0
- package/dist/BarChart.js.map +1 -0
- package/dist/Button.js +17 -3
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +96 -0
- package/dist/Common.js.map +1 -1
- package/dist/DashboardGrid.d.ts +4 -0
- package/dist/DashboardGrid.js +38 -0
- package/dist/DashboardGrid.js.map +1 -0
- package/dist/DataTable.js +83 -11
- package/dist/DataTable.js.map +1 -1
- package/dist/DonutChart.d.ts +5 -0
- package/dist/DonutChart.js +84 -0
- package/dist/DonutChart.js.map +1 -0
- package/dist/Filter.js +12 -2
- package/dist/Filter.js.map +1 -1
- package/dist/GPTChat.d.ts +7 -1
- package/dist/GPTChat.js +28 -15
- package/dist/GPTChat.js.map +1 -1
- package/dist/LineChart.d.ts +5 -0
- package/dist/LineChart.js +68 -0
- package/dist/LineChart.js.map +1 -0
- package/dist/MarkdownEmbed.d.ts +6 -0
- package/dist/MarkdownEmbed.js +17 -0
- package/dist/MarkdownEmbed.js.map +1 -0
- package/dist/MarkdownView.js +38 -3
- package/dist/MarkdownView.js.map +1 -1
- package/dist/Modal.js +12 -10
- package/dist/Modal.js.map +1 -1
- package/dist/ModalSheet.d.ts +5 -1
- package/dist/ModalSheet.js +31 -13
- package/dist/ModalSheet.js.map +1 -1
- package/dist/MultiselectField.js +4 -9
- package/dist/MultiselectField.js.map +1 -1
- package/dist/NotificationBell.d.ts +16 -0
- package/dist/NotificationBell.js +15 -0
- package/dist/NotificationBell.js.map +1 -0
- package/dist/NotificationInbox.d.ts +21 -0
- package/dist/NotificationInbox.js +41 -0
- package/dist/NotificationInbox.js.map +1 -0
- package/dist/NotificationPreferences.d.ts +14 -0
- package/dist/NotificationPreferences.js +32 -0
- package/dist/NotificationPreferences.js.map +1 -0
- package/dist/TextField.js +83 -6
- package/dist/TextField.js.map +1 -1
- package/dist/announcementBuildVersion.d.ts +6 -0
- package/dist/announcementBuildVersion.js +21 -0
- package/dist/announcementBuildVersion.js.map +1 -0
- package/dist/announcementFrequency.d.ts +42 -0
- package/dist/announcementFrequency.js +163 -0
- package/dist/announcementFrequency.js.map +1 -0
- package/dist/announcementPlatform.d.ts +2 -0
- package/dist/announcementPlatform.js +11 -0
- package/dist/announcementPlatform.js.map +1 -0
- package/dist/charts/ChartFrame.d.ts +12 -0
- package/dist/charts/ChartFrame.js +13 -0
- package/dist/charts/ChartFrame.js.map +1 -0
- package/dist/charts/bars.d.ts +18 -0
- package/dist/charts/bars.js +29 -0
- package/dist/charts/bars.js.map +1 -0
- package/dist/charts/layout.d.ts +47 -0
- package/dist/charts/layout.js +56 -0
- package/dist/charts/layout.js.map +1 -0
- package/dist/charts/paths.d.ts +31 -0
- package/dist/charts/paths.js +52 -0
- package/dist/charts/paths.js.map +1 -0
- package/dist/charts/scales.d.ts +6 -0
- package/dist/charts/scales.js +43 -0
- package/dist/charts/scales.js.map +1 -0
- package/dist/charts/theme.d.ts +9 -0
- package/dist/charts/theme.js +15 -0
- package/dist/charts/theme.js.map +1 -0
- package/dist/charts/types/chartTypes.d.ts +17 -0
- package/dist/charts/types/chartTypes.js +2 -0
- package/dist/charts/types/chartTypes.js.map +1 -0
- package/dist/dashboardGridLayout.d.ts +22 -0
- package/dist/dashboardGridLayout.js +33 -0
- package/dist/dashboardGridLayout.js.map +1 -0
- package/dist/dataTableFilters.d.ts +33 -0
- package/dist/dataTableFilters.js +221 -0
- package/dist/dataTableFilters.js.map +1 -0
- package/dist/dataTableListQuery.d.ts +42 -0
- package/dist/dataTableListQuery.js +161 -0
- package/dist/dataTableListQuery.js.map +1 -0
- package/dist/index.d.ts +18 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +12 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js +8 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/markdownEmbeds.d.ts +6 -0
- package/dist/markdownEmbeds.js +59 -0
- package/dist/markdownEmbeds.js.map +1 -0
- package/dist/testing/resolveTestId.js +2 -1
- package/dist/testing/resolveTestId.js.map +1 -1
- package/dist/testing/types.d.ts +3 -0
- package/dist/textFieldOscillationGuard.d.ts +25 -0
- package/dist/textFieldOscillationGuard.js +41 -0
- package/dist/textFieldOscillationGuard.js.map +1 -0
- package/dist/useAcknowledgeAnnouncement.d.ts +71 -0
- package/dist/useAcknowledgeAnnouncement.js +94 -0
- package/dist/useAcknowledgeAnnouncement.js.map +1 -0
- package/dist/useAnnouncements.d.ts +73 -0
- package/dist/useAnnouncements.js +66 -0
- package/dist/useAnnouncements.js.map +1 -0
- package/package.json +7 -18
- package/src/AnnouncementBanner.test.tsx +267 -0
- package/src/AnnouncementBanner.tsx +128 -0
- package/src/AnnouncementNavigator.test.tsx +759 -0
- package/src/AnnouncementNavigator.tsx +287 -0
- package/src/AnnouncementScreen.test.tsx +97 -0
- package/src/AnnouncementScreen.tsx +70 -0
- package/src/AreaChart.test.tsx +68 -0
- package/src/AreaChart.tsx +194 -0
- package/src/BarChart.test.tsx +96 -0
- package/src/BarChart.tsx +208 -0
- package/src/Button.test.tsx +66 -0
- package/src/Button.tsx +17 -2
- package/src/Common.ts +95 -0
- package/src/ConsentNavigator.test.tsx +55 -0
- package/src/DashboardGrid.test.tsx +75 -0
- package/src/DashboardGrid.tsx +69 -0
- package/src/DataTable.test.tsx +296 -1
- package/src/DataTable.tsx +210 -18
- package/src/DonutChart.test.tsx +121 -0
- package/src/DonutChart.tsx +160 -0
- package/src/EmojiSelector.test.tsx +51 -0
- package/src/Field.test.tsx +18 -0
- package/src/Filter.test.tsx +25 -0
- package/src/Filter.tsx +38 -7
- package/src/GPTChat.test.tsx +107 -0
- package/src/GPTChat.tsx +176 -86
- package/src/LineChart.test.tsx +166 -0
- package/src/LineChart.tsx +190 -0
- package/src/MarkdownEmbed.test.tsx +34 -0
- package/src/MarkdownEmbed.tsx +43 -0
- package/src/MarkdownView.test.tsx +59 -2
- package/src/MarkdownView.tsx +51 -3
- package/src/Modal.test.tsx +107 -14
- package/src/Modal.tsx +20 -8
- package/src/ModalSheet.test.tsx +121 -9
- package/src/ModalSheet.tsx +46 -11
- package/src/MultiselectField.test.tsx +21 -0
- package/src/MultiselectField.tsx +11 -13
- package/src/NotificationBell.test.tsx +91 -0
- package/src/NotificationBell.tsx +77 -0
- package/src/NotificationInbox.test.tsx +130 -0
- package/src/NotificationInbox.tsx +144 -0
- package/src/NotificationPreferences.test.tsx +25 -0
- package/src/NotificationPreferences.tsx +75 -0
- package/src/RootImportRegression.test.tsx +20 -0
- package/src/TextField.test.tsx +421 -0
- package/src/TextField.tsx +121 -4
- package/src/__snapshots__/Field.test.tsx.snap +22 -0
- package/src/__snapshots__/ModalSheet.test.tsx.snap +4 -1
- package/src/__snapshots__/TextArea.test.tsx.snap +5 -5
- package/src/__snapshots__/TextField.test.tsx.snap +5 -5
- package/src/announcementBuildVersion.test.ts +43 -0
- package/src/announcementBuildVersion.ts +21 -0
- package/src/announcementFrequency.test.ts +245 -0
- package/src/announcementFrequency.ts +246 -0
- package/src/announcementPlatform.test.ts +27 -0
- package/src/announcementPlatform.ts +13 -0
- package/src/charts/ChartFrame.tsx +67 -0
- package/src/charts/bars.test.ts +56 -0
- package/src/charts/bars.ts +53 -0
- package/src/charts/layout.test.ts +80 -0
- package/src/charts/layout.ts +99 -0
- package/src/charts/paths.test.ts +79 -0
- package/src/charts/paths.ts +100 -0
- package/src/charts/scales.test.ts +59 -0
- package/src/charts/scales.ts +60 -0
- package/src/charts/theme.test.ts +22 -0
- package/src/charts/theme.ts +24 -0
- package/src/charts/types/chartTypes.ts +19 -0
- package/src/dashboardGridLayout.test.ts +90 -0
- package/src/dashboardGridLayout.ts +60 -0
- package/src/dataTableFilters.test.tsx +552 -0
- package/src/dataTableFilters.tsx +441 -0
- package/src/dataTableListQuery.test.ts +220 -0
- package/src/dataTableListQuery.ts +201 -0
- package/src/index.tsx +21 -1
- package/src/lazyBoundaries/heavyOptionalExports.tsx +28 -0
- package/src/login/__snapshots__/LoginScreen.test.tsx.snap +22 -0
- package/src/markdownEmbeds.test.ts +53 -0
- package/src/markdownEmbeds.ts +62 -0
- package/src/pdfHtmlTemplate.test.ts +119 -0
- package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +22 -0
- package/src/testing/resolveTestId.test.ts +3 -0
- package/src/testing/resolveTestId.ts +1 -0
- package/src/testing/types.ts +3 -0
- package/src/textFieldOscillationGuard.test.ts +63 -0
- package/src/textFieldOscillationGuard.ts +89 -0
- package/src/types/react-native-markdown-display.d.ts +6 -0
- package/src/types/react-native-swiper-flatlist.d.ts +5 -5
- package/src/useAcknowledgeAnnouncement.test.ts +223 -0
- package/src/useAcknowledgeAnnouncement.ts +166 -0
- package/src/useAnnouncements.test.ts +226 -0
- package/src/useAnnouncements.ts +150 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type {FC, ReactNode} from "react";
|
|
2
|
+
|
|
3
|
+
import {Box} from "../Box";
|
|
4
|
+
import {Spinner} from "../Spinner";
|
|
5
|
+
import {Text} from "../Text";
|
|
6
|
+
import {resolveTestID, toTestProps} from "../testing/resolveTestId";
|
|
7
|
+
import {CHART_FOOTER_ROW_HEIGHT} from "./layout";
|
|
8
|
+
|
|
9
|
+
export interface ChartFrameProps {
|
|
10
|
+
accessibilityLabel?: string;
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
emptyText: string;
|
|
13
|
+
isEmpty: boolean;
|
|
14
|
+
legendLabel?: string;
|
|
15
|
+
loading?: boolean;
|
|
16
|
+
testID?: string;
|
|
17
|
+
tooltipText?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const ChartFrame: FC<ChartFrameProps> = ({
|
|
21
|
+
accessibilityLabel,
|
|
22
|
+
children,
|
|
23
|
+
emptyText,
|
|
24
|
+
isEmpty,
|
|
25
|
+
legendLabel,
|
|
26
|
+
loading = false,
|
|
27
|
+
testID,
|
|
28
|
+
tooltipText,
|
|
29
|
+
}) => {
|
|
30
|
+
const showPlot = !loading && !isEmpty;
|
|
31
|
+
|
|
32
|
+
// alignSelf stretch keeps the frame full width even when a parent centers its children, so the
|
|
33
|
+
// measured plot width comes from the container instead of the chart's own content.
|
|
34
|
+
return (
|
|
35
|
+
<Box
|
|
36
|
+
accessibilityLabel={accessibilityLabel}
|
|
37
|
+
alignSelf="stretch"
|
|
38
|
+
minWidth={0}
|
|
39
|
+
width="100%"
|
|
40
|
+
{...toTestProps(testID)}
|
|
41
|
+
>
|
|
42
|
+
{loading ? (
|
|
43
|
+
<Box padding={4}>
|
|
44
|
+
<Spinner testID={resolveTestID(testID, "spinner")} />
|
|
45
|
+
</Box>
|
|
46
|
+
) : null}
|
|
47
|
+
{isEmpty && !loading ? <Text skipLinking>{emptyText}</Text> : null}
|
|
48
|
+
{showPlot ? children : null}
|
|
49
|
+
{showPlot ? (
|
|
50
|
+
<Box height={CHART_FOOTER_ROW_HEIGHT} minWidth={0} overflow="hidden">
|
|
51
|
+
{tooltipText ? (
|
|
52
|
+
<Text size="sm" skipLinking testID={resolveTestID(testID, "tooltip")} truncate>
|
|
53
|
+
{tooltipText}
|
|
54
|
+
</Text>
|
|
55
|
+
) : null}
|
|
56
|
+
</Box>
|
|
57
|
+
) : null}
|
|
58
|
+
{showPlot && legendLabel ? (
|
|
59
|
+
<Box height={CHART_FOOTER_ROW_HEIGHT} minWidth={0} overflow="hidden">
|
|
60
|
+
<Text size="sm" skipLinking testID={resolveTestID(testID, "legend")} truncate>
|
|
61
|
+
{legendLabel}
|
|
62
|
+
</Text>
|
|
63
|
+
</Box>
|
|
64
|
+
) : null}
|
|
65
|
+
</Box>
|
|
66
|
+
);
|
|
67
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {getBarLayout, MARK_HIT_SIZE} from "./bars";
|
|
4
|
+
|
|
5
|
+
const yIdentity = (value: number): number => {
|
|
6
|
+
return 100 - value;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
describe("getBarLayout", () => {
|
|
10
|
+
it("draws a positive bar from the value down to the zero baseline", () => {
|
|
11
|
+
const layout = getBarLayout({
|
|
12
|
+
barWidth: 20,
|
|
13
|
+
baselineY: 100,
|
|
14
|
+
value: 40,
|
|
15
|
+
xCenter: 50,
|
|
16
|
+
y: yIdentity,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
expect(layout.x).toBe(40);
|
|
20
|
+
expect(layout.y).toBe(60);
|
|
21
|
+
expect(layout.width).toBe(20);
|
|
22
|
+
expect(layout.height).toBe(40);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("draws a negative bar from the zero baseline down to the value", () => {
|
|
26
|
+
const layout = getBarLayout({
|
|
27
|
+
barWidth: 20,
|
|
28
|
+
baselineY: 40,
|
|
29
|
+
value: -20,
|
|
30
|
+
xCenter: 50,
|
|
31
|
+
y: yIdentity,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(layout.x).toBe(40);
|
|
35
|
+
expect(layout.y).toBe(40);
|
|
36
|
+
expect(layout.width).toBe(20);
|
|
37
|
+
expect(layout.height).toBe(80);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("omits a zero-value bar and keeps a pressable hit target on the baseline", () => {
|
|
41
|
+
const layout = getBarLayout({
|
|
42
|
+
barWidth: 8,
|
|
43
|
+
baselineY: 100,
|
|
44
|
+
value: 0,
|
|
45
|
+
xCenter: 50,
|
|
46
|
+
y: yIdentity,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(layout.height).toBe(0);
|
|
50
|
+
expect(layout.y).toBe(100);
|
|
51
|
+
expect(layout.hitWidth).toBe(MARK_HIT_SIZE);
|
|
52
|
+
expect(layout.hitHeight).toBe(MARK_HIT_SIZE);
|
|
53
|
+
expect(layout.hitX).toBe(50 - MARK_HIT_SIZE / 2);
|
|
54
|
+
expect(layout.hitY).toBe(100 - MARK_HIT_SIZE / 2);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const MARK_HIT_SIZE = 24;
|
|
2
|
+
|
|
3
|
+
export interface BarLayout {
|
|
4
|
+
height: number;
|
|
5
|
+
hitHeight: number;
|
|
6
|
+
hitWidth: number;
|
|
7
|
+
hitX: number;
|
|
8
|
+
hitY: number;
|
|
9
|
+
width: number;
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const getBarLayout = ({
|
|
15
|
+
barWidth,
|
|
16
|
+
baselineY,
|
|
17
|
+
value,
|
|
18
|
+
xCenter,
|
|
19
|
+
y,
|
|
20
|
+
}: {
|
|
21
|
+
barWidth: number;
|
|
22
|
+
baselineY: number;
|
|
23
|
+
value: number;
|
|
24
|
+
xCenter: number;
|
|
25
|
+
y: (value: number) => number;
|
|
26
|
+
}): BarLayout => {
|
|
27
|
+
const valueY = y(value);
|
|
28
|
+
const x = xCenter - barWidth / 2;
|
|
29
|
+
let rectY = baselineY;
|
|
30
|
+
let rectHeight = 0;
|
|
31
|
+
if (value > 0) {
|
|
32
|
+
rectY = valueY;
|
|
33
|
+
rectHeight = Math.max(baselineY - valueY, 0);
|
|
34
|
+
} else if (value < 0) {
|
|
35
|
+
rectY = baselineY;
|
|
36
|
+
rectHeight = Math.max(valueY - baselineY, 0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const hitWidth = Math.max(barWidth, MARK_HIT_SIZE);
|
|
40
|
+
const hitHeight = Math.max(rectHeight, MARK_HIT_SIZE);
|
|
41
|
+
const barMidY = rectHeight === 0 ? baselineY : rectY + rectHeight / 2;
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
height: rectHeight,
|
|
45
|
+
hitHeight,
|
|
46
|
+
hitWidth,
|
|
47
|
+
hitX: xCenter - hitWidth / 2,
|
|
48
|
+
hitY: barMidY - hitHeight / 2,
|
|
49
|
+
width: barWidth,
|
|
50
|
+
x,
|
|
51
|
+
y: rectY,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CHART_Y_AXIS_MAX_WIDTH,
|
|
5
|
+
getChartAxisWidth,
|
|
6
|
+
getChartPlot,
|
|
7
|
+
getDonutSize,
|
|
8
|
+
getPlotHeight,
|
|
9
|
+
getXTickStyle,
|
|
10
|
+
getYTickStyle,
|
|
11
|
+
} from "./layout";
|
|
12
|
+
|
|
13
|
+
describe("chart layout", () => {
|
|
14
|
+
it("takes the tick and tooltip rows out of the requested height", () => {
|
|
15
|
+
expect(getPlotHeight({hasLegend: false, height: 200})).toBe(164);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("also reserves the legend row when the chart has one", () => {
|
|
19
|
+
expect(getPlotHeight({hasLegend: true, height: 200})).toBe(146);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("keeps a drawable plot when the requested height is smaller than the rows", () => {
|
|
23
|
+
expect(getPlotHeight({hasLegend: true, height: 20})).toBe(40);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("fits a donut inside the height left by tooltip and padded, gapped legend rows", () => {
|
|
27
|
+
expect(getDonutSize({chartWidth: 400, height: 220, legendRowCount: 3})).toBe(124);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("keeps a donut square when the container is narrower than the height", () => {
|
|
31
|
+
expect(getDonutSize({chartWidth: 90, height: 220, legendRowCount: 1})).toBe(90);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("caps the value axis gutter on a wide chart", () => {
|
|
35
|
+
expect(getChartAxisWidth(300)).toBe(CHART_Y_AXIS_MAX_WIDTH);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("shrinks the value axis gutter so a narrow chart keeps a plot", () => {
|
|
39
|
+
expect(getChartAxisWidth(100)).toBe(30);
|
|
40
|
+
expect(getChartAxisWidth(0)).toBe(0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("insets the plot inside the measured chart width minus the axis gutter", () => {
|
|
44
|
+
expect(getChartPlot({chartWidth: 300, height: 200})).toEqual({
|
|
45
|
+
height: 184,
|
|
46
|
+
left: 8,
|
|
47
|
+
top: 8,
|
|
48
|
+
width: 244,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("keeps a positive plot box when the container is narrower than the insets", () => {
|
|
53
|
+
const plot = getChartPlot({chartWidth: 0, height: 0});
|
|
54
|
+
|
|
55
|
+
expect(plot.width).toBe(1);
|
|
56
|
+
expect(plot.height).toBe(1);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("centers an x tick slot on its band center", () => {
|
|
60
|
+
expect(getXTickStyle({bandwidth: 60, xCenter: 100})).toEqual({
|
|
61
|
+
left: 70,
|
|
62
|
+
position: "absolute",
|
|
63
|
+
top: 0,
|
|
64
|
+
width: 60,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("keeps an x tick slot positive when there are no bands", () => {
|
|
69
|
+
expect(getXTickStyle({bandwidth: 0, xCenter: 10}).width).toBe(1);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("centers a y tick label on its value and spans the axis gutter", () => {
|
|
73
|
+
expect(getYTickStyle({axisWidth: 40, y: 50})).toEqual({
|
|
74
|
+
left: 0,
|
|
75
|
+
position: "absolute",
|
|
76
|
+
top: 43,
|
|
77
|
+
width: 40,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type {ChartPlot} from "./types/chartTypes";
|
|
2
|
+
|
|
3
|
+
export const CHART_Y_AXIS_MAX_WIDTH = 40;
|
|
4
|
+
export const CHART_X_AXIS_HEIGHT = 18;
|
|
5
|
+
export const CHART_FOOTER_ROW_HEIGHT = 18;
|
|
6
|
+
|
|
7
|
+
const Y_AXIS_WIDTH_RATIO = 0.3;
|
|
8
|
+
const PLOT_INSET = 8;
|
|
9
|
+
const Y_TICK_HALF_HEIGHT = 7;
|
|
10
|
+
const MIN_PLOT_HEIGHT = 40;
|
|
11
|
+
const MIN_DONUT_SIZE = 40;
|
|
12
|
+
const DONUT_LEGEND_PADDING = 8;
|
|
13
|
+
const DONUT_LEGEND_ROW_GAP = 4;
|
|
14
|
+
|
|
15
|
+
export interface ChartTickStyle {
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
left: number;
|
|
18
|
+
position: "absolute";
|
|
19
|
+
top: number;
|
|
20
|
+
width: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Value labels take a share of a narrow chart instead of a fixed gutter, so a chart in a small
|
|
25
|
+
* card keeps a usable plot instead of spending most of its width on the axis.
|
|
26
|
+
*/
|
|
27
|
+
export const getChartAxisWidth = (chartWidth: number): number => {
|
|
28
|
+
if (chartWidth <= 0) {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
return Math.min(CHART_Y_AXIS_MAX_WIDTH, Math.floor(chartWidth * Y_AXIS_WIDTH_RATIO));
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* `height` is the whole chart, not just the drawing, so a chart dropped into a fixed-height slot
|
|
36
|
+
* fits it. The tick row and the tooltip row come out of that budget; the tooltip row is reserved
|
|
37
|
+
* whether or not a tooltip is showing, so hovering never reflows the page.
|
|
38
|
+
*/
|
|
39
|
+
export const getPlotHeight = ({
|
|
40
|
+
hasLegend,
|
|
41
|
+
height,
|
|
42
|
+
}: {
|
|
43
|
+
hasLegend: boolean;
|
|
44
|
+
height: number;
|
|
45
|
+
}): number => {
|
|
46
|
+
const footer = CHART_X_AXIS_HEIGHT + CHART_FOOTER_ROW_HEIGHT * (hasLegend ? 2 : 1);
|
|
47
|
+
return Math.max(height - footer, MIN_PLOT_HEIGHT);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const getDonutSize = ({
|
|
51
|
+
chartWidth,
|
|
52
|
+
height,
|
|
53
|
+
legendRowCount,
|
|
54
|
+
}: {
|
|
55
|
+
chartWidth: number;
|
|
56
|
+
height: number;
|
|
57
|
+
legendRowCount: number;
|
|
58
|
+
}): number => {
|
|
59
|
+
const legendGaps = Math.max(legendRowCount - 1, 0) * DONUT_LEGEND_ROW_GAP;
|
|
60
|
+
const legendHeight =
|
|
61
|
+
legendRowCount * CHART_FOOTER_ROW_HEIGHT + DONUT_LEGEND_PADDING * 2 + legendGaps;
|
|
62
|
+
const footer = CHART_FOOTER_ROW_HEIGHT + legendHeight;
|
|
63
|
+
return Math.max(Math.min(chartWidth, height - footer), MIN_DONUT_SIZE);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const getChartPlot = ({
|
|
67
|
+
chartWidth,
|
|
68
|
+
height,
|
|
69
|
+
}: {
|
|
70
|
+
chartWidth: number;
|
|
71
|
+
height: number;
|
|
72
|
+
}): ChartPlot => {
|
|
73
|
+
return {
|
|
74
|
+
height: Math.max(height - PLOT_INSET * 2, 1),
|
|
75
|
+
left: PLOT_INSET,
|
|
76
|
+
top: PLOT_INSET,
|
|
77
|
+
width: Math.max(chartWidth - getChartAxisWidth(chartWidth) - PLOT_INSET * 2, 1),
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* X tick labels are absolutely positioned on their band center. Laying them out in a flow row
|
|
83
|
+
* instead would let a long label set the chart's min-content width and push the plot past its
|
|
84
|
+
* container.
|
|
85
|
+
*/
|
|
86
|
+
export const getXTickStyle = ({
|
|
87
|
+
bandwidth,
|
|
88
|
+
xCenter,
|
|
89
|
+
}: {
|
|
90
|
+
bandwidth: number;
|
|
91
|
+
xCenter: number;
|
|
92
|
+
}): ChartTickStyle => {
|
|
93
|
+
const width = Math.max(bandwidth, 1);
|
|
94
|
+
return {left: xCenter - width / 2, position: "absolute", top: 0, width};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const getYTickStyle = ({axisWidth, y}: {axisWidth: number; y: number}): ChartTickStyle => {
|
|
98
|
+
return {left: 0, position: "absolute", top: y - Y_TICK_HALF_HEIGHT, width: axisWidth};
|
|
99
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getAreaPath,
|
|
5
|
+
getDonutSliceAngles,
|
|
6
|
+
getDonutSliceHitCenter,
|
|
7
|
+
getDonutSlicePath,
|
|
8
|
+
getLinePath,
|
|
9
|
+
} from "./paths";
|
|
10
|
+
import {createCartesianScales} from "./scales";
|
|
11
|
+
import type {ChartPoint} from "./types/chartTypes";
|
|
12
|
+
|
|
13
|
+
const FIXTURE_POINTS: ChartPoint[] = [
|
|
14
|
+
{label: "A", value: 0},
|
|
15
|
+
{label: "B", value: 50},
|
|
16
|
+
{label: "C", value: 100},
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const PLOT = {height: 100, left: 10, top: 0, width: 90};
|
|
20
|
+
|
|
21
|
+
describe("chart paths", () => {
|
|
22
|
+
it("builds a linear line path through the fixture centers", () => {
|
|
23
|
+
const scales = createCartesianScales({plot: PLOT, points: FIXTURE_POINTS});
|
|
24
|
+
const path = getLinePath({points: FIXTURE_POINTS, scales});
|
|
25
|
+
|
|
26
|
+
expect(path).toBe("M25,100L55,50L85,0");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("builds an area path that includes the plot baseline", () => {
|
|
30
|
+
const scales = createCartesianScales({plot: PLOT, points: FIXTURE_POINTS});
|
|
31
|
+
const path = getAreaPath({baselineY: PLOT.top + PLOT.height, points: FIXTURE_POINTS, scales});
|
|
32
|
+
|
|
33
|
+
expect(path).toContain("M25,100");
|
|
34
|
+
expect(path).toContain("L55,50");
|
|
35
|
+
expect(path).toContain("L85,0");
|
|
36
|
+
expect(path).toContain("L85,100");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("returns empty strings for empty point lists", () => {
|
|
40
|
+
const scales = createCartesianScales({plot: PLOT, points: []});
|
|
41
|
+
|
|
42
|
+
expect(getLinePath({points: [], scales})).toBe("");
|
|
43
|
+
expect(getAreaPath({baselineY: 100, points: [], scales})).toBe("");
|
|
44
|
+
expect(getDonutSlicePath({endAngle: 0, innerRadius: 10, outerRadius: 20, startAngle: 0})).toBe(
|
|
45
|
+
""
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("builds a nonempty donut slice for a half-circle", () => {
|
|
50
|
+
const path = getDonutSlicePath({
|
|
51
|
+
endAngle: Math.PI,
|
|
52
|
+
innerRadius: 20,
|
|
53
|
+
outerRadius: 40,
|
|
54
|
+
startAngle: 0,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
expect(path.startsWith("M")).toBe(true);
|
|
58
|
+
expect(path.length).toBeGreaterThan(10);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("starts donut slices at d3-shape 0 (12 o'clock), not -π/2", () => {
|
|
62
|
+
const [first, second] = getDonutSliceAngles([{value: 50}, {value: 50}]);
|
|
63
|
+
|
|
64
|
+
expect(first?.start).toBe(0);
|
|
65
|
+
expect(first?.end).toBe(Math.PI);
|
|
66
|
+
expect(second?.start).toBe(Math.PI);
|
|
67
|
+
expect(second?.end).toBe(Math.PI * 2);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("places equal-slice hit centers on the same side as d3-shape paint", () => {
|
|
71
|
+
const firstMid = Math.PI / 2;
|
|
72
|
+
const secondMid = (3 * Math.PI) / 2;
|
|
73
|
+
const first = getDonutSliceHitCenter({center: 100, hitRadius: 40, midAngle: firstMid});
|
|
74
|
+
const second = getDonutSliceHitCenter({center: 100, hitRadius: 40, midAngle: secondMid});
|
|
75
|
+
|
|
76
|
+
expect(first).toEqual({x: 140, y: 100});
|
|
77
|
+
expect(second).toEqual({x: 60, y: 100});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {arc, area, line} from "d3-shape";
|
|
2
|
+
|
|
3
|
+
import type {ChartPoint, ChartScales} from "./types/chartTypes";
|
|
4
|
+
|
|
5
|
+
export const getLinePath = ({
|
|
6
|
+
points,
|
|
7
|
+
scales,
|
|
8
|
+
}: {
|
|
9
|
+
points: ChartPoint[];
|
|
10
|
+
scales: ChartScales;
|
|
11
|
+
}): string => {
|
|
12
|
+
if (points.length === 0) {
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const path = line<ChartPoint>()
|
|
17
|
+
.x((point) => scales.xCenter(point.label))
|
|
18
|
+
.y((point) => scales.y(point.value))(points);
|
|
19
|
+
|
|
20
|
+
return path ?? "";
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getAreaPath = ({
|
|
24
|
+
baselineY,
|
|
25
|
+
points,
|
|
26
|
+
scales,
|
|
27
|
+
}: {
|
|
28
|
+
baselineY: number;
|
|
29
|
+
points: ChartPoint[];
|
|
30
|
+
scales: ChartScales;
|
|
31
|
+
}): string => {
|
|
32
|
+
if (points.length === 0) {
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const path = area<ChartPoint>()
|
|
37
|
+
.x((point) => scales.xCenter(point.label))
|
|
38
|
+
.y0(baselineY)
|
|
39
|
+
.y1((point) => scales.y(point.value))(points);
|
|
40
|
+
|
|
41
|
+
return path ?? "";
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const getDonutSlicePath = ({
|
|
45
|
+
endAngle,
|
|
46
|
+
innerRadius,
|
|
47
|
+
outerRadius,
|
|
48
|
+
startAngle,
|
|
49
|
+
}: {
|
|
50
|
+
endAngle: number;
|
|
51
|
+
innerRadius: number;
|
|
52
|
+
outerRadius: number;
|
|
53
|
+
startAngle: number;
|
|
54
|
+
}): string => {
|
|
55
|
+
if (startAngle === endAngle || outerRadius <= 0) {
|
|
56
|
+
return "";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const path = arc()({
|
|
60
|
+
endAngle,
|
|
61
|
+
innerRadius,
|
|
62
|
+
outerRadius,
|
|
63
|
+
startAngle,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return path ?? "";
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const TAU = Math.PI * 2;
|
|
70
|
+
|
|
71
|
+
export const getDonutSliceAngles = (
|
|
72
|
+
data: {value: number}[]
|
|
73
|
+
): Array<{end: number; start: number}> => {
|
|
74
|
+
const total = data.reduce((sum, point) => sum + Math.max(point.value, 0), 0);
|
|
75
|
+
let cursor = 0;
|
|
76
|
+
|
|
77
|
+
return data.map((point) => {
|
|
78
|
+
const sweep = total === 0 ? 0 : (Math.max(point.value, 0) / total) * TAU;
|
|
79
|
+
const start = cursor;
|
|
80
|
+
cursor += sweep;
|
|
81
|
+
return {end: cursor, start};
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** d3-shape `arc` treats 0 as 12 o'clock by subtracting π/2 before cos/sin. */
|
|
86
|
+
export const getDonutSliceHitCenter = ({
|
|
87
|
+
center,
|
|
88
|
+
hitRadius,
|
|
89
|
+
midAngle,
|
|
90
|
+
}: {
|
|
91
|
+
center: number;
|
|
92
|
+
hitRadius: number;
|
|
93
|
+
midAngle: number;
|
|
94
|
+
}): {x: number; y: number} => {
|
|
95
|
+
const paintAngle = midAngle - Math.PI / 2;
|
|
96
|
+
return {
|
|
97
|
+
x: center + Math.cos(paintAngle) * hitRadius,
|
|
98
|
+
y: center + Math.sin(paintAngle) * hitRadius,
|
|
99
|
+
};
|
|
100
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {createCartesianScales, getYTickValues} from "./scales";
|
|
4
|
+
import type {ChartPoint} from "./types/chartTypes";
|
|
5
|
+
|
|
6
|
+
const FIXTURE_POINTS: ChartPoint[] = [
|
|
7
|
+
{label: "A", value: 0},
|
|
8
|
+
{label: "B", value: 50},
|
|
9
|
+
{label: "C", value: 100},
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
const PLOT = {height: 100, left: 10, top: 0, width: 90};
|
|
13
|
+
|
|
14
|
+
describe("createCartesianScales", () => {
|
|
15
|
+
it("maps equal bands and inverted y for a 90x100 plot", () => {
|
|
16
|
+
const scales = createCartesianScales({plot: PLOT, points: FIXTURE_POINTS});
|
|
17
|
+
|
|
18
|
+
expect(scales.bandwidth).toBe(30);
|
|
19
|
+
expect(scales.xStart("A")).toBe(10);
|
|
20
|
+
expect(scales.xStart("B")).toBe(40);
|
|
21
|
+
expect(scales.xStart("C")).toBe(70);
|
|
22
|
+
expect(scales.xCenter("A")).toBe(25);
|
|
23
|
+
expect(scales.xCenter("B")).toBe(55);
|
|
24
|
+
expect(scales.xCenter("C")).toBe(85);
|
|
25
|
+
expect(scales.y(0)).toBe(100);
|
|
26
|
+
expect(scales.y(100)).toBe(0);
|
|
27
|
+
expect(scales.y(50)).toBe(50);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("returns three ticks from the y domain including zero", () => {
|
|
31
|
+
expect(getYTickValues(FIXTURE_POINTS)).toEqual([0, 50, 100]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("anchors all-negative values at zero so bars can grow downward", () => {
|
|
35
|
+
const points: ChartPoint[] = [
|
|
36
|
+
{label: "A", value: -10},
|
|
37
|
+
{label: "B", value: -5},
|
|
38
|
+
];
|
|
39
|
+
const scales = createCartesianScales({plot: PLOT, points});
|
|
40
|
+
|
|
41
|
+
expect(getYTickValues(points)).toEqual([-10, -5, 0]);
|
|
42
|
+
expect(scales.y(0)).toBe(PLOT.top);
|
|
43
|
+
expect(scales.y(-10)).toBe(PLOT.top + PLOT.height);
|
|
44
|
+
expect(scales.y(-5)).toBe(PLOT.top + PLOT.height / 2);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns no ticks for empty points", () => {
|
|
48
|
+
expect(getYTickValues([])).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("does not throw on empty points", () => {
|
|
52
|
+
const scales = createCartesianScales({plot: PLOT, points: []});
|
|
53
|
+
|
|
54
|
+
expect(scales.bandwidth).toBe(0);
|
|
55
|
+
expect(scales.xStart("A")).toBe(PLOT.left);
|
|
56
|
+
expect(scales.xCenter("A")).toBe(PLOT.left);
|
|
57
|
+
expect(scales.y(0)).toBe(PLOT.top + PLOT.height);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {scaleBand, scaleLinear} from "d3-scale";
|
|
2
|
+
|
|
3
|
+
import type {ChartPlot, ChartPoint, ChartScales} from "./types/chartTypes";
|
|
4
|
+
|
|
5
|
+
const EMPTY_Y_DOMAIN: [number, number] = [0, 1];
|
|
6
|
+
|
|
7
|
+
const getYDomain = (points: ChartPoint[]): [number, number] => {
|
|
8
|
+
if (points.length === 0) {
|
|
9
|
+
return EMPTY_Y_DOMAIN;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const values = points.map((point) => point.value);
|
|
13
|
+
const min = Math.min(0, ...values);
|
|
14
|
+
const max = Math.max(0, ...values);
|
|
15
|
+
if (min === max) {
|
|
16
|
+
return [min, min + 1];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return [min, max];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const getYTickValues = (points: ChartPoint[]): number[] => {
|
|
23
|
+
if (points.length === 0) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const [min, max] = getYDomain(points);
|
|
28
|
+
const mid = min + (max - min) / 2;
|
|
29
|
+
return [min, mid, max];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const createCartesianScales = ({
|
|
33
|
+
plot,
|
|
34
|
+
points,
|
|
35
|
+
}: {
|
|
36
|
+
plot: ChartPlot;
|
|
37
|
+
points: ChartPoint[];
|
|
38
|
+
}): ChartScales => {
|
|
39
|
+
const bottom = plot.top + plot.height;
|
|
40
|
+
const labels = points.map((point) => point.label);
|
|
41
|
+
const xScale = scaleBand<string>()
|
|
42
|
+
.domain(labels)
|
|
43
|
+
.range([plot.left, plot.left + plot.width]);
|
|
44
|
+
const yScale = scaleLinear().domain(getYDomain(points)).range([bottom, plot.top]);
|
|
45
|
+
const bandwidth = labels.length === 0 ? 0 : xScale.bandwidth();
|
|
46
|
+
|
|
47
|
+
const xStart = (label: string): number => {
|
|
48
|
+
return xScale(label) ?? plot.left;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const xCenter = (label: string): number => {
|
|
52
|
+
return xStart(label) + bandwidth / 2;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const y = (value: number): number => {
|
|
56
|
+
return yScale(value);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return {bandwidth, xCenter, xStart, y};
|
|
60
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import type {TerrenoTheme} from "../Common";
|
|
4
|
+
import {getChartPaint} from "./theme";
|
|
5
|
+
|
|
6
|
+
const THEME = {
|
|
7
|
+
border: {default: "#CDCDCD"},
|
|
8
|
+
surface: {error: "#BD1111", primary: "#0E9DCD", secondaryDark: "#2B6072"},
|
|
9
|
+
text: {accent: "#956A00", secondaryDark: "#543C00"},
|
|
10
|
+
} as unknown as TerrenoTheme;
|
|
11
|
+
|
|
12
|
+
describe("getChartPaint", () => {
|
|
13
|
+
it("maps theme tokens onto series, slices, grid, and axis paints", () => {
|
|
14
|
+
const paint = getChartPaint(THEME);
|
|
15
|
+
|
|
16
|
+
expect(paint.series).toBe("#0E9DCD");
|
|
17
|
+
expect(paint.seriesFill).toBe("#0E9DCD");
|
|
18
|
+
expect(paint.slices).toEqual(["#0E9DCD", "#2B6072", "#956A00", "#BD1111"]);
|
|
19
|
+
expect(paint.grid).toBe("#CDCDCD");
|
|
20
|
+
expect(paint.axis).toBe("#543C00");
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type {TerrenoTheme} from "../Common";
|
|
2
|
+
|
|
3
|
+
export interface ChartPaint {
|
|
4
|
+
axis: string;
|
|
5
|
+
grid: string;
|
|
6
|
+
series: string;
|
|
7
|
+
seriesFill: string;
|
|
8
|
+
slices: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const getChartPaint = (theme: TerrenoTheme): ChartPaint => {
|
|
12
|
+
return {
|
|
13
|
+
axis: theme.text.secondaryDark,
|
|
14
|
+
grid: theme.border.default,
|
|
15
|
+
series: theme.surface.primary,
|
|
16
|
+
seriesFill: theme.surface.primary,
|
|
17
|
+
slices: [
|
|
18
|
+
theme.surface.primary,
|
|
19
|
+
theme.surface.secondaryDark,
|
|
20
|
+
theme.text.accent,
|
|
21
|
+
theme.surface.error,
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
};
|