@nuvui/charts 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +75 -2
- package/dist/components/chart/chart-container.cjs +62 -0
- package/dist/components/chart/chart-container.d.cts +44 -0
- package/dist/components/chart/chart-container.d.ts +44 -0
- package/dist/components/chart/chart-container.js +62 -0
- package/dist/components/chart/chart-legend.cjs +55 -0
- package/dist/components/chart/chart-legend.d.cts +29 -0
- package/dist/components/chart/chart-legend.d.ts +29 -0
- package/dist/components/chart/chart-legend.js +54 -0
- package/dist/components/chart/chart-table.cjs +42 -0
- package/dist/components/chart/chart-table.d.cts +41 -0
- package/dist/components/chart/chart-table.d.ts +41 -0
- package/dist/components/chart/chart-table.js +42 -0
- package/dist/components/chart/chart-tooltip.cjs +99 -0
- package/dist/components/chart/chart-tooltip.d.cts +41 -0
- package/dist/components/chart/chart-tooltip.d.ts +41 -0
- package/dist/components/chart/chart-tooltip.js +97 -0
- package/dist/components/chart/config.cjs +81 -0
- package/dist/components/chart/config.d.cts +45 -0
- package/dist/components/chart/config.d.ts +45 -0
- package/dist/components/chart/config.js +75 -0
- package/dist/components/chart/context.cjs +20 -0
- package/dist/components/chart/context.d.cts +6 -0
- package/dist/components/chart/context.d.ts +6 -0
- package/dist/components/chart/context.js +18 -0
- package/dist/components/chart/index.cjs +15 -0
- package/dist/components/chart/index.d.cts +8 -0
- package/dist/components/chart/index.d.ts +8 -0
- package/dist/components/chart/index.js +6 -0
- package/dist/components/chart/patterns.cjs +81 -0
- package/dist/components/chart/patterns.js +81 -0
- package/dist/components/chart/swatch.cjs +34 -0
- package/dist/components/chart/swatch.d.cts +7 -0
- package/dist/components/chart/swatch.d.ts +7 -0
- package/dist/components/chart/swatch.js +34 -0
- package/dist/css/chart.css +199 -0
- package/dist/index.cjs +15 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +6 -0
- package/dist/styles.css +199 -0
- package/dist/utils/cx.cjs +6 -0
- package/dist/utils/cx.js +6 -0
- package/package.json +96 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nuvraxis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nuvui/charts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Charts for [`@nuvui/react`](https://github.com/Nuvraxis/nuvui/tree/main/packages/ui): a container, a tooltip, a legend and a data table for charts built from [Recharts](https://recharts.github.io) 3's own parts.
|
|
4
|
+
|
|
5
|
+
This is early, and the package hasn't been released. See the [repository README](https://github.com/Nuvraxis/nuvui#readme) for where things stand.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @nuvui/charts @nuvui/react recharts
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
"use client";
|
|
13
|
+
|
|
14
|
+
import "@nuvui/react/styles.css";
|
|
15
|
+
import "@nuvui/charts/styles.css";
|
|
16
|
+
import {
|
|
17
|
+
type ChartConfig,
|
|
18
|
+
ChartContainer,
|
|
19
|
+
ChartLegend,
|
|
20
|
+
ChartTable,
|
|
21
|
+
ChartTooltip,
|
|
22
|
+
chartFill,
|
|
23
|
+
} from "@nuvui/charts";
|
|
24
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from "recharts";
|
|
25
|
+
|
|
26
|
+
type Month = { month: string; desktop: number; mobile: number };
|
|
27
|
+
|
|
28
|
+
const config = {
|
|
29
|
+
desktop: { label: "Desktop" },
|
|
30
|
+
mobile: { label: "Mobile" },
|
|
31
|
+
} satisfies ChartConfig;
|
|
32
|
+
|
|
33
|
+
export function Visitors({ months }: { months: Month[] }) {
|
|
34
|
+
return (
|
|
35
|
+
<ChartContainer
|
|
36
|
+
config={config}
|
|
37
|
+
aria-label="Visitors by month"
|
|
38
|
+
table={<ChartTable data={months} category="month" categoryLabel="Month" />}
|
|
39
|
+
>
|
|
40
|
+
<BarChart data={months}>
|
|
41
|
+
<CartesianGrid vertical={false} />
|
|
42
|
+
<XAxis dataKey="month" />
|
|
43
|
+
<ChartTooltip />
|
|
44
|
+
<ChartLegend />
|
|
45
|
+
<Bar dataKey="desktop" fill={chartFill("desktop")} />
|
|
46
|
+
<Bar dataKey="mobile" fill={chartFill("mobile")} />
|
|
47
|
+
</BarChart>
|
|
48
|
+
</ChartContainer>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## What's in it
|
|
54
|
+
|
|
55
|
+
- `ChartContainer`: sizes the chart, names it, and sets a color, a fill and a dash for each series as CSS variables. A series with no color of its own takes the next of the theme's eight chart colors. Recharts' axes, grid and labels are styled from the tokens.
|
|
56
|
+
- `chartColor`, `chartFill` and `chartDash`: the text of those variables, for Recharts' `stroke`, `fill` and `strokeDasharray`.
|
|
57
|
+
- `ChartTooltip` and `ChartLegend`: Recharts' tooltip and legend, drawn like the rest of the library, with the labels from the config.
|
|
58
|
+
- `ChartTable`: the chart's numbers as a table, for screen readers only or drawn under the chart.
|
|
59
|
+
|
|
60
|
+
Recharts does the drawing: the scales, the shapes, the axes, the animation and the keyboard. Nothing of it is wrapped or renamed.
|
|
61
|
+
|
|
62
|
+
A color is a CSS variable, so switching the theme recolors a chart without drawing it again. With `patterns`, each series also gets a fill and a dash of its own, so series can be told apart without color. Where the browser forces colors, that happens by itself.
|
|
63
|
+
|
|
64
|
+
## Peer dependencies
|
|
65
|
+
|
|
66
|
+
`@nuvui/react` and `recharts` 3 are peer dependencies. You import the chart's parts from `recharts` yourself, so your app owns that version. Recharts asks for `react-is` as a peer of its own.
|
|
67
|
+
|
|
68
|
+
A chart is a client component. Put `"use client"` at the top of the file it's written in.
|
|
69
|
+
|
|
70
|
+
It's a package of its own so that an app which only wants buttons and dialogs doesn't have to install a charting library.
|
|
71
|
+
|
|
72
|
+
Docs will live at https://nuvui.nuvraxis.com.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_cx = require("../../utils/cx.cjs");
|
|
3
|
+
const require_config = require("./config.cjs");
|
|
4
|
+
const require_context = require("./context.cjs");
|
|
5
|
+
const require_patterns = require("./patterns.cjs");
|
|
6
|
+
let react = require("react");
|
|
7
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
+
//#region src/components/chart/chart-container.tsx
|
|
9
|
+
const fill = {
|
|
10
|
+
width: "100%",
|
|
11
|
+
height: "100%"
|
|
12
|
+
};
|
|
13
|
+
const ChartContainer = (0, react.forwardRef)(function ChartContainer({ config, children, table, patterns = false, formatValue, className, style, "aria-label": label, "aria-labelledby": labelledBy, ...props }, ref) {
|
|
14
|
+
const id = `nuv-chart-${(0, react.useId)().replace(/[^a-zA-Z0-9_-]/g, "")}`;
|
|
15
|
+
const series = (0, react.useMemo)(() => require_config.resolveSeries(config, id), [config, id]);
|
|
16
|
+
const variables = (0, react.useMemo)(() => Object.assign({}, ...series.map((entry) => entry.variables)), [series]);
|
|
17
|
+
const context = (0, react.useMemo)(() => ({
|
|
18
|
+
config,
|
|
19
|
+
formatValue,
|
|
20
|
+
name: {
|
|
21
|
+
"aria-label": label,
|
|
22
|
+
"aria-labelledby": labelledBy
|
|
23
|
+
}
|
|
24
|
+
}), [
|
|
25
|
+
config,
|
|
26
|
+
formatValue,
|
|
27
|
+
label,
|
|
28
|
+
labelledBy
|
|
29
|
+
]);
|
|
30
|
+
const chart = children;
|
|
31
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_context.ChartContext.Provider, {
|
|
32
|
+
value: context,
|
|
33
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
34
|
+
ref,
|
|
35
|
+
className: require_cx.cx("nuv-chart", className),
|
|
36
|
+
"data-patterns": patterns ? "" : void 0,
|
|
37
|
+
style: {
|
|
38
|
+
...variables,
|
|
39
|
+
...style
|
|
40
|
+
},
|
|
41
|
+
...props,
|
|
42
|
+
children: [
|
|
43
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_patterns.Patterns, { series }),
|
|
44
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
45
|
+
className: "nuv-chart__plot",
|
|
46
|
+
children: (0, react.cloneElement)(chart, {
|
|
47
|
+
responsive: chart.props.responsive ?? true,
|
|
48
|
+
style: {
|
|
49
|
+
...fill,
|
|
50
|
+
...chart.props.style
|
|
51
|
+
},
|
|
52
|
+
"aria-label": chart.props["aria-label"] ?? label,
|
|
53
|
+
"aria-labelledby": chart.props["aria-labelledby"] ?? labelledBy
|
|
54
|
+
})
|
|
55
|
+
}),
|
|
56
|
+
table
|
|
57
|
+
]
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
//#endregion
|
|
62
|
+
exports.ChartContainer = ChartContainer;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ChartConfig } from "./config.cjs";
|
|
2
|
+
import { ChartValueFormatter } from "./context.cjs";
|
|
3
|
+
import { HTMLAttributes, ReactElement, ReactNode } from "react";
|
|
4
|
+
//#region src/components/chart/chart-container.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* What the chart is called. One of the two is required: a chart is a
|
|
7
|
+
* picture, and the name is what a screen reader has to say for it.
|
|
8
|
+
*/
|
|
9
|
+
export type ChartName = {
|
|
10
|
+
"aria-label": string;
|
|
11
|
+
"aria-labelledby"?: string;
|
|
12
|
+
} | {
|
|
13
|
+
"aria-label"?: string;
|
|
14
|
+
"aria-labelledby": string;
|
|
15
|
+
};
|
|
16
|
+
export interface ChartContainerOwnProps {
|
|
17
|
+
/** The series of the chart: a label, and optionally a color, for each. */
|
|
18
|
+
config: ChartConfig;
|
|
19
|
+
/**
|
|
20
|
+
* One Recharts chart, such as `<BarChart>`. It's sized to the container
|
|
21
|
+
* and given the container's name.
|
|
22
|
+
*/
|
|
23
|
+
children: ReactElement;
|
|
24
|
+
/**
|
|
25
|
+
* The same numbers as a table, usually a `ChartTable`. It's drawn after
|
|
26
|
+
* the chart.
|
|
27
|
+
*/
|
|
28
|
+
table?: ReactNode;
|
|
29
|
+
/**
|
|
30
|
+
* Fills each series with a pattern of its own and dashes its line, so
|
|
31
|
+
* that series can be told apart without color. Where the browser is
|
|
32
|
+
* forcing colors this happens whatever is set here.
|
|
33
|
+
* @default false
|
|
34
|
+
*/
|
|
35
|
+
patterns?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* How a value is written in the tooltip and the table. Left out, a value
|
|
38
|
+
* is shown as it is in the data.
|
|
39
|
+
*/
|
|
40
|
+
formatValue?: ChartValueFormatter;
|
|
41
|
+
}
|
|
42
|
+
export type ChartContainerProps = ChartContainerOwnProps & ChartName & Omit<HTMLAttributes<HTMLDivElement>, keyof ChartContainerOwnProps | "aria-label" | "aria-labelledby">;
|
|
43
|
+
export declare const ChartContainer: import("react").ForwardRefExoticComponent<ChartContainerProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
44
|
+
//#endregion
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ChartConfig } from "./config.js";
|
|
2
|
+
import { ChartValueFormatter } from "./context.js";
|
|
3
|
+
import { HTMLAttributes, ReactElement, ReactNode } from "react";
|
|
4
|
+
//#region src/components/chart/chart-container.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* What the chart is called. One of the two is required: a chart is a
|
|
7
|
+
* picture, and the name is what a screen reader has to say for it.
|
|
8
|
+
*/
|
|
9
|
+
export type ChartName = {
|
|
10
|
+
"aria-label": string;
|
|
11
|
+
"aria-labelledby"?: string;
|
|
12
|
+
} | {
|
|
13
|
+
"aria-label"?: string;
|
|
14
|
+
"aria-labelledby": string;
|
|
15
|
+
};
|
|
16
|
+
export interface ChartContainerOwnProps {
|
|
17
|
+
/** The series of the chart: a label, and optionally a color, for each. */
|
|
18
|
+
config: ChartConfig;
|
|
19
|
+
/**
|
|
20
|
+
* One Recharts chart, such as `<BarChart>`. It's sized to the container
|
|
21
|
+
* and given the container's name.
|
|
22
|
+
*/
|
|
23
|
+
children: ReactElement;
|
|
24
|
+
/**
|
|
25
|
+
* The same numbers as a table, usually a `ChartTable`. It's drawn after
|
|
26
|
+
* the chart.
|
|
27
|
+
*/
|
|
28
|
+
table?: ReactNode;
|
|
29
|
+
/**
|
|
30
|
+
* Fills each series with a pattern of its own and dashes its line, so
|
|
31
|
+
* that series can be told apart without color. Where the browser is
|
|
32
|
+
* forcing colors this happens whatever is set here.
|
|
33
|
+
* @default false
|
|
34
|
+
*/
|
|
35
|
+
patterns?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* How a value is written in the tooltip and the table. Left out, a value
|
|
38
|
+
* is shown as it is in the data.
|
|
39
|
+
*/
|
|
40
|
+
formatValue?: ChartValueFormatter;
|
|
41
|
+
}
|
|
42
|
+
export type ChartContainerProps = ChartContainerOwnProps & ChartName & Omit<HTMLAttributes<HTMLDivElement>, keyof ChartContainerOwnProps | "aria-label" | "aria-labelledby">;
|
|
43
|
+
export declare const ChartContainer: import("react").ForwardRefExoticComponent<ChartContainerProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
44
|
+
//#endregion
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cx } from "../../utils/cx.js";
|
|
3
|
+
import { resolveSeries } from "./config.js";
|
|
4
|
+
import { ChartContext } from "./context.js";
|
|
5
|
+
import { Patterns } from "./patterns.js";
|
|
6
|
+
import { cloneElement, forwardRef, useId, useMemo } from "react";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
//#region src/components/chart/chart-container.tsx
|
|
9
|
+
const fill = {
|
|
10
|
+
width: "100%",
|
|
11
|
+
height: "100%"
|
|
12
|
+
};
|
|
13
|
+
const ChartContainer = forwardRef(function ChartContainer({ config, children, table, patterns = false, formatValue, className, style, "aria-label": label, "aria-labelledby": labelledBy, ...props }, ref) {
|
|
14
|
+
const id = `nuv-chart-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
|
|
15
|
+
const series = useMemo(() => resolveSeries(config, id), [config, id]);
|
|
16
|
+
const variables = useMemo(() => Object.assign({}, ...series.map((entry) => entry.variables)), [series]);
|
|
17
|
+
const context = useMemo(() => ({
|
|
18
|
+
config,
|
|
19
|
+
formatValue,
|
|
20
|
+
name: {
|
|
21
|
+
"aria-label": label,
|
|
22
|
+
"aria-labelledby": labelledBy
|
|
23
|
+
}
|
|
24
|
+
}), [
|
|
25
|
+
config,
|
|
26
|
+
formatValue,
|
|
27
|
+
label,
|
|
28
|
+
labelledBy
|
|
29
|
+
]);
|
|
30
|
+
const chart = children;
|
|
31
|
+
return /* @__PURE__ */ jsx(ChartContext.Provider, {
|
|
32
|
+
value: context,
|
|
33
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
34
|
+
ref,
|
|
35
|
+
className: cx("nuv-chart", className),
|
|
36
|
+
"data-patterns": patterns ? "" : void 0,
|
|
37
|
+
style: {
|
|
38
|
+
...variables,
|
|
39
|
+
...style
|
|
40
|
+
},
|
|
41
|
+
...props,
|
|
42
|
+
children: [
|
|
43
|
+
/* @__PURE__ */ jsx(Patterns, { series }),
|
|
44
|
+
/* @__PURE__ */ jsx("div", {
|
|
45
|
+
className: "nuv-chart__plot",
|
|
46
|
+
children: cloneElement(chart, {
|
|
47
|
+
responsive: chart.props.responsive ?? true,
|
|
48
|
+
style: {
|
|
49
|
+
...fill,
|
|
50
|
+
...chart.props.style
|
|
51
|
+
},
|
|
52
|
+
"aria-label": chart.props["aria-label"] ?? label,
|
|
53
|
+
"aria-labelledby": chart.props["aria-labelledby"] ?? labelledBy
|
|
54
|
+
})
|
|
55
|
+
}),
|
|
56
|
+
table
|
|
57
|
+
]
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
//#endregion
|
|
62
|
+
export { ChartContainer };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_cx = require("../../utils/cx.cjs");
|
|
3
|
+
const require_context = require("./context.cjs");
|
|
4
|
+
const require_swatch = require("./swatch.cjs");
|
|
5
|
+
const require_chart_tooltip = require("./chart-tooltip.cjs");
|
|
6
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
|
+
let recharts = require("recharts");
|
|
8
|
+
//#region src/components/chart/chart-legend.tsx
|
|
9
|
+
/**
|
|
10
|
+
* What's inside a chart's legend. `ChartLegend` draws it by itself. Use it
|
|
11
|
+
* directly to pass Recharts' `Legend` a `content` of your own making.
|
|
12
|
+
*/
|
|
13
|
+
function ChartLegendContent({ payload, indicator = "square", nameKey, className }) {
|
|
14
|
+
const { config } = require_context.useChart("ChartLegendContent");
|
|
15
|
+
const keys = Object.keys(config);
|
|
16
|
+
const items = (payload ?? []).filter((item) => item.type !== "none").map((item) => {
|
|
17
|
+
const series = require_chart_tooltip.seriesOf(config, item, nameKey, "legend");
|
|
18
|
+
return {
|
|
19
|
+
item,
|
|
20
|
+
series,
|
|
21
|
+
place: series ? keys.indexOf(series) : keys.length
|
|
22
|
+
};
|
|
23
|
+
}).sort((a, b) => a.place - b.place);
|
|
24
|
+
if (items.length === 0) return null;
|
|
25
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
|
|
26
|
+
className: require_cx.cx("nuv-chart__legend", className),
|
|
27
|
+
children: items.map(({ item, series }, index) => {
|
|
28
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
|
|
29
|
+
className: require_cx.cx("nuv-chart__legend-item", item.inactive && "nuv-chart__legend-item--inactive"),
|
|
30
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_swatch.Swatch, {
|
|
31
|
+
series,
|
|
32
|
+
color: item.color,
|
|
33
|
+
indicator
|
|
34
|
+
}), (series && config[series]?.label) ?? item.value]
|
|
35
|
+
}, `${index}-${String(item.dataKey)}-${String(item.value)}`);
|
|
36
|
+
})
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Recharts' `Legend`, drawn like the rest of the library. It takes
|
|
41
|
+
* everything `Legend` does.
|
|
42
|
+
*/
|
|
43
|
+
function ChartLegend({ indicator, nameKey, className, content, ...props }) {
|
|
44
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(recharts.Legend, {
|
|
45
|
+
content: content ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ChartLegendContent, {
|
|
46
|
+
indicator,
|
|
47
|
+
nameKey,
|
|
48
|
+
className
|
|
49
|
+
}),
|
|
50
|
+
...props
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
exports.ChartLegend = ChartLegend;
|
|
55
|
+
exports.ChartLegendContent = ChartLegendContent;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ChartIndicator } from "./swatch.cjs";
|
|
2
|
+
import { DefaultLegendContentProps, LegendProps } from "recharts";
|
|
3
|
+
//#region src/components/chart/chart-legend.d.ts
|
|
4
|
+
export interface ChartLegendContentOwnProps {
|
|
5
|
+
/**
|
|
6
|
+
* The sample drawn next to each series.
|
|
7
|
+
* @default "square"
|
|
8
|
+
*/
|
|
9
|
+
indicator?: ChartIndicator;
|
|
10
|
+
/**
|
|
11
|
+
* The field of a data row that holds the series' key. Only needed when
|
|
12
|
+
* neither the series' name nor its data key is a key of the config.
|
|
13
|
+
*/
|
|
14
|
+
nameKey?: string;
|
|
15
|
+
className?: string;
|
|
16
|
+
}
|
|
17
|
+
export type ChartLegendContentProps = ChartLegendContentOwnProps & Pick<DefaultLegendContentProps, "payload">;
|
|
18
|
+
/**
|
|
19
|
+
* What's inside a chart's legend. `ChartLegend` draws it by itself. Use it
|
|
20
|
+
* directly to pass Recharts' `Legend` a `content` of your own making.
|
|
21
|
+
*/
|
|
22
|
+
export declare function ChartLegendContent({ payload, indicator, nameKey, className }: ChartLegendContentProps): import("react").JSX.Element | null;
|
|
23
|
+
export type ChartLegendProps = ChartLegendContentOwnProps & LegendProps;
|
|
24
|
+
/**
|
|
25
|
+
* Recharts' `Legend`, drawn like the rest of the library. It takes
|
|
26
|
+
* everything `Legend` does.
|
|
27
|
+
*/
|
|
28
|
+
export declare function ChartLegend({ indicator, nameKey, className, content, ...props }: ChartLegendProps): import("react").JSX.Element;
|
|
29
|
+
//#endregion
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ChartIndicator } from "./swatch.js";
|
|
2
|
+
import { DefaultLegendContentProps, LegendProps } from "recharts";
|
|
3
|
+
//#region src/components/chart/chart-legend.d.ts
|
|
4
|
+
export interface ChartLegendContentOwnProps {
|
|
5
|
+
/**
|
|
6
|
+
* The sample drawn next to each series.
|
|
7
|
+
* @default "square"
|
|
8
|
+
*/
|
|
9
|
+
indicator?: ChartIndicator;
|
|
10
|
+
/**
|
|
11
|
+
* The field of a data row that holds the series' key. Only needed when
|
|
12
|
+
* neither the series' name nor its data key is a key of the config.
|
|
13
|
+
*/
|
|
14
|
+
nameKey?: string;
|
|
15
|
+
className?: string;
|
|
16
|
+
}
|
|
17
|
+
export type ChartLegendContentProps = ChartLegendContentOwnProps & Pick<DefaultLegendContentProps, "payload">;
|
|
18
|
+
/**
|
|
19
|
+
* What's inside a chart's legend. `ChartLegend` draws it by itself. Use it
|
|
20
|
+
* directly to pass Recharts' `Legend` a `content` of your own making.
|
|
21
|
+
*/
|
|
22
|
+
export declare function ChartLegendContent({ payload, indicator, nameKey, className }: ChartLegendContentProps): import("react").JSX.Element | null;
|
|
23
|
+
export type ChartLegendProps = ChartLegendContentOwnProps & LegendProps;
|
|
24
|
+
/**
|
|
25
|
+
* Recharts' `Legend`, drawn like the rest of the library. It takes
|
|
26
|
+
* everything `Legend` does.
|
|
27
|
+
*/
|
|
28
|
+
export declare function ChartLegend({ indicator, nameKey, className, content, ...props }: ChartLegendProps): import("react").JSX.Element;
|
|
29
|
+
//#endregion
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cx } from "../../utils/cx.js";
|
|
3
|
+
import { useChart } from "./context.js";
|
|
4
|
+
import { Swatch } from "./swatch.js";
|
|
5
|
+
import { seriesOf } from "./chart-tooltip.js";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { Legend } from "recharts";
|
|
8
|
+
//#region src/components/chart/chart-legend.tsx
|
|
9
|
+
/**
|
|
10
|
+
* What's inside a chart's legend. `ChartLegend` draws it by itself. Use it
|
|
11
|
+
* directly to pass Recharts' `Legend` a `content` of your own making.
|
|
12
|
+
*/
|
|
13
|
+
function ChartLegendContent({ payload, indicator = "square", nameKey, className }) {
|
|
14
|
+
const { config } = useChart("ChartLegendContent");
|
|
15
|
+
const keys = Object.keys(config);
|
|
16
|
+
const items = (payload ?? []).filter((item) => item.type !== "none").map((item) => {
|
|
17
|
+
const series = seriesOf(config, item, nameKey, "legend");
|
|
18
|
+
return {
|
|
19
|
+
item,
|
|
20
|
+
series,
|
|
21
|
+
place: series ? keys.indexOf(series) : keys.length
|
|
22
|
+
};
|
|
23
|
+
}).sort((a, b) => a.place - b.place);
|
|
24
|
+
if (items.length === 0) return null;
|
|
25
|
+
return /* @__PURE__ */ jsx("ul", {
|
|
26
|
+
className: cx("nuv-chart__legend", className),
|
|
27
|
+
children: items.map(({ item, series }, index) => {
|
|
28
|
+
return /* @__PURE__ */ jsxs("li", {
|
|
29
|
+
className: cx("nuv-chart__legend-item", item.inactive && "nuv-chart__legend-item--inactive"),
|
|
30
|
+
children: [/* @__PURE__ */ jsx(Swatch, {
|
|
31
|
+
series,
|
|
32
|
+
color: item.color,
|
|
33
|
+
indicator
|
|
34
|
+
}), (series && config[series]?.label) ?? item.value]
|
|
35
|
+
}, `${index}-${String(item.dataKey)}-${String(item.value)}`);
|
|
36
|
+
})
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Recharts' `Legend`, drawn like the rest of the library. It takes
|
|
41
|
+
* everything `Legend` does.
|
|
42
|
+
*/
|
|
43
|
+
function ChartLegend({ indicator, nameKey, className, content, ...props }) {
|
|
44
|
+
return /* @__PURE__ */ jsx(Legend, {
|
|
45
|
+
content: content ?? /* @__PURE__ */ jsx(ChartLegendContent, {
|
|
46
|
+
indicator,
|
|
47
|
+
nameKey,
|
|
48
|
+
className
|
|
49
|
+
}),
|
|
50
|
+
...props
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { ChartLegend, ChartLegendContent };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_cx = require("../../utils/cx.cjs");
|
|
3
|
+
const require_context = require("./context.cjs");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
6
|
+
//#region src/components/chart/chart-table.tsx
|
|
7
|
+
/**
|
|
8
|
+
* The numbers of a chart as a table. A chart is a picture, and this is its
|
|
9
|
+
* text: what a screen reader reads, and what anyone can read a number off.
|
|
10
|
+
* It goes in the container's `table`.
|
|
11
|
+
*/
|
|
12
|
+
const ChartTable = (0, react.forwardRef)(function ChartTable({ data, category, categoryLabel, series, visible = false, caption, formatCategory, formatValue, className, ...props }, ref) {
|
|
13
|
+
const chart = require_context.useChart("ChartTable");
|
|
14
|
+
const format = formatValue ?? chart.formatValue;
|
|
15
|
+
const columns = series ?? Object.keys(chart.config);
|
|
16
|
+
const rows = data;
|
|
17
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
18
|
+
className: require_cx.cx("nuv-chart__data", !visible && "nuv-chart__data--hidden"),
|
|
19
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("table", {
|
|
20
|
+
ref,
|
|
21
|
+
className: require_cx.cx("nuv-chart__table", className),
|
|
22
|
+
...caption ? null : chart.name,
|
|
23
|
+
...props,
|
|
24
|
+
children: [
|
|
25
|
+
caption ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("caption", { children: caption }) : null,
|
|
26
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("thead", { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("tr", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("th", {
|
|
27
|
+
scope: "col",
|
|
28
|
+
children: categoryLabel
|
|
29
|
+
}), columns.map((key) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("th", {
|
|
30
|
+
scope: "col",
|
|
31
|
+
children: chart.config[key]?.label ?? key
|
|
32
|
+
}, key))] }) }),
|
|
33
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("tbody", { children: rows.map((row, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("tr", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("th", {
|
|
34
|
+
scope: "row",
|
|
35
|
+
children: require_context.show(row[category], category, formatCategory)
|
|
36
|
+
}), columns.map((key) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("td", { children: require_context.show(row[key], key, format) }, key))] }, index)) })
|
|
37
|
+
]
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.ChartTable = ChartTable;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ChartValueFormatter } from "./context.cjs";
|
|
2
|
+
import { ReactNode, TableHTMLAttributes } from "react";
|
|
3
|
+
//#region src/components/chart/chart-table.d.ts
|
|
4
|
+
export interface ChartTableOwnProps {
|
|
5
|
+
/** The rows the chart was drawn from. */
|
|
6
|
+
data: ReadonlyArray<object>;
|
|
7
|
+
/**
|
|
8
|
+
* The field that names each row, such as the month. It's the heading of
|
|
9
|
+
* the row.
|
|
10
|
+
*/
|
|
11
|
+
category: string;
|
|
12
|
+
/** The heading of that column. */
|
|
13
|
+
categoryLabel: ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* The fields to show, in order. Left out, it's every key of the chart's
|
|
16
|
+
* config.
|
|
17
|
+
*/
|
|
18
|
+
series?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Shows the table under the chart. Without it the table is there for
|
|
21
|
+
* screen readers only.
|
|
22
|
+
* @default false
|
|
23
|
+
*/
|
|
24
|
+
visible?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* A caption for the table. Without one the table has the chart's name.
|
|
27
|
+
*/
|
|
28
|
+
caption?: ReactNode;
|
|
29
|
+
/** How a category is written. */
|
|
30
|
+
formatCategory?: (value: number | string) => ReactNode;
|
|
31
|
+
/** How a value is written. The container's `formatValue` by default. */
|
|
32
|
+
formatValue?: ChartValueFormatter;
|
|
33
|
+
}
|
|
34
|
+
export interface ChartTableProps extends ChartTableOwnProps, Omit<TableHTMLAttributes<HTMLTableElement>, keyof ChartTableOwnProps> {}
|
|
35
|
+
/**
|
|
36
|
+
* The numbers of a chart as a table. A chart is a picture, and this is its
|
|
37
|
+
* text: what a screen reader reads, and what anyone can read a number off.
|
|
38
|
+
* It goes in the container's `table`.
|
|
39
|
+
*/
|
|
40
|
+
export declare const ChartTable: import("react").ForwardRefExoticComponent<ChartTableProps & import("react").RefAttributes<HTMLTableElement>>;
|
|
41
|
+
//#endregion
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ChartValueFormatter } from "./context.js";
|
|
2
|
+
import { ReactNode, TableHTMLAttributes } from "react";
|
|
3
|
+
//#region src/components/chart/chart-table.d.ts
|
|
4
|
+
export interface ChartTableOwnProps {
|
|
5
|
+
/** The rows the chart was drawn from. */
|
|
6
|
+
data: ReadonlyArray<object>;
|
|
7
|
+
/**
|
|
8
|
+
* The field that names each row, such as the month. It's the heading of
|
|
9
|
+
* the row.
|
|
10
|
+
*/
|
|
11
|
+
category: string;
|
|
12
|
+
/** The heading of that column. */
|
|
13
|
+
categoryLabel: ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* The fields to show, in order. Left out, it's every key of the chart's
|
|
16
|
+
* config.
|
|
17
|
+
*/
|
|
18
|
+
series?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Shows the table under the chart. Without it the table is there for
|
|
21
|
+
* screen readers only.
|
|
22
|
+
* @default false
|
|
23
|
+
*/
|
|
24
|
+
visible?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* A caption for the table. Without one the table has the chart's name.
|
|
27
|
+
*/
|
|
28
|
+
caption?: ReactNode;
|
|
29
|
+
/** How a category is written. */
|
|
30
|
+
formatCategory?: (value: number | string) => ReactNode;
|
|
31
|
+
/** How a value is written. The container's `formatValue` by default. */
|
|
32
|
+
formatValue?: ChartValueFormatter;
|
|
33
|
+
}
|
|
34
|
+
export interface ChartTableProps extends ChartTableOwnProps, Omit<TableHTMLAttributes<HTMLTableElement>, keyof ChartTableOwnProps> {}
|
|
35
|
+
/**
|
|
36
|
+
* The numbers of a chart as a table. A chart is a picture, and this is its
|
|
37
|
+
* text: what a screen reader reads, and what anyone can read a number off.
|
|
38
|
+
* It goes in the container's `table`.
|
|
39
|
+
*/
|
|
40
|
+
export declare const ChartTable: import("react").ForwardRefExoticComponent<ChartTableProps & import("react").RefAttributes<HTMLTableElement>>;
|
|
41
|
+
//#endregion
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cx } from "../../utils/cx.js";
|
|
3
|
+
import { show, useChart } from "./context.js";
|
|
4
|
+
import { forwardRef } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
//#region src/components/chart/chart-table.tsx
|
|
7
|
+
/**
|
|
8
|
+
* The numbers of a chart as a table. A chart is a picture, and this is its
|
|
9
|
+
* text: what a screen reader reads, and what anyone can read a number off.
|
|
10
|
+
* It goes in the container's `table`.
|
|
11
|
+
*/
|
|
12
|
+
const ChartTable = forwardRef(function ChartTable({ data, category, categoryLabel, series, visible = false, caption, formatCategory, formatValue, className, ...props }, ref) {
|
|
13
|
+
const chart = useChart("ChartTable");
|
|
14
|
+
const format = formatValue ?? chart.formatValue;
|
|
15
|
+
const columns = series ?? Object.keys(chart.config);
|
|
16
|
+
const rows = data;
|
|
17
|
+
return /* @__PURE__ */ jsx("div", {
|
|
18
|
+
className: cx("nuv-chart__data", !visible && "nuv-chart__data--hidden"),
|
|
19
|
+
children: /* @__PURE__ */ jsxs("table", {
|
|
20
|
+
ref,
|
|
21
|
+
className: cx("nuv-chart__table", className),
|
|
22
|
+
...caption ? null : chart.name,
|
|
23
|
+
...props,
|
|
24
|
+
children: [
|
|
25
|
+
caption ? /* @__PURE__ */ jsx("caption", { children: caption }) : null,
|
|
26
|
+
/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [/* @__PURE__ */ jsx("th", {
|
|
27
|
+
scope: "col",
|
|
28
|
+
children: categoryLabel
|
|
29
|
+
}), columns.map((key) => /* @__PURE__ */ jsx("th", {
|
|
30
|
+
scope: "col",
|
|
31
|
+
children: chart.config[key]?.label ?? key
|
|
32
|
+
}, key))] }) }),
|
|
33
|
+
/* @__PURE__ */ jsx("tbody", { children: rows.map((row, index) => /* @__PURE__ */ jsxs("tr", { children: [/* @__PURE__ */ jsx("th", {
|
|
34
|
+
scope: "row",
|
|
35
|
+
children: show(row[category], category, formatCategory)
|
|
36
|
+
}), columns.map((key) => /* @__PURE__ */ jsx("td", { children: show(row[key], key, format) }, key))] }, index)) })
|
|
37
|
+
]
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
//#endregion
|
|
42
|
+
export { ChartTable };
|