@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
|
@@ -30,6 +30,11 @@ export type ForecastItemData = {
|
|
|
30
30
|
* histories, a forecast joins one only when this names it.
|
|
31
31
|
*/
|
|
32
32
|
continues?: string | number;
|
|
33
|
+
/**
|
|
34
|
+
* Draw this line across rows it has no value for. Without it the chart's
|
|
35
|
+
* `connectForecastNulls` decides.
|
|
36
|
+
*/
|
|
37
|
+
connectNulls?: boolean;
|
|
33
38
|
};
|
|
34
39
|
|
|
35
40
|
export type Dot = {
|
|
@@ -63,7 +68,13 @@ export const getForecastQuantileBandColor = (index: number) =>
|
|
|
63
68
|
];
|
|
64
69
|
|
|
65
70
|
/** A history line as drawn: data key, label and colour with defaults applied. */
|
|
66
|
-
export type HistoryLine = {
|
|
71
|
+
export type HistoryLine = {
|
|
72
|
+
dataKey: string;
|
|
73
|
+
name: string;
|
|
74
|
+
color: string;
|
|
75
|
+
/** The line's own `connectNulls`, when it sets one. */
|
|
76
|
+
connectNulls?: boolean;
|
|
77
|
+
};
|
|
67
78
|
|
|
68
79
|
/** Default colour of the history at `index`: the forecast palette from its far end. */
|
|
69
80
|
export function getHistoryColor(index: number): string {
|
|
@@ -79,6 +90,7 @@ export function resolveHistoryLines(
|
|
|
79
90
|
dataKey: historySeriesKey(h.id),
|
|
80
91
|
name: h.name || String(h.id),
|
|
81
92
|
color: h.color ?? getHistoryColor(index),
|
|
93
|
+
connectNulls: h.connectNulls,
|
|
82
94
|
}));
|
|
83
95
|
}
|
|
84
96
|
|
|
@@ -110,6 +122,7 @@ export const ChartLines = memo(
|
|
|
110
122
|
connectForecastNulls = false,
|
|
111
123
|
historyOnTop = false,
|
|
112
124
|
showEndDots = true,
|
|
125
|
+
dotLonePoints = false,
|
|
113
126
|
}: {
|
|
114
127
|
chartData: ChartDataPoint[];
|
|
115
128
|
forecastData?: ForecastItemData[];
|
|
@@ -134,6 +147,8 @@ export const ChartLines = memo(
|
|
|
134
147
|
historyOnTop?: boolean;
|
|
135
148
|
/** Mark each line's last point with a dot (default: on). */
|
|
136
149
|
showEndDots?: boolean;
|
|
150
|
+
/** Dot a point a gapped line leaves alone (default: off). */
|
|
151
|
+
dotLonePoints?: boolean;
|
|
137
152
|
}) => {
|
|
138
153
|
const dotStroke = isDarkTheme ? '#000000' : '#FFFFFF';
|
|
139
154
|
|
|
@@ -199,31 +214,61 @@ export const ChartLines = memo(
|
|
|
199
214
|
return indices;
|
|
200
215
|
}, [chartData, forecastData]);
|
|
201
216
|
|
|
202
|
-
//
|
|
217
|
+
// How many values each line has, for `dotLonePoints` on a line drawn
|
|
218
|
+
// across gaps: such a line leaves a point alone only when it is its only
|
|
219
|
+
// value.
|
|
220
|
+
const valueCounts = useMemo(() => {
|
|
221
|
+
if (!dotLonePoints) return undefined;
|
|
222
|
+
const counts: Record<string, number> = {};
|
|
223
|
+
for (const row of chartData) {
|
|
224
|
+
for (const [key, value] of Object.entries(row)) {
|
|
225
|
+
if (typeof value === 'number') counts[key] = (counts[key] ?? 0) + 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return counts;
|
|
229
|
+
}, [chartData, dotLonePoints]);
|
|
230
|
+
|
|
231
|
+
// A dot on the line's last point (with `showEndDots`), and (with
|
|
232
|
+
// `dotLonePoints`) on any point the line leaves alone, which would
|
|
233
|
+
// otherwise not show: no value in the rows on either side, or, for a line
|
|
234
|
+
// drawn across gaps, no other value at all.
|
|
203
235
|
const renderDot = (
|
|
204
236
|
props: any,
|
|
205
237
|
lastIndex: number,
|
|
206
238
|
dataKey: string,
|
|
207
239
|
color: string,
|
|
240
|
+
connected: boolean,
|
|
208
241
|
) => {
|
|
242
|
+
const i = props.index;
|
|
209
243
|
if (
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
props.
|
|
214
|
-
props.cx != null
|
|
244
|
+
typeof i !== 'number' ||
|
|
245
|
+
chartData[i]?.[dataKey] == null ||
|
|
246
|
+
props.cy == null ||
|
|
247
|
+
props.cx == null
|
|
215
248
|
) {
|
|
216
|
-
return
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
const isEnd = showEndDots && lastIndex >= 0 && i === lastIndex;
|
|
252
|
+
const isAlone =
|
|
253
|
+
dotLonePoints &&
|
|
254
|
+
(connected
|
|
255
|
+
? valueCounts?.[dataKey] === 1
|
|
256
|
+
: chartData[i - 1]?.[dataKey] == null &&
|
|
257
|
+
chartData[i + 1]?.[dataKey] == null);
|
|
258
|
+
if (isEnd || isAlone) {
|
|
259
|
+
return (
|
|
260
|
+
<circle
|
|
261
|
+
key={`dot-${dataKey}-${i}`}
|
|
262
|
+
cx={props.cx}
|
|
263
|
+
cy={props.cy}
|
|
264
|
+
r={3}
|
|
265
|
+
fill={color}
|
|
266
|
+
/>
|
|
267
|
+
);
|
|
217
268
|
}
|
|
218
269
|
return null;
|
|
219
270
|
};
|
|
220
271
|
|
|
221
|
-
const renderHistoryDot = (props: any, dataKey: string, color: string) =>
|
|
222
|
-
renderDot(props, lastHistoryIndices[dataKey] ?? -1, dataKey, color);
|
|
223
|
-
|
|
224
|
-
const renderForecastDot = (props: any, id: number, color: string) =>
|
|
225
|
-
renderDot(props, lastForecastIndices[id] ?? -1, `forecast_${id}`, color);
|
|
226
|
-
|
|
227
272
|
const animationActive = Boolean(shouldAnimate) && !disableAnimation;
|
|
228
273
|
const historicalAnimationActive =
|
|
229
274
|
animationActive && !disableHistoricalAnimation;
|
|
@@ -236,30 +281,45 @@ export const ChartLines = memo(
|
|
|
236
281
|
// hiding, showing and added forecasts, and leaves the tooltip's order.
|
|
237
282
|
const historyZIndex = historyOnTop ? DefaultZIndexes.line + 1 : undefined;
|
|
238
283
|
|
|
239
|
-
const historyElements = historyLines.map(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
284
|
+
const historyElements = historyLines.map(
|
|
285
|
+
({ dataKey, name, color, connectNulls }) => {
|
|
286
|
+
const connected = connectNulls ?? connectHistoryNulls;
|
|
287
|
+
return (
|
|
288
|
+
<Line
|
|
289
|
+
key={dataKey}
|
|
290
|
+
zIndex={historyZIndex}
|
|
291
|
+
type="monotone"
|
|
292
|
+
dataKey={dataKey}
|
|
293
|
+
stroke={color}
|
|
294
|
+
strokeWidth={1}
|
|
295
|
+
// A line with no dot to draw skips the per-point call.
|
|
296
|
+
dot={
|
|
297
|
+
showEndDots || dotLonePoints
|
|
298
|
+
? props =>
|
|
299
|
+
renderDot(
|
|
300
|
+
props,
|
|
301
|
+
lastHistoryIndices[dataKey] ?? -1,
|
|
302
|
+
dataKey,
|
|
303
|
+
color,
|
|
304
|
+
connected,
|
|
305
|
+
)
|
|
306
|
+
: false
|
|
307
|
+
}
|
|
308
|
+
activeDot={{ ...activeDotHistorical, fill: color }}
|
|
309
|
+
name={name}
|
|
310
|
+
legendType={showLegend === false ? 'none' : undefined}
|
|
311
|
+
strokeDasharray="0"
|
|
312
|
+
connectNulls={connected}
|
|
313
|
+
hide={hiddenSeries?.has(dataKey)}
|
|
314
|
+
isAnimationActive={historicalAnimationActive}
|
|
315
|
+
animationDuration={animationDuration}
|
|
316
|
+
animateNewValues={valueAnimationEnabled}
|
|
317
|
+
animationBegin={0}
|
|
318
|
+
animationEasing="ease-out"
|
|
319
|
+
/>
|
|
320
|
+
);
|
|
321
|
+
},
|
|
322
|
+
);
|
|
263
323
|
|
|
264
324
|
const forecastElements = forecastData?.map(
|
|
265
325
|
(data: ForecastItemData, index: number) => {
|
|
@@ -269,6 +329,7 @@ export const ChartLines = memo(
|
|
|
269
329
|
color = FORECAST_LINE_COLORS[index % FORECAST_LINE_COLORS.length],
|
|
270
330
|
} = data;
|
|
271
331
|
const dataKey = `forecast_${id}`;
|
|
332
|
+
const connected = data.connectNulls ?? connectForecastNulls;
|
|
272
333
|
|
|
273
334
|
return (
|
|
274
335
|
<Line
|
|
@@ -279,12 +340,21 @@ export const ChartLines = memo(
|
|
|
279
340
|
strokeWidth={1}
|
|
280
341
|
strokeDasharray={forecastLineStyle === 'dashed' ? '4 2' : '0'}
|
|
281
342
|
dot={
|
|
282
|
-
showEndDots
|
|
343
|
+
showEndDots || dotLonePoints
|
|
344
|
+
? props =>
|
|
345
|
+
renderDot(
|
|
346
|
+
props,
|
|
347
|
+
lastForecastIndices[id] ?? -1,
|
|
348
|
+
dataKey,
|
|
349
|
+
color,
|
|
350
|
+
connected,
|
|
351
|
+
)
|
|
352
|
+
: false
|
|
283
353
|
}
|
|
284
354
|
activeDot={activeDotForecast(color)}
|
|
285
355
|
name={`${name || `${id}`}`}
|
|
286
356
|
legendType={showLegend === false ? 'none' : undefined}
|
|
287
|
-
connectNulls={
|
|
357
|
+
connectNulls={connected}
|
|
288
358
|
hide={hiddenSeries?.has(dataKey)}
|
|
289
359
|
isAnimationActive={forecastAnimationActive}
|
|
290
360
|
animationDuration={animationDuration}
|
|
@@ -23,6 +23,11 @@ export type HistoryItemData = {
|
|
|
23
23
|
* colour to both.
|
|
24
24
|
*/
|
|
25
25
|
color?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Draw this line across rows it has no value for. Without it the chart's
|
|
28
|
+
* `connectHistoryNulls` decides.
|
|
29
|
+
*/
|
|
30
|
+
connectNulls?: boolean;
|
|
26
31
|
};
|
|
27
32
|
|
|
28
33
|
/** Data key of one history line in `chartData` rows. */
|
|
@@ -19,6 +19,7 @@ StackedAreaChart shows how a total is built from parts over time: cost layers st
|
|
|
19
19
|
- Keep `margin.pct` in the host's state and pass it on every change; the margin layer stays mounted, so typing does not redraw the chart.
|
|
20
20
|
- Set `yearTicks` for multi-year monthly data so the axis reads one label per year.
|
|
21
21
|
- Pass `hiddenKeys` and `onToggleKey` when the host must remember which series are hidden.
|
|
22
|
+
- Name the axes with `xAxisLabel` and `yAxisLabel`; pass `tooltipValueFormatter` when the tooltip should read differently from the axis (for example with a unit).
|
|
22
23
|
|
|
23
24
|
## Don't
|
|
24
25
|
|
|
@@ -196,31 +196,75 @@ function compareSides(
|
|
|
196
196
|
* A value axis from 0 for a top value: at most `maxIntervals` steps of
|
|
197
197
|
* 1, 2, 2.5 or 5 × 10ⁿ, with a little headroom above the top. Small changes
|
|
198
198
|
* in the top (a margin being typed) move the axis by one step at most,
|
|
199
|
-
* instead of doubling it the way automatic rounding can.
|
|
199
|
+
* instead of doubling it the way automatic rounding can. A `bottom` below 0
|
|
200
|
+
* (a negative layer or line) starts the axis at a step below it, as `min`.
|
|
200
201
|
*/
|
|
201
202
|
export function valueAxis(
|
|
202
203
|
top: number,
|
|
203
204
|
maxIntervals = 5,
|
|
204
205
|
headroom = 0.05,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
206
|
+
bottom = 0,
|
|
207
|
+
): { min?: number; max: number; ticks: number[] } {
|
|
208
|
+
const low = Number.isFinite(bottom) && bottom < 0 ? bottom * (1 + headroom) : 0;
|
|
209
|
+
const high =
|
|
210
|
+
Number.isFinite(top) && top > 0 ? top * (1 + headroom) : low < 0 ? 0 : 1;
|
|
211
|
+
let exponent = Math.floor(Math.log10((high - low) / maxIntervals));
|
|
208
212
|
for (;;) {
|
|
209
213
|
for (const mantissa of [1, 2, 2.5, 5]) {
|
|
210
214
|
const step = mantissa * 10 ** exponent;
|
|
211
|
-
const
|
|
212
|
-
|
|
215
|
+
const first = low < 0 ? Math.floor(low / step + 1e-9) * step : 0;
|
|
216
|
+
const count = Math.ceil((high - first) / step - 1e-9);
|
|
217
|
+
// Both ends round out to a step, so a range across 0 gets one more.
|
|
218
|
+
if (count <= maxIntervals + (low < 0 ? 1 : 0)) {
|
|
213
219
|
const ticks = Array.from({ length: count + 1 }, (_, i) =>
|
|
214
|
-
Number((i * step).toPrecision(12)),
|
|
220
|
+
Number((first + i * step).toPrecision(12)),
|
|
215
221
|
);
|
|
216
|
-
|
|
222
|
+
const max = ticks[ticks.length - 1];
|
|
223
|
+
return low < 0 ? { min: ticks[0], max, ticks } : { max, ticks };
|
|
217
224
|
}
|
|
218
225
|
}
|
|
219
226
|
exponent += 1;
|
|
220
227
|
}
|
|
221
228
|
}
|
|
222
229
|
|
|
223
|
-
/**
|
|
230
|
+
/**
|
|
231
|
+
* The lowest point the stack (a negative layer or margin pulls it below 0)
|
|
232
|
+
* or a line reaches; 0 when nothing is negative.
|
|
233
|
+
*/
|
|
234
|
+
export function chartBottom(
|
|
235
|
+
rows: readonly StackedAreaChartDatum[],
|
|
236
|
+
layerKeys: readonly string[],
|
|
237
|
+
lineKeys: readonly string[],
|
|
238
|
+
): number {
|
|
239
|
+
let bottom = 0;
|
|
240
|
+
for (const row of rows) {
|
|
241
|
+
bottom = Math.min(bottom, stackExtent(row, layerKeys)[0]);
|
|
242
|
+
for (const key of lineKeys) bottom = Math.min(bottom, num(row[key]) ?? 0);
|
|
243
|
+
}
|
|
244
|
+
return bottom;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The lowest and highest running totals up a row's stack (layers, then the
|
|
249
|
+
* margin): recharts draws each layer from the total below it, so a negative
|
|
250
|
+
* layer above a positive one leaves the peak below the final total.
|
|
251
|
+
*/
|
|
252
|
+
function stackExtent(
|
|
253
|
+
row: StackedAreaChartDatum,
|
|
254
|
+
layerKeys: readonly string[],
|
|
255
|
+
): [number, number] {
|
|
256
|
+
let running = 0;
|
|
257
|
+
let low = 0;
|
|
258
|
+
let high = 0;
|
|
259
|
+
for (const key of [...layerKeys, MARGIN_KEY]) {
|
|
260
|
+
running += num(row[key]) ?? 0;
|
|
261
|
+
low = Math.min(low, running);
|
|
262
|
+
high = Math.max(high, running);
|
|
263
|
+
}
|
|
264
|
+
return [low, high];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The highest point the stack (layers plus margin) or a line reaches. */
|
|
224
268
|
export function chartTop(
|
|
225
269
|
rows: readonly StackedAreaChartDatum[],
|
|
226
270
|
layerKeys: readonly string[],
|
|
@@ -228,10 +272,7 @@ export function chartTop(
|
|
|
228
272
|
): number {
|
|
229
273
|
let top = 0;
|
|
230
274
|
for (const row of rows) {
|
|
231
|
-
top = Math.max(
|
|
232
|
-
top,
|
|
233
|
-
layersTotal(row, layerKeys) + (num(row[MARGIN_KEY]) ?? 0),
|
|
234
|
-
);
|
|
275
|
+
top = Math.max(top, stackExtent(row, layerKeys)[1]);
|
|
235
276
|
for (const key of lineKeys) top = Math.max(top, num(row[key]) ?? 0);
|
|
236
277
|
}
|
|
237
278
|
return top;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import cn from 'classnames';
|
|
2
2
|
import { CSSProperties } from 'react';
|
|
3
3
|
|
|
4
|
-
import type { ChartConfig } from '#uilib/components/ui/Chart';
|
|
5
4
|
import ChartS from '#uilib/components/ui/Chart/Chart.styl';
|
|
5
|
+
import type { ChartConfig } from '#uilib/components/ui/Chart/Chart.types';
|
|
6
6
|
|
|
7
7
|
export type TooltipRow = {
|
|
8
8
|
dataKey?: unknown;
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import cn from 'classnames';
|
|
2
2
|
import { ReactNode, forwardRef, useMemo } from 'react';
|
|
3
3
|
|
|
4
|
-
import type { ChartConfig } from '#uilib/components/ui/Chart';
|
|
5
|
-
import { ChartContainer, ChartTooltip } from '#uilib/components/ui/Chart';
|
|
6
4
|
import ChartS from '#uilib/components/ui/Chart/Chart.styl';
|
|
5
|
+
import type { ChartConfig } from '#uilib/components/ui/Chart/Chart.types';
|
|
7
6
|
import { getChartSeriesColor } from '#uilib/components/ui/Chart/chartSeriesPalette';
|
|
7
|
+
// Straight from the components, not the Chart index, which also loads the
|
|
8
|
+
// canvas chart (lightweight-charts) and its styles.
|
|
9
|
+
import { ChartContainer } from '#uilib/components/ui/Chart/components/ChartContainer';
|
|
10
|
+
import { ChartPlacedTooltip as ChartTooltip } from '#uilib/components/ui/Chart/components/ChartPlacedTooltip';
|
|
8
11
|
import { useLegendState } from '#uilib/components/ui/Chart/hooks/useLegendState';
|
|
9
12
|
import {
|
|
10
13
|
Area,
|
|
11
14
|
Bar,
|
|
12
15
|
CartesianGrid,
|
|
13
16
|
ComposedChart,
|
|
17
|
+
Label,
|
|
14
18
|
Line,
|
|
15
19
|
ReferenceArea,
|
|
16
20
|
XAxis,
|
|
@@ -19,6 +23,7 @@ import {
|
|
|
19
23
|
|
|
20
24
|
import {
|
|
21
25
|
MARGIN_KEY,
|
|
26
|
+
chartBottom,
|
|
22
27
|
chartTop,
|
|
23
28
|
compareRowZones,
|
|
24
29
|
compareZones,
|
|
@@ -59,6 +64,8 @@ const splitKeys = (joined: string) => joined.split(KEY_SEP).slice(1);
|
|
|
59
64
|
const ZONE_Z_INDEX = 50;
|
|
60
65
|
|
|
61
66
|
const AXIS_LINE = { stroke: 'var(--border)', strokeWidth: 1 };
|
|
67
|
+
/** Room for an x-axis title under the ticks (recharts' default is 30). */
|
|
68
|
+
const X_AXIS_HEIGHT_WITH_LABEL = 44;
|
|
62
69
|
|
|
63
70
|
const layerColor = (layer: StackedAreaChartLayer, index: number) =>
|
|
64
71
|
layer.color ?? getChartSeriesColor(index);
|
|
@@ -118,8 +125,11 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
118
125
|
showTooltip = true,
|
|
119
126
|
showGrid = true,
|
|
120
127
|
valueFormatter,
|
|
128
|
+
tooltipValueFormatter = valueFormatter,
|
|
121
129
|
xTickFormatter,
|
|
122
130
|
tooltipLabelFormatter,
|
|
131
|
+
xAxisLabel,
|
|
132
|
+
yAxisLabel,
|
|
123
133
|
height,
|
|
124
134
|
...props
|
|
125
135
|
},
|
|
@@ -181,11 +191,15 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
181
191
|
const visibleLinesJoined = joinKeys(
|
|
182
192
|
overlays.filter(o => !hidden.has(o.key)).map(o => o.key),
|
|
183
193
|
);
|
|
184
|
-
const yAxis = useMemo(
|
|
185
|
-
()
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
194
|
+
const yAxis = useMemo(() => {
|
|
195
|
+
const lines = splitKeys(visibleLinesJoined);
|
|
196
|
+
return valueAxis(
|
|
197
|
+
chartTop(rows, visibleKeys, lines),
|
|
198
|
+
undefined,
|
|
199
|
+
undefined,
|
|
200
|
+
chartBottom(rows, visibleKeys, lines),
|
|
201
|
+
);
|
|
202
|
+
}, [rows, visibleKeys, visibleLinesJoined]);
|
|
189
203
|
const ticks = useMemo(
|
|
190
204
|
() => (yearTicks ? findYearTicks(data, xKey) : undefined),
|
|
191
205
|
[yearTicks, data, xKey],
|
|
@@ -240,18 +254,28 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
240
254
|
minTickGap={ticks ? 8 : 24}
|
|
241
255
|
tickLine={false}
|
|
242
256
|
axisLine={AXIS_LINE}
|
|
243
|
-
|
|
257
|
+
height={xAxisLabel ? X_AXIS_HEIGHT_WITH_LABEL : undefined}
|
|
258
|
+
>
|
|
259
|
+
{xAxisLabel && (
|
|
260
|
+
<Label value={xAxisLabel} position="insideBottom" offset={0} />
|
|
261
|
+
)}
|
|
262
|
+
</XAxis>
|
|
244
263
|
<YAxis
|
|
245
264
|
// Our own steps: automatic rounding can double the axis when the
|
|
246
265
|
// margin nudges the top past a round number.
|
|
247
|
-
domain={[0, yAxis.max]}
|
|
266
|
+
domain={[yAxis.min ?? 0, yAxis.max]}
|
|
248
267
|
ticks={yAxis.ticks}
|
|
249
268
|
allowDataOverflow
|
|
250
269
|
tickFormatter={valueFormatter}
|
|
251
|
-
|
|
270
|
+
// Room for the rotated title beside the ticks.
|
|
271
|
+
width={yAxisLabel ? 72 : 56}
|
|
252
272
|
tickLine={false}
|
|
253
273
|
axisLine={AXIS_LINE}
|
|
254
|
-
|
|
274
|
+
>
|
|
275
|
+
{yAxisLabel && (
|
|
276
|
+
<Label value={yAxisLabel} angle={-90} position="insideLeft" />
|
|
277
|
+
)}
|
|
278
|
+
</YAxis>
|
|
255
279
|
{showTooltip && (
|
|
256
280
|
<ChartTooltip
|
|
257
281
|
content={tooltip => (
|
|
@@ -260,7 +284,7 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
260
284
|
payload={tooltip.payload as ReadonlyArray<TooltipRow>}
|
|
261
285
|
label={tooltip.label}
|
|
262
286
|
config={config}
|
|
263
|
-
valueFormatter={
|
|
287
|
+
valueFormatter={tooltipValueFormatter}
|
|
264
288
|
labelFormatter={tooltipLabelFormatter}
|
|
265
289
|
/>
|
|
266
290
|
)}
|
|
@@ -88,6 +88,15 @@ export interface StackedAreaChartProps extends Omit<
|
|
|
88
88
|
showGrid?: boolean;
|
|
89
89
|
/** Formats numeric y-axis ticks and tooltip values. */
|
|
90
90
|
valueFormatter?: (value: number) => string;
|
|
91
|
+
/**
|
|
92
|
+
* Formats the tooltip's values (for example with a unit), when they should
|
|
93
|
+
* read differently from the axis. Defaults to `valueFormatter`.
|
|
94
|
+
*/
|
|
95
|
+
tooltipValueFormatter?: (value: number) => string;
|
|
96
|
+
/** Title under the x axis. */
|
|
97
|
+
xAxisLabel?: string;
|
|
98
|
+
/** Title along the y axis. */
|
|
99
|
+
yAxisLabel?: string;
|
|
91
100
|
/** Formats x-axis ticks (ignored for year ticks, which show the year). */
|
|
92
101
|
xTickFormatter?: (value: string | number) => string;
|
|
93
102
|
/** Formats the tooltip's x label. */
|