@sybilion/uilib 1.8.3 → 1.8.5
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 +39 -9
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.styl.js +2 -2
- 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 +14 -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 +2 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +54 -13
- package/src/components/ui/StackedAreaChart/StackedAreaChart.styl +3 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.styl.d.ts +1 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +1 -1
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +80 -13
- package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +14 -0
- package/src/docs/pages/StackedAreaChartPage.tsx +30 -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. */
|
|
@@ -17,8 +17,10 @@ StackedAreaChart shows how a total is built from parts over time: cost layers st
|
|
|
17
17
|
- Expect zones to change at rows: a crossing between two rows shows at the next row. Hiding a layer or the compare line in the legend updates the margin, the zones and the weights to match what is drawn.
|
|
18
18
|
- Give each layer a `label`; the legend and tooltip use it.
|
|
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
|
+
- Set `dotLonePoints` on an overlay that can have a single value in the window (for example one price paid): a bridged line with one value draws nothing, so it gets a dot.
|
|
20
21
|
- Set `yearTicks` for multi-year monthly data so the axis reads one label per year.
|
|
21
22
|
- Pass `hiddenKeys` and `onToggleKey` when the host must remember which series are hidden.
|
|
23
|
+
- Name the axes with `xAxisLabel` and `yAxisLabel`; pass `tooltipValueFormatter` when the tooltip should read differently from the axis (for example with a unit).
|
|
22
24
|
|
|
23
25
|
## Don't
|
|
24
26
|
|
|
@@ -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,11 +64,14 @@ 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);
|
|
65
72
|
const overlayColor = (overlay: StackedAreaChartOverlay) =>
|
|
66
73
|
overlay.color ?? DEFAULT_OVERLAY_COLOR;
|
|
74
|
+
const LONE_DOT_RADIUS = 3;
|
|
67
75
|
|
|
68
76
|
function LegendEntry({
|
|
69
77
|
swatch,
|
|
@@ -118,8 +126,11 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
118
126
|
showTooltip = true,
|
|
119
127
|
showGrid = true,
|
|
120
128
|
valueFormatter,
|
|
129
|
+
tooltipValueFormatter = valueFormatter,
|
|
121
130
|
xTickFormatter,
|
|
122
131
|
tooltipLabelFormatter,
|
|
132
|
+
xAxisLabel,
|
|
133
|
+
yAxisLabel,
|
|
123
134
|
height,
|
|
124
135
|
...props
|
|
125
136
|
},
|
|
@@ -168,6 +179,18 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
168
179
|
[showWeights, data, visibleKeys],
|
|
169
180
|
);
|
|
170
181
|
const asBars = rows.length < barsBelow;
|
|
182
|
+
// How many values each overlay has: a bridged line with one value draws
|
|
183
|
+
// nothing, so `dotLonePoints` dots it.
|
|
184
|
+
const overlayValueCounts = useMemo(() => {
|
|
185
|
+
const counts: Record<string, number> = {};
|
|
186
|
+
for (const overlay of overlays) {
|
|
187
|
+
counts[overlay.key] = data.filter(row => {
|
|
188
|
+
const value = row[overlay.key];
|
|
189
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
190
|
+
}).length;
|
|
191
|
+
}
|
|
192
|
+
return counts;
|
|
193
|
+
}, [data, overlays]);
|
|
171
194
|
const compareKey = overlays.find(o => o.compare)?.key;
|
|
172
195
|
const zonesOn = compareKey != null && !hidden.has(compareKey);
|
|
173
196
|
const zones = useMemo(() => {
|
|
@@ -181,11 +204,15 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
181
204
|
const visibleLinesJoined = joinKeys(
|
|
182
205
|
overlays.filter(o => !hidden.has(o.key)).map(o => o.key),
|
|
183
206
|
);
|
|
184
|
-
const yAxis = useMemo(
|
|
185
|
-
()
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
207
|
+
const yAxis = useMemo(() => {
|
|
208
|
+
const lines = splitKeys(visibleLinesJoined);
|
|
209
|
+
return valueAxis(
|
|
210
|
+
chartTop(rows, visibleKeys, lines),
|
|
211
|
+
undefined,
|
|
212
|
+
undefined,
|
|
213
|
+
chartBottom(rows, visibleKeys, lines),
|
|
214
|
+
);
|
|
215
|
+
}, [rows, visibleKeys, visibleLinesJoined]);
|
|
189
216
|
const ticks = useMemo(
|
|
190
217
|
() => (yearTicks ? findYearTicks(data, xKey) : undefined),
|
|
191
218
|
[yearTicks, data, xKey],
|
|
@@ -240,18 +267,28 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
240
267
|
minTickGap={ticks ? 8 : 24}
|
|
241
268
|
tickLine={false}
|
|
242
269
|
axisLine={AXIS_LINE}
|
|
243
|
-
|
|
270
|
+
height={xAxisLabel ? X_AXIS_HEIGHT_WITH_LABEL : undefined}
|
|
271
|
+
>
|
|
272
|
+
{xAxisLabel && (
|
|
273
|
+
<Label value={xAxisLabel} position="insideBottom" offset={0} />
|
|
274
|
+
)}
|
|
275
|
+
</XAxis>
|
|
244
276
|
<YAxis
|
|
245
277
|
// Our own steps: automatic rounding can double the axis when the
|
|
246
278
|
// margin nudges the top past a round number.
|
|
247
|
-
domain={[0, yAxis.max]}
|
|
279
|
+
domain={[yAxis.min ?? 0, yAxis.max]}
|
|
248
280
|
ticks={yAxis.ticks}
|
|
249
281
|
allowDataOverflow
|
|
250
282
|
tickFormatter={valueFormatter}
|
|
251
|
-
|
|
283
|
+
// Room for the rotated title beside the ticks.
|
|
284
|
+
width={yAxisLabel ? 72 : 56}
|
|
252
285
|
tickLine={false}
|
|
253
286
|
axisLine={AXIS_LINE}
|
|
254
|
-
|
|
287
|
+
>
|
|
288
|
+
{yAxisLabel && (
|
|
289
|
+
<Label value={yAxisLabel} angle={-90} position="insideLeft" />
|
|
290
|
+
)}
|
|
291
|
+
</YAxis>
|
|
255
292
|
{showTooltip && (
|
|
256
293
|
<ChartTooltip
|
|
257
294
|
content={tooltip => (
|
|
@@ -260,7 +297,7 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
260
297
|
payload={tooltip.payload as ReadonlyArray<TooltipRow>}
|
|
261
298
|
label={tooltip.label}
|
|
262
299
|
config={config}
|
|
263
|
-
valueFormatter={
|
|
300
|
+
valueFormatter={tooltipValueFormatter}
|
|
264
301
|
labelFormatter={tooltipLabelFormatter}
|
|
265
302
|
/>
|
|
266
303
|
)}
|
|
@@ -327,6 +364,10 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
327
364
|
))}
|
|
328
365
|
{overlays.map(overlay => {
|
|
329
366
|
const color = overlayColor(overlay);
|
|
367
|
+
const lone =
|
|
368
|
+
!asBars &&
|
|
369
|
+
overlay.dotLonePoints &&
|
|
370
|
+
overlayValueCounts[overlay.key] === 1;
|
|
330
371
|
return (
|
|
331
372
|
<Line
|
|
332
373
|
key={overlay.key}
|
|
@@ -335,7 +376,33 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
335
376
|
name={overlay.key}
|
|
336
377
|
stroke={color}
|
|
337
378
|
strokeWidth={1.75}
|
|
338
|
-
dot={
|
|
379
|
+
dot={
|
|
380
|
+
asBars
|
|
381
|
+
? { r: 4, fill: color, stroke: color }
|
|
382
|
+
: lone
|
|
383
|
+
? (props: {
|
|
384
|
+
cx?: number;
|
|
385
|
+
cy?: number;
|
|
386
|
+
index?: number;
|
|
387
|
+
value?: unknown;
|
|
388
|
+
}) =>
|
|
389
|
+
typeof props.value === 'number' &&
|
|
390
|
+
Number.isFinite(props.value) &&
|
|
391
|
+
props.cx != null &&
|
|
392
|
+
props.cy != null ? (
|
|
393
|
+
<circle
|
|
394
|
+
key={`lone-${overlay.key}-${props.index}`}
|
|
395
|
+
className={S.loneDot}
|
|
396
|
+
cx={props.cx}
|
|
397
|
+
cy={props.cy}
|
|
398
|
+
r={LONE_DOT_RADIUS}
|
|
399
|
+
fill={color}
|
|
400
|
+
/>
|
|
401
|
+
) : (
|
|
402
|
+
<g key={`lone-${overlay.key}-${props.index}`} />
|
|
403
|
+
)
|
|
404
|
+
: false
|
|
405
|
+
}
|
|
339
406
|
activeDot={false}
|
|
340
407
|
connectNulls
|
|
341
408
|
isAnimationActive={false}
|
|
@@ -27,6 +27,11 @@ export interface StackedAreaChartOverlay {
|
|
|
27
27
|
* line. At most one overlay should set it; the first one that does is used.
|
|
28
28
|
*/
|
|
29
29
|
compare?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Dot the line's only value. Gaps are bridged, so a line with a single
|
|
32
|
+
* value would otherwise draw nothing.
|
|
33
|
+
*/
|
|
34
|
+
dotLonePoints?: boolean;
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
export interface StackedAreaChartMargin {
|
|
@@ -88,6 +93,15 @@ export interface StackedAreaChartProps extends Omit<
|
|
|
88
93
|
showGrid?: boolean;
|
|
89
94
|
/** Formats numeric y-axis ticks and tooltip values. */
|
|
90
95
|
valueFormatter?: (value: number) => string;
|
|
96
|
+
/**
|
|
97
|
+
* Formats the tooltip's values (for example with a unit), when they should
|
|
98
|
+
* read differently from the axis. Defaults to `valueFormatter`.
|
|
99
|
+
*/
|
|
100
|
+
tooltipValueFormatter?: (value: number) => string;
|
|
101
|
+
/** Title under the x axis. */
|
|
102
|
+
xAxisLabel?: string;
|
|
103
|
+
/** Title along the y axis. */
|
|
104
|
+
yAxisLabel?: string;
|
|
91
105
|
/** Formats x-axis ticks (ignored for year ticks, which show the year). */
|
|
92
106
|
xTickFormatter?: (value: string | number) => string;
|
|
93
107
|
/** Formats the tooltip's x label. */
|
|
@@ -48,6 +48,20 @@ const PAID: StackedAreaChartOverlay[] = [
|
|
|
48
48
|
{ key: 'paid', label: 'Prices paid', compare: true },
|
|
49
49
|
];
|
|
50
50
|
|
|
51
|
+
// One price paid in the window, next to a market line.
|
|
52
|
+
const ONE_PAID_ROWS: StackedAreaChartDatum[] = COST_ROWS.slice(24).map(
|
|
53
|
+
(row, i) => ({
|
|
54
|
+
...row,
|
|
55
|
+
paid: i === 7 ? 3.4 : null,
|
|
56
|
+
market: Number((3.1 + 0.2 * Math.sin(i / 2)).toFixed(2)),
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const ONE_PAID_OVERLAYS: StackedAreaChartOverlay[] = [
|
|
61
|
+
{ key: 'market', label: 'Market', color: 'var(--muted-foreground)' },
|
|
62
|
+
{ key: 'paid', label: 'Prices paid', dotLonePoints: true },
|
|
63
|
+
];
|
|
64
|
+
|
|
51
65
|
const QUARTER_ROWS: StackedAreaChartDatum[] = [
|
|
52
66
|
{
|
|
53
67
|
quarter: 'Q1',
|
|
@@ -121,6 +135,22 @@ export default function StackedAreaChartPage() {
|
|
|
121
135
|
height={320}
|
|
122
136
|
/>
|
|
123
137
|
</PageContentSection>
|
|
138
|
+
<PageContentSection>
|
|
139
|
+
<SectionHeader
|
|
140
|
+
title="A lone price"
|
|
141
|
+
description="A market line, and a single price paid shown as a dot with dotLonePoints (a line with one value draws nothing)."
|
|
142
|
+
withMargin
|
|
143
|
+
/>
|
|
144
|
+
<StackedAreaChart
|
|
145
|
+
data={ONE_PAID_ROWS}
|
|
146
|
+
xKey="date"
|
|
147
|
+
layers={COST_LAYERS}
|
|
148
|
+
overlays={ONE_PAID_OVERLAYS}
|
|
149
|
+
valueFormatter={formatPrice}
|
|
150
|
+
tooltipLabelFormatter={formatMonth}
|
|
151
|
+
height={260}
|
|
152
|
+
/>
|
|
153
|
+
</PageContentSection>
|
|
124
154
|
<PageContentSection>
|
|
125
155
|
<SectionHeader
|
|
126
156
|
title="Few points"
|