@sybilion/uilib 1.8.3 → 1.8.4
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/esm/src/components/ui/BarChart/BarChart.js +39 -7
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +71 -6
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.styl.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartAxes.js +11 -3
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +6 -3
- package/dist/esm/src/components/ui/Chart/components/QuantileBands.js +1 -1
- package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.js +20 -0
- package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.js +7 -0
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +49 -13
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.js +45 -11
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +15 -8
- package/dist/esm/types/src/components/ui/BarChart/BarChart.types.d.ts +22 -0
- package/dist/esm/types/src/components/ui/Chart/chartForecastVisualization.types.d.ts +5 -0
- package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +22 -5
- package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.d.ts +8 -1
- package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.d.ts +22 -0
- package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/index.d.ts +1 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +10 -1
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/chartHistories.d.ts +5 -0
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.d.ts +10 -3
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +1 -1
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +9 -0
- package/dist/esm/types/tests/viewChartGaps.test.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ui/BarChart/AGENT.md +4 -0
- package/src/components/ui/BarChart/BarChart.styl.d.ts +1 -1
- package/src/components/ui/BarChart/BarChart.tsx +107 -21
- package/src/components/ui/BarChart/BarChart.types.ts +22 -0
- package/src/components/ui/Chart/AGENT.md +3 -0
- package/src/components/ui/Chart/chartForecastVisualization.types.ts +5 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.styl +5 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.styl.d.ts +1 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.tsx +124 -5
- package/src/components/ui/Chart/components/ChartAxes.tsx +29 -5
- package/src/components/ui/Chart/components/ChartTooltipContent.tsx +6 -3
- package/src/components/ui/Chart/components/QuantileBands.tsx +1 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl +34 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.d.ts +12 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.tsx +70 -0
- package/src/components/ui/Chart/components/SeriesLegend/index.ts +1 -0
- package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +110 -40
- package/src/components/ui/ChartAreaInteractive/chartHistories.ts +5 -0
- package/src/components/ui/StackedAreaChart/AGENT.md +1 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +54 -13
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +1 -1
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +36 -12
- package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +9 -0
|
@@ -83,6 +83,11 @@ export interface QuantileBandConfig {
|
|
|
83
83
|
strokeOpacity?: number;
|
|
84
84
|
type?: 'monotone' | 'linear' | 'step' | 'stepBefore' | 'stepAfter' | 'basis' | 'natural';
|
|
85
85
|
fillPattern?: 'solid' | 'dots' | 'lines';
|
|
86
|
+
/**
|
|
87
|
+
* List the range in the tooltip (default true). Read by the Recharts charts'
|
|
88
|
+
* bands (BaseChartWrapper, ChartAreaInteractive's default mode).
|
|
89
|
+
*/
|
|
90
|
+
showInTooltip?: boolean;
|
|
86
91
|
/**
|
|
87
92
|
* Fill across months with no range (default true). Read by the Recharts
|
|
88
93
|
* charts' bands (BaseChartWrapper, ChartAreaInteractive's default mode),
|
|
@@ -53,6 +53,12 @@ export interface BaseChartWrapperProps {
|
|
|
53
53
|
historyOnTop?: boolean;
|
|
54
54
|
/** Mark each line's last point with a dot (default: on). */
|
|
55
55
|
showEndDots?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Dot a point a line would leave invisible, so it still shows: with no
|
|
58
|
+
* value on either side on a line not drawn across gaps, or a line's only
|
|
59
|
+
* value on one drawn across them (default: off).
|
|
60
|
+
*/
|
|
61
|
+
dotLonePoints?: boolean;
|
|
56
62
|
formatDate?: (value: string, detailed?: boolean) => string;
|
|
57
63
|
labelFormatter?: (value: unknown) => string;
|
|
58
64
|
onLegendClick?: (data: LegendPayload, index: number, event: any) => void;
|
|
@@ -81,6 +87,13 @@ export interface BaseChartWrapperProps {
|
|
|
81
87
|
showChartAxesLegend?: boolean;
|
|
82
88
|
xAxisLabel?: string;
|
|
83
89
|
yAxisLabel?: string;
|
|
90
|
+
/**
|
|
91
|
+
* `category` (default) gives every row one slot; `number` places the rows
|
|
92
|
+
* by their `date` read as a number (`'12.5'`), spaced by value.
|
|
93
|
+
*/
|
|
94
|
+
xAxisType?: 'category' | 'number';
|
|
95
|
+
/** The x values to label (for example the first day of each month). */
|
|
96
|
+
xTicks?: ReadonlyArray<string | number>;
|
|
84
97
|
showActiveDots?: boolean;
|
|
85
98
|
overlayElements?: ReactNode;
|
|
86
99
|
hiddenSeries?: Set<string>;
|
|
@@ -91,7 +104,15 @@ export interface BaseChartWrapperProps {
|
|
|
91
104
|
onFailedAnalysisClick?: (analysisId?: number | string) => void;
|
|
92
105
|
maxVisibleItems?: number;
|
|
93
106
|
preventDeselection?: boolean;
|
|
94
|
-
|
|
107
|
+
/**
|
|
108
|
+
* `default`: the analyses legend (forecast icons, +N overflow, latest
|
|
109
|
+
* first). `svg`: drawn into the chart for export. `plain`: one entry per
|
|
110
|
+
* line, in `legendOrder` (else histories then forecasts), each a button
|
|
111
|
+
* that hides or shows it through `onLegendClick`.
|
|
112
|
+
*/
|
|
113
|
+
legendVariant?: 'default' | 'svg' | 'plain';
|
|
114
|
+
/** With `legendVariant="plain"`: the lines' data keys in legend order. */
|
|
115
|
+
legendOrder?: string[];
|
|
95
116
|
legendWidth?: number;
|
|
96
117
|
legendMarginLeft?: number;
|
|
97
118
|
containerProps?: Omit<ComponentProps<typeof ChartContainer>, 'config' | 'className' | 'children'>;
|
|
@@ -111,8 +132,4 @@ export interface BaseChartWrapperProps {
|
|
|
111
132
|
/** Isolate a single series by data key; `allDataKeys` lists every toggleable key. */
|
|
112
133
|
onShowOnly?: (dataKey: string, allDataKeys: string[]) => void;
|
|
113
134
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Base chart wrapper component with common structure
|
|
116
|
-
* Handles container, grid, axes, tooltip, and legend
|
|
117
|
-
*/
|
|
118
135
|
export declare function BaseChartWrapper(props: BaseChartWrapperProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -10,6 +10,13 @@ interface ChartAxesProps {
|
|
|
10
10
|
yMin?: number;
|
|
11
11
|
yMax?: number;
|
|
12
12
|
autoScaleYAxis?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* `category` (default) gives every row one slot; `number` places the rows
|
|
15
|
+
* by their `date` read as a number, from the smallest to the largest.
|
|
16
|
+
*/
|
|
17
|
+
xAxisType?: 'category' | 'number';
|
|
18
|
+
/** The x values to label; without it recharts picks them. */
|
|
19
|
+
xTicks?: ReadonlyArray<string | number>;
|
|
13
20
|
/**
|
|
14
21
|
* Draw no axes. A hidden x-axis is kept, so the tooltip still names the
|
|
15
22
|
* row's date. The y-axis keeps fixed `yMin`/`yMax` (with `autoScaleYAxis`
|
|
@@ -18,5 +25,5 @@ interface ChartAxesProps {
|
|
|
18
25
|
*/
|
|
19
26
|
hide?: boolean;
|
|
20
27
|
}
|
|
21
|
-
export declare function ChartAxes({ xAxisClassName, yAxisClassName, xAxisLabel, yAxisLabel, formatDate, formatNumber, xMin, xMax, yMin, yMax, autoScaleYAxis, hide, }: ChartAxesProps): import("react/jsx-runtime").JSX.Element;
|
|
28
|
+
export declare function ChartAxes({ xAxisClassName, yAxisClassName, xAxisLabel, yAxisLabel, formatDate, formatNumber, xMin, xMax, yMin, yMax, autoScaleYAxis, xAxisType, xTicks, hide, }: ChartAxesProps): import("react/jsx-runtime").JSX.Element;
|
|
22
29
|
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
export type SeriesLegendItem = {
|
|
3
|
+
/** The series' data key; the legend hands it back on a click. */
|
|
4
|
+
dataKey: string;
|
|
5
|
+
label: ReactNode;
|
|
6
|
+
color: string;
|
|
7
|
+
/** Draw the swatch as a dashed line (a forecast line). */
|
|
8
|
+
dashed?: boolean;
|
|
9
|
+
/** Draw the swatch as a filled square (a bar or an area) instead of a line. */
|
|
10
|
+
square?: boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A plain legend: one entry per series, in the order given, each a button
|
|
14
|
+
* that hides or shows its series. A hidden series' entry is dimmed.
|
|
15
|
+
*/
|
|
16
|
+
export declare function SeriesLegend({ items, hiddenSeries, onToggle, className, }: {
|
|
17
|
+
items: SeriesLegendItem[];
|
|
18
|
+
hiddenSeries?: ReadonlySet<string>;
|
|
19
|
+
/** Without it the entries are text, not buttons. */
|
|
20
|
+
onToggle?: (dataKey: string) => void;
|
|
21
|
+
className?: string;
|
|
22
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SeriesLegend, type SeriesLegendItem } from './SeriesLegend';
|
|
@@ -19,6 +19,11 @@ export type ForecastItemData = {
|
|
|
19
19
|
* histories, a forecast joins one only when this names it.
|
|
20
20
|
*/
|
|
21
21
|
continues?: string | number;
|
|
22
|
+
/**
|
|
23
|
+
* Draw this line across rows it has no value for. Without it the chart's
|
|
24
|
+
* `connectForecastNulls` decides.
|
|
25
|
+
*/
|
|
26
|
+
connectNulls?: boolean;
|
|
22
27
|
};
|
|
23
28
|
export type Dot = {
|
|
24
29
|
x: number;
|
|
@@ -43,6 +48,8 @@ export type HistoryLine = {
|
|
|
43
48
|
dataKey: string;
|
|
44
49
|
name: string;
|
|
45
50
|
color: string;
|
|
51
|
+
/** The line's own `connectNulls`, when it sets one. */
|
|
52
|
+
connectNulls?: boolean;
|
|
46
53
|
};
|
|
47
54
|
/** Default colour of the history at `index`: the forecast palette from its far end. */
|
|
48
55
|
export declare function getHistoryColor(index: number): string;
|
|
@@ -50,7 +57,7 @@ export declare function getHistoryColor(index: number): string;
|
|
|
50
57
|
export declare function resolveHistoryLines(histories?: HistoryItemData[]): HistoryLine[];
|
|
51
58
|
/** Same tint logic as intervals/threshold overlays: band fill follows forecast line hex when known. */
|
|
52
59
|
export declare function resolveQuantileBandFillForForecastLine(lineColor: string, forecastIndex: number): string;
|
|
53
|
-
export declare const ChartLines: import("react").MemoExoticComponent<({ chartData, forecastData, historyData, hiddenSeries, isDarkTheme, shouldAnimate, historicalLineColor, showLegend, forecastLineStyle, disableAnimation, disableHistoricalAnimation, connectHistoryNulls, connectForecastNulls, historyOnTop, showEndDots, }: {
|
|
60
|
+
export declare const ChartLines: import("react").MemoExoticComponent<({ chartData, forecastData, historyData, hiddenSeries, isDarkTheme, shouldAnimate, historicalLineColor, showLegend, forecastLineStyle, disableAnimation, disableHistoricalAnimation, connectHistoryNulls, connectForecastNulls, historyOnTop, showEndDots, dotLonePoints, }: {
|
|
54
61
|
chartData: ChartDataPoint[];
|
|
55
62
|
forecastData?: ForecastItemData[];
|
|
56
63
|
/** Several history lines (one per product). Omit for the single `historical` line. */
|
|
@@ -74,4 +81,6 @@ export declare const ChartLines: import("react").MemoExoticComponent<({ chartDat
|
|
|
74
81
|
historyOnTop?: boolean;
|
|
75
82
|
/** Mark each line's last point with a dot (default: on). */
|
|
76
83
|
showEndDots?: boolean;
|
|
84
|
+
/** Dot a point a gapped line leaves alone (default: off). */
|
|
85
|
+
dotLonePoints?: boolean;
|
|
77
86
|
}) => import("react/jsx-runtime").JSX.Element>;
|
|
@@ -21,6 +21,11 @@ export type HistoryItemData = {
|
|
|
21
21
|
* colour to both.
|
|
22
22
|
*/
|
|
23
23
|
color?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Draw this line across rows it has no value for. Without it the chart's
|
|
26
|
+
* `connectHistoryNulls` decides.
|
|
27
|
+
*/
|
|
28
|
+
connectNulls?: boolean;
|
|
24
29
|
};
|
|
25
30
|
/** Data key of one history line in `chartData` rows. */
|
|
26
31
|
export declare function historySeriesKey(id: string | number): string;
|
|
@@ -48,13 +48,20 @@ export declare function compareRowZones(rows: readonly StackedAreaChartDatum[],
|
|
|
48
48
|
* A value axis from 0 for a top value: at most `maxIntervals` steps of
|
|
49
49
|
* 1, 2, 2.5 or 5 × 10ⁿ, with a little headroom above the top. Small changes
|
|
50
50
|
* in the top (a margin being typed) move the axis by one step at most,
|
|
51
|
-
* instead of doubling it the way automatic rounding can.
|
|
51
|
+
* instead of doubling it the way automatic rounding can. A `bottom` below 0
|
|
52
|
+
* (a negative layer or line) starts the axis at a step below it, as `min`.
|
|
52
53
|
*/
|
|
53
|
-
export declare function valueAxis(top: number, maxIntervals?: number, headroom?: number): {
|
|
54
|
+
export declare function valueAxis(top: number, maxIntervals?: number, headroom?: number, bottom?: number): {
|
|
55
|
+
min?: number;
|
|
54
56
|
max: number;
|
|
55
57
|
ticks: number[];
|
|
56
58
|
};
|
|
57
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* The lowest point the stack (a negative layer or margin pulls it below 0)
|
|
61
|
+
* or a line reaches; 0 when nothing is negative.
|
|
62
|
+
*/
|
|
63
|
+
export declare function chartBottom(rows: readonly StackedAreaChartDatum[], layerKeys: readonly string[], lineKeys: readonly string[]): number;
|
|
64
|
+
/** The highest point the stack (layers plus margin) or a line reaches. */
|
|
58
65
|
export declare function chartTop(rows: readonly StackedAreaChartDatum[], layerKeys: readonly string[], lineKeys: readonly string[]): number;
|
|
59
66
|
/**
|
|
60
67
|
* The x value of the first row in each year, when x values are date strings
|
|
@@ -76,6 +76,15 @@ export interface StackedAreaChartProps extends Omit<HTMLAttributes<HTMLDivElemen
|
|
|
76
76
|
showGrid?: boolean;
|
|
77
77
|
/** Formats numeric y-axis ticks and tooltip values. */
|
|
78
78
|
valueFormatter?: (value: number) => string;
|
|
79
|
+
/**
|
|
80
|
+
* Formats the tooltip's values (for example with a unit), when they should
|
|
81
|
+
* read differently from the axis. Defaults to `valueFormatter`.
|
|
82
|
+
*/
|
|
83
|
+
tooltipValueFormatter?: (value: number) => string;
|
|
84
|
+
/** Title under the x axis. */
|
|
85
|
+
xAxisLabel?: string;
|
|
86
|
+
/** Title along the y axis. */
|
|
87
|
+
yAxisLabel?: string;
|
|
79
88
|
/** Formats x-axis ticks (ignored for year ticks, which show the year). */
|
|
80
89
|
xTickFormatter?: (value: string | number) => string;
|
|
81
90
|
/** Formats the tooltip's x label. */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
package/package.json
CHANGED
|
@@ -14,3 +14,7 @@ BarChart compares values across categories. Provide rows of data, a category key
|
|
|
14
14
|
## Do
|
|
15
15
|
|
|
16
16
|
- Start the value axis at zero so bar lengths stay proportional.
|
|
17
|
+
- Pass `valueFormatter` for the axis and `tooltipValueFormatter` for the tooltip (for example with a unit; without it the tooltip prints the raw value); `categoryTickFormatter` and `tooltipLabelFormatter` format the categories (dates, for instance).
|
|
18
|
+
- Name the axes with `categoryAxisLabel` and `valueAxisLabel`.
|
|
19
|
+
- The legend sits under the chart box (it adds its own height to `height`). It is plain text unless `onToggleKey` is passed; then a click hides or shows a series. Pass `hiddenKeys` too when the host must remember which are hidden.
|
|
20
|
+
- `disableAnimation` draws the bars at once (for a page that is exported or printed).
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import cn from 'classnames';
|
|
2
2
|
import { forwardRef, useMemo } from 'react';
|
|
3
3
|
|
|
4
|
-
import type { ChartConfig } from '#uilib/components/ui/Chart';
|
|
5
|
-
import {
|
|
6
|
-
ChartContainer,
|
|
7
|
-
ChartLegend,
|
|
8
|
-
ChartLegendContent,
|
|
9
|
-
ChartTooltip,
|
|
10
|
-
ChartTooltipContent,
|
|
11
|
-
} from '#uilib/components/ui/Chart';
|
|
4
|
+
import type { ChartConfig } from '#uilib/components/ui/Chart/Chart.types';
|
|
12
5
|
import { getChartSeriesColor } from '#uilib/components/ui/Chart/chartSeriesPalette';
|
|
6
|
+
// Straight from the components, not the Chart index, which also loads the
|
|
7
|
+
// canvas chart (lightweight-charts) and its styles.
|
|
8
|
+
import { ChartContainer } from '#uilib/components/ui/Chart/components/ChartContainer';
|
|
9
|
+
import { ChartPlacedTooltip as ChartTooltip } from '#uilib/components/ui/Chart/components/ChartPlacedTooltip';
|
|
10
|
+
import { ChartTooltipContent } from '#uilib/components/ui/Chart/components/ChartTooltipContent';
|
|
11
|
+
import { SeriesLegend } from '#uilib/components/ui/Chart/components/SeriesLegend';
|
|
12
|
+
import { useLegendState } from '#uilib/components/ui/Chart/hooks/useLegendState';
|
|
13
13
|
import {
|
|
14
14
|
Bar,
|
|
15
15
|
CartesianGrid,
|
|
16
|
+
Label,
|
|
16
17
|
BarChart as RechartsBarChart,
|
|
17
18
|
XAxis,
|
|
18
19
|
YAxis,
|
|
@@ -22,6 +23,11 @@ import S from './BarChart.styl';
|
|
|
22
23
|
import { BarChartProps, BarChartSeries } from './BarChart.types';
|
|
23
24
|
|
|
24
25
|
const STACK_ID = 'stack';
|
|
26
|
+
/** Room for an axis title beside the ticks (recharts' default is 30). */
|
|
27
|
+
const CATEGORY_AXIS_HEIGHT_WITH_LABEL = 44;
|
|
28
|
+
|
|
29
|
+
const seriesColor = (item: BarChartSeries, index: number) =>
|
|
30
|
+
item.color ?? getChartSeriesColor(index);
|
|
25
31
|
|
|
26
32
|
function buildChartConfig(series: BarChartSeries[]): ChartConfig {
|
|
27
33
|
const config: ChartConfig = {};
|
|
@@ -29,7 +35,7 @@ function buildChartConfig(series: BarChartSeries[]): ChartConfig {
|
|
|
29
35
|
series.forEach((item, index) => {
|
|
30
36
|
config[item.key] = {
|
|
31
37
|
label: item.label ?? item.key,
|
|
32
|
-
color: item
|
|
38
|
+
color: seriesColor(item, index),
|
|
33
39
|
};
|
|
34
40
|
});
|
|
35
41
|
|
|
@@ -49,18 +55,58 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
49
55
|
showTooltip = true,
|
|
50
56
|
showGrid = true,
|
|
51
57
|
valueFormatter,
|
|
58
|
+
tooltipValueFormatter,
|
|
59
|
+
categoryTickFormatter,
|
|
60
|
+
tooltipLabelFormatter,
|
|
61
|
+
categoryAxisLabel,
|
|
62
|
+
valueAxisLabel,
|
|
63
|
+
hiddenKeys,
|
|
64
|
+
onToggleKey,
|
|
52
65
|
height,
|
|
66
|
+
disableAnimation,
|
|
53
67
|
...props
|
|
54
68
|
},
|
|
55
69
|
ref,
|
|
56
70
|
) => {
|
|
57
71
|
const config = useMemo(() => buildChartConfig(series), [series]);
|
|
72
|
+
const legend = useLegendState();
|
|
73
|
+
const hidden: ReadonlySet<string> = hiddenKeys ?? legend.hiddenSeries;
|
|
74
|
+
// The legend entries are buttons only for a host that asked for toggling
|
|
75
|
+
// (onToggleKey); without hiddenKeys the chart keeps the hidden set itself.
|
|
76
|
+
const toggle = onToggleKey
|
|
77
|
+
? (key: string) => {
|
|
78
|
+
onToggleKey(key);
|
|
79
|
+
if (!hiddenKeys) legend.toggleSeries(key);
|
|
80
|
+
}
|
|
81
|
+
: undefined;
|
|
58
82
|
|
|
59
83
|
const isHorizontal = orientation === 'horizontal';
|
|
60
84
|
const stackId = mode === 'stacked' ? STACK_ID : undefined;
|
|
61
85
|
const barRadius: [number, number, number, number] | number =
|
|
62
86
|
mode === 'stacked' ? 0 : isHorizontal ? [0, 3, 3, 0] : [3, 3, 0, 0];
|
|
63
87
|
|
|
88
|
+
const axisLine = { stroke: 'var(--border)', strokeWidth: 1 };
|
|
89
|
+
const categoryLabel = categoryAxisLabel ? (
|
|
90
|
+
isHorizontal ? (
|
|
91
|
+
<Label value={categoryAxisLabel} angle={-90} position="insideLeft" />
|
|
92
|
+
) : (
|
|
93
|
+
<Label value={categoryAxisLabel} position="insideBottom" offset={0} />
|
|
94
|
+
)
|
|
95
|
+
) : null;
|
|
96
|
+
const valueLabel = valueAxisLabel ? (
|
|
97
|
+
isHorizontal ? (
|
|
98
|
+
<Label value={valueAxisLabel} position="insideBottom" offset={0} />
|
|
99
|
+
) : (
|
|
100
|
+
<Label value={valueAxisLabel} angle={-90} position="insideLeft" />
|
|
101
|
+
)
|
|
102
|
+
) : null;
|
|
103
|
+
const labelFormatter = tooltipLabelFormatter
|
|
104
|
+
? (label: unknown) =>
|
|
105
|
+
tooltipLabelFormatter(
|
|
106
|
+
typeof label === 'number' ? label : String(label ?? ''),
|
|
107
|
+
)
|
|
108
|
+
: undefined;
|
|
109
|
+
|
|
64
110
|
return (
|
|
65
111
|
<div ref={ref} className={cn(S.root, className)} {...props}>
|
|
66
112
|
<ChartContainer
|
|
@@ -85,16 +131,25 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
85
131
|
<XAxis
|
|
86
132
|
type="number"
|
|
87
133
|
tickLine={false}
|
|
88
|
-
axisLine={
|
|
134
|
+
axisLine={axisLine}
|
|
89
135
|
tickFormatter={valueFormatter}
|
|
90
|
-
|
|
136
|
+
height={
|
|
137
|
+
valueAxisLabel ? CATEGORY_AXIS_HEIGHT_WITH_LABEL : undefined
|
|
138
|
+
}
|
|
139
|
+
>
|
|
140
|
+
{valueLabel}
|
|
141
|
+
</XAxis>
|
|
91
142
|
<YAxis
|
|
92
143
|
type="category"
|
|
93
144
|
dataKey={categoryKey}
|
|
94
145
|
tickLine={false}
|
|
95
|
-
axisLine={
|
|
96
|
-
|
|
97
|
-
|
|
146
|
+
axisLine={axisLine}
|
|
147
|
+
tickFormatter={categoryTickFormatter}
|
|
148
|
+
// Room for the rotated title beside the category names.
|
|
149
|
+
width={categoryAxisLabel ? 96 : 80}
|
|
150
|
+
>
|
|
151
|
+
{categoryLabel}
|
|
152
|
+
</YAxis>
|
|
98
153
|
</>
|
|
99
154
|
) : (
|
|
100
155
|
<>
|
|
@@ -102,21 +157,38 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
102
157
|
type="category"
|
|
103
158
|
dataKey={categoryKey}
|
|
104
159
|
tickLine={false}
|
|
105
|
-
axisLine={
|
|
106
|
-
|
|
160
|
+
axisLine={axisLine}
|
|
161
|
+
tickFormatter={categoryTickFormatter}
|
|
162
|
+
height={
|
|
163
|
+
categoryAxisLabel
|
|
164
|
+
? CATEGORY_AXIS_HEIGHT_WITH_LABEL
|
|
165
|
+
: undefined
|
|
166
|
+
}
|
|
167
|
+
>
|
|
168
|
+
{categoryLabel}
|
|
169
|
+
</XAxis>
|
|
107
170
|
<YAxis
|
|
108
171
|
type="number"
|
|
109
172
|
tickLine={false}
|
|
110
|
-
axisLine={
|
|
111
|
-
|
|
173
|
+
axisLine={axisLine}
|
|
174
|
+
// Room for the title beside the tick labels.
|
|
175
|
+
width={valueAxisLabel ? 64 : 48}
|
|
112
176
|
tickFormatter={valueFormatter}
|
|
113
|
-
|
|
177
|
+
>
|
|
178
|
+
{valueLabel}
|
|
179
|
+
</YAxis>
|
|
114
180
|
</>
|
|
115
181
|
)}
|
|
116
182
|
{showTooltip && (
|
|
117
183
|
<ChartTooltip
|
|
118
184
|
cursor={{ fill: 'var(--muted)' }}
|
|
119
|
-
content={
|
|
185
|
+
content={
|
|
186
|
+
<ChartTooltipContent
|
|
187
|
+
indicator="dot"
|
|
188
|
+
valueFormatter={tooltipValueFormatter}
|
|
189
|
+
labelFormatter={labelFormatter}
|
|
190
|
+
/>
|
|
191
|
+
}
|
|
120
192
|
/>
|
|
121
193
|
)}
|
|
122
194
|
{series.map(item => (
|
|
@@ -127,11 +199,25 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
127
199
|
stackId={stackId}
|
|
128
200
|
radius={barRadius}
|
|
129
201
|
maxBarSize={48}
|
|
202
|
+
hide={hidden.has(item.key)}
|
|
203
|
+
isAnimationActive={!disableAnimation}
|
|
130
204
|
/>
|
|
131
205
|
))}
|
|
132
|
-
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
|
|
133
206
|
</RechartsBarChart>
|
|
134
207
|
</ChartContainer>
|
|
208
|
+
|
|
209
|
+
{showLegend && (
|
|
210
|
+
<SeriesLegend
|
|
211
|
+
items={series.map((item, index) => ({
|
|
212
|
+
dataKey: item.key,
|
|
213
|
+
label: item.label ?? item.key,
|
|
214
|
+
color: seriesColor(item, index),
|
|
215
|
+
square: true,
|
|
216
|
+
}))}
|
|
217
|
+
hiddenSeries={hidden}
|
|
218
|
+
onToggle={toggle}
|
|
219
|
+
/>
|
|
220
|
+
)}
|
|
135
221
|
</div>
|
|
136
222
|
);
|
|
137
223
|
},
|
|
@@ -31,6 +31,28 @@ export interface BarChartProps extends Omit<
|
|
|
31
31
|
showGrid?: boolean;
|
|
32
32
|
/** Formats numeric axis tick values. */
|
|
33
33
|
valueFormatter?: (value: number) => string;
|
|
34
|
+
/** Formats the tooltip's values (for example with a unit); without it they print as they are. */
|
|
35
|
+
tooltipValueFormatter?: (value: number) => string;
|
|
36
|
+
/** Formats the category axis's ticks. */
|
|
37
|
+
categoryTickFormatter?: (value: string | number) => string;
|
|
38
|
+
/** Formats the tooltip's category label. */
|
|
39
|
+
tooltipLabelFormatter?: (value: string | number) => string;
|
|
40
|
+
/** Title of the category axis. */
|
|
41
|
+
categoryAxisLabel?: string;
|
|
42
|
+
/** Title of the value axis. */
|
|
43
|
+
valueAxisLabel?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Keys of hidden series. With it, the host owns visibility; without it
|
|
46
|
+
* (and with onToggleKey) the chart keeps the hidden set itself.
|
|
47
|
+
*/
|
|
48
|
+
hiddenKeys?: ReadonlySet<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Called with a series key when its legend entry is clicked. Without it the
|
|
51
|
+
* legend is plain text.
|
|
52
|
+
*/
|
|
53
|
+
onToggleKey?: (key: string) => void;
|
|
34
54
|
/** Fixed chart height in px; defaults to a 16:9 responsive box. */
|
|
35
55
|
height?: number;
|
|
56
|
+
/** Draw the bars at once, without the grow-in animation. */
|
|
57
|
+
disableAnimation?: boolean;
|
|
36
58
|
}
|
|
@@ -16,6 +16,9 @@ ChartContainer wraps Recharts charts with the library’s theming, mapping a Cha
|
|
|
16
16
|
- `historyOnTop` draws the history lines over the forecast lines, for many faint forecasts that would otherwise cover them.
|
|
17
17
|
- `showAxes={false}` draws no axes but keeps a hidden x-axis, so the tooltip still names the row's date; fixed `yMin`/`yMax` (with `autoScaleYAxis={false}`) still set the y range.
|
|
18
18
|
- `showEndDots={false}` drops the dot on each line's last point (for many lines drawn as one bundle).
|
|
19
|
+
- A history or forecast item's `connectNulls` overrides the chart's `connectHistoryNulls` / `connectForecastNulls` for that line. With `dotLonePoints`, a point the line would leave invisible gets a dot: on a line not drawn across gaps, a point with no value on either side; on a line drawn across gaps, its only value.
|
|
20
|
+
- `legendVariant="plain"` lists every line (history and forecast alike) in `legendOrder` (their data keys), each a button that calls `onLegendClick` with its data key; the default legend is the analyses selector (forecast icons, +N overflow, latest first). A band with `showInTooltip: false` stays out of the tooltip.
|
|
21
|
+
- `xAxisType="number"` places the rows by their `date` read as a number, spaced by value; `xTicks` names the x values to label (for example the first day of each month on a daily chart).
|
|
19
22
|
- Use ChartTooltip (not Recharts' Tooltip directly) so a chart narrower than 500px (`NARROW_CHART_PX`) opens its tooltip under the chart instead of over the lines, starting right under the x-axis labels (over any legend below). BaseChartWrapper, ChartAreaInteractive, BarChart, DonutChart, StackedAreaChart and the driver card chart already do. A parent that hides overflow (a chat bubble, a card) can clip it there. Pass `position` to place it yourself.
|
|
20
23
|
|
|
21
24
|
## Example
|
|
@@ -96,6 +96,11 @@ export interface QuantileBandConfig {
|
|
|
96
96
|
| 'basis'
|
|
97
97
|
| 'natural';
|
|
98
98
|
fillPattern?: 'solid' | 'dots' | 'lines';
|
|
99
|
+
/**
|
|
100
|
+
* List the range in the tooltip (default true). Read by the Recharts charts'
|
|
101
|
+
* bands (BaseChartWrapper, ChartAreaInteractive's default mode).
|
|
102
|
+
*/
|
|
103
|
+
showInTooltip?: boolean;
|
|
99
104
|
/**
|
|
100
105
|
* Fill across months with no range (default true). Read by the Recharts
|
|
101
106
|
* charts' bands (BaseChartWrapper, ChartAreaInteractive's default mode),
|