@torpor/ui 1.2.3 → 1.3.1
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/Charts/AreaChart.torp +79 -0
- package/dist/Charts/BarChart.torp +6 -4
- package/dist/Charts/Chart.torp +25 -8
- package/dist/Charts/ChartArea.torp +119 -0
- package/dist/Charts/ChartBars.torp +76 -28
- package/dist/Charts/ChartColumns.torp +74 -17
- package/dist/Charts/ChartGrid.torp +16 -12
- package/dist/Charts/ChartLegend.torp +111 -0
- package/dist/Charts/ChartLines.torp +41 -5
- package/dist/Charts/ChartPoints.torp +69 -0
- package/dist/Charts/ChartTypes.ts +16 -4
- package/dist/Charts/ChartXAxis.torp +67 -12
- package/dist/Charts/ChartYAxis.torp +47 -17
- package/dist/Charts/ColumnChart.torp +3 -1
- package/dist/Charts/DataSeries.ts +2 -1
- package/dist/Charts/LineChart.torp +3 -1
- package/dist/Charts/PieChart.torp +80 -10
- package/dist/Charts/ScatterChart.torp +3 -2
- package/dist/Charts/index.d.ts +157 -16
- package/dist/Charts/index.js +5 -1
- package/dist/utils/chartLayout.ts +137 -35
- package/dist/utils/chartUtils.ts +49 -4
- package/package.json +5 -5
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ClassValue, StyleValue } from "@torpor/view";
|
|
2
|
+
import Chart from "./Chart.torp";
|
|
3
|
+
import ChartArea from "./ChartArea.torp";
|
|
4
|
+
import ChartLines from "./ChartLines.torp";
|
|
5
|
+
import ChartXAxis from "./ChartXAxis.torp";
|
|
6
|
+
import ChartGrid from "./ChartGrid.torp";
|
|
7
|
+
import ChartYAxis from "./ChartYAxis.torp";
|
|
8
|
+
import type DataSeries from "./DataSeries";
|
|
9
|
+
|
|
10
|
+
interface AreaChartProps {
|
|
11
|
+
/** An ID for the root element */
|
|
12
|
+
id?: string;
|
|
13
|
+
/** Classes for the root element */
|
|
14
|
+
class?: ClassValue;
|
|
15
|
+
/** Styles for the root element */
|
|
16
|
+
style?: StyleValue;
|
|
17
|
+
/** An ARIA label describing the chart for screen readers */
|
|
18
|
+
ariaLabel?: string;
|
|
19
|
+
series: DataSeries[];
|
|
20
|
+
height?: number;
|
|
21
|
+
width?: number;
|
|
22
|
+
xLabel?: string;
|
|
23
|
+
yLabel?: string;
|
|
24
|
+
maxValue?: number;
|
|
25
|
+
stepCount?: number;
|
|
26
|
+
stepValue?: number;
|
|
27
|
+
showHLines?: boolean;
|
|
28
|
+
showVLines?: boolean;
|
|
29
|
+
/** The opacity of each area's fill (default 0.25) */
|
|
30
|
+
opacity?: number;
|
|
31
|
+
/** Stacks each series on top of the one below it */
|
|
32
|
+
stacked?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An area chart: each series drawn as its line with the region under it
|
|
37
|
+
* filled.
|
|
38
|
+
*
|
|
39
|
+
* This is a preset over the layered chart components:
|
|
40
|
+
*
|
|
41
|
+
* ```
|
|
42
|
+
* <Chart>
|
|
43
|
+
* <ChartYAxis />
|
|
44
|
+
* <ChartGrid />
|
|
45
|
+
* <ChartXAxis />
|
|
46
|
+
* <ChartArea seriesIndex={0} />
|
|
47
|
+
* <ChartLines seriesIndex={0} />
|
|
48
|
+
* </Chart>
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* Compose those directly to customize -- e.g. a line for a second series
|
|
52
|
+
* overlaid on the first's area, or `stacked` to pile the series up.
|
|
53
|
+
*/
|
|
54
|
+
export default function AreaChart($props: AreaChartProps) {
|
|
55
|
+
@render {
|
|
56
|
+
<Chart
|
|
57
|
+
{$props?.id}
|
|
58
|
+
class={["torp-area-chart", $props?.class]}
|
|
59
|
+
{$props?.style}
|
|
60
|
+
{$props?.ariaLabel}
|
|
61
|
+
{$props?.series}
|
|
62
|
+
{$props?.height}
|
|
63
|
+
{$props?.width}
|
|
64
|
+
{$props?.xLabel}
|
|
65
|
+
{$props?.yLabel}
|
|
66
|
+
{$props?.maxValue}
|
|
67
|
+
{$props?.stepCount}
|
|
68
|
+
{$props?.stepValue}
|
|
69
|
+
>
|
|
70
|
+
<ChartYAxis />
|
|
71
|
+
<ChartGrid hLines={$props.showHLines === true} vLines={$props.showVLines === true} />
|
|
72
|
+
<ChartXAxis />
|
|
73
|
+
@for (let i of $props.series.map((_, i) => i)) {
|
|
74
|
+
<ChartArea seriesIndex={i} opacity={$props.opacity} stacked={$props.stacked === true} />
|
|
75
|
+
<ChartLines seriesIndex={i} stacked={$props.stacked === true} />
|
|
76
|
+
}
|
|
77
|
+
</Chart>
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -25,12 +25,14 @@ interface BarChartProps {
|
|
|
25
25
|
stepValue?: number;
|
|
26
26
|
showHLines?: boolean;
|
|
27
27
|
showVLines?: boolean;
|
|
28
|
+
/** Stacks the values in each band into one bar per series */
|
|
29
|
+
stacked?: boolean;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
|
-
* A bar chart:
|
|
32
|
-
*
|
|
33
|
-
*
|
|
33
|
+
* A bar chart: horizontal bars drawn from the left edge towards their
|
|
34
|
+
* values, with category labels down the side and the value scale along the
|
|
35
|
+
* bottom.
|
|
34
36
|
*
|
|
35
37
|
* This is a preset over the layered chart components:
|
|
36
38
|
*
|
|
@@ -64,7 +66,7 @@ export default function BarChart($props: BarChartProps) {
|
|
|
64
66
|
<ChartYAxis />
|
|
65
67
|
<ChartGrid hLines={$props.showHLines === true} vLines={$props.showVLines === true} />
|
|
66
68
|
<ChartXAxis />
|
|
67
|
-
<ChartBars />
|
|
69
|
+
<ChartBars stacked={$props.stacked === true} />
|
|
68
70
|
</Chart>
|
|
69
71
|
}
|
|
70
72
|
}
|
package/dist/Charts/Chart.torp
CHANGED
|
@@ -19,9 +19,9 @@ interface ChartProps {
|
|
|
19
19
|
height?: number;
|
|
20
20
|
/** Fixed width in pixels (default: fills the container, minimum 400) */
|
|
21
21
|
width?: number;
|
|
22
|
-
/** Label under the x axis */
|
|
22
|
+
/** Label under the x axis, drawn by ChartXAxis */
|
|
23
23
|
xLabel?: string;
|
|
24
|
-
/** Label beside the y axis */
|
|
24
|
+
/** Label beside the y axis, drawn by ChartYAxis */
|
|
25
25
|
yLabel?: string;
|
|
26
26
|
/** The value at the top of the value axis, instead of the series' maximum */
|
|
27
27
|
maxValue?: number;
|
|
@@ -33,8 +33,8 @@ interface ChartProps {
|
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* A container for layered chart components. It measures its text, works out
|
|
36
|
-
* the
|
|
37
|
-
*
|
|
36
|
+
* the scales, and provides them to whatever axes, grid lines and marks are
|
|
37
|
+
* slotted inside:
|
|
38
38
|
*
|
|
39
39
|
* ```
|
|
40
40
|
* <Chart ariaLabel="Revenue" series={revenue}>
|
|
@@ -46,12 +46,20 @@ interface ChartProps {
|
|
|
46
46
|
* </Chart>
|
|
47
47
|
* ```
|
|
48
48
|
*
|
|
49
|
+
* Layers **reserve space** through the Chart's layout: adding a ChartXAxis
|
|
50
|
+
* claims a strip along the bottom, a ChartYAxis claims a strip on its side,
|
|
51
|
+
* a ChartLegend claims a strip for itself, and so on. The plot area is
|
|
52
|
+
* whatever space is left over, and the mark layers -- columns, bars, lines,
|
|
53
|
+
* areas, points -- draw only into that, overlapping each other freely. A
|
|
54
|
+
* Chart with no axes uses the whole svg; take one away and the marks grow
|
|
55
|
+
* into the space it gives back.
|
|
56
|
+
*
|
|
49
57
|
* Layers can be mixed freely -- columns with a line overlaid, an axis on
|
|
50
58
|
* either side, only a y axis with no x axis -- so a preset like LineChart is
|
|
51
59
|
* just one such combination.
|
|
52
60
|
*
|
|
53
|
-
* See also the ready-made presets: BarChart, ColumnChart,
|
|
54
|
-
* ScatterChart.
|
|
61
|
+
* See also the ready-made presets: AreaChart, BarChart, ColumnChart,
|
|
62
|
+
* LineChart, ScatterChart.
|
|
55
63
|
*/
|
|
56
64
|
export default function Chart($props: ChartProps | undefined) {
|
|
57
65
|
let container: HTMLElement;
|
|
@@ -61,8 +69,6 @@ export default function Chart($props: ChartProps | undefined) {
|
|
|
61
69
|
series: () => $props?.series ?? [],
|
|
62
70
|
height: () => $props?.height,
|
|
63
71
|
width: () => $props?.width,
|
|
64
|
-
xLabel: () => $props?.xLabel,
|
|
65
|
-
yLabel: () => $props?.yLabel,
|
|
66
72
|
maxValue: () => $props?.maxValue,
|
|
67
73
|
stepCount: () => $props?.stepCount,
|
|
68
74
|
stepValue: () => $props?.stepValue,
|
|
@@ -81,6 +87,8 @@ export default function Chart($props: ChartProps | undefined) {
|
|
|
81
87
|
|
|
82
88
|
let $chartState = $watch({
|
|
83
89
|
horizontal: false,
|
|
90
|
+
vertical: false,
|
|
91
|
+
linear: false,
|
|
84
92
|
});
|
|
85
93
|
|
|
86
94
|
const context: ChartContext = {
|
|
@@ -89,11 +97,20 @@ export default function Chart($props: ChartProps | undefined) {
|
|
|
89
97
|
xLabel: () => $props?.xLabel ?? "",
|
|
90
98
|
yLabel: () => $props?.yLabel ?? "",
|
|
91
99
|
horizontal: () => $chartState.horizontal,
|
|
100
|
+
vertical: () => $chartState.vertical,
|
|
101
|
+
linear: () => $chartState.linear,
|
|
92
102
|
registerMark: (orientation) => {
|
|
93
103
|
if (orientation === "horizontal") {
|
|
94
104
|
$chartState.horizontal = true;
|
|
105
|
+
} else if (orientation === "vertical") {
|
|
106
|
+
$chartState.vertical = true;
|
|
107
|
+
} else {
|
|
108
|
+
$chartState.linear = true;
|
|
95
109
|
}
|
|
96
110
|
},
|
|
111
|
+
registerMax: (id, value) => {
|
|
112
|
+
$layout.announceMax(id, value);
|
|
113
|
+
},
|
|
97
114
|
colorAt,
|
|
98
115
|
};
|
|
99
116
|
$context[ChartContextName] = context;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
|
+
import type DataSeries from "./DataSeries";
|
|
3
|
+
import { calculateCrossStackMax } from "../utils/chartUtils";
|
|
4
|
+
|
|
5
|
+
interface ChartAreaProps {
|
|
6
|
+
/** The index of the series to draw (default 0) */
|
|
7
|
+
seriesIndex?: number;
|
|
8
|
+
/** The color to draw with, instead of the series' own */
|
|
9
|
+
color?: string;
|
|
10
|
+
/** The fill opacity (default 0.25) */
|
|
11
|
+
opacity?: number;
|
|
12
|
+
/** Stacks this series on top of the ones below it, instead of from the baseline */
|
|
13
|
+
stacked?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Area marks for a Chart: fills the region between the chosen series' line
|
|
18
|
+
* and the bottom of the plot. Add a ChartLines layer with the same
|
|
19
|
+
* seriesIndex to draw the line itself.
|
|
20
|
+
*
|
|
21
|
+
* With `stacked`, each area sits on top of the stacked series below it --
|
|
22
|
+
* give every stacked area the same treatment and the Chart's value domain
|
|
23
|
+
* grows to fit the largest total.
|
|
24
|
+
*/
|
|
25
|
+
export default function ChartArea($props?: ChartAreaProps) {
|
|
26
|
+
const context = $context[ChartContextName] as ChartContext;
|
|
27
|
+
if (!context) {
|
|
28
|
+
throw new Error("This layer must be contained within a Chart");
|
|
29
|
+
}
|
|
30
|
+
const { layout, series, colorAt, registerMax } = context;
|
|
31
|
+
context.registerMark("linear");
|
|
32
|
+
|
|
33
|
+
function index(): number {
|
|
34
|
+
return $props?.seriesIndex ?? 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function stacked(): boolean {
|
|
38
|
+
return $props?.stacked === true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Stacked areas reach higher than any single series, so raise the
|
|
42
|
+
// domain to the largest stack total (released when this layer unmounts
|
|
43
|
+
// or stops stacking)
|
|
44
|
+
$run(() => {
|
|
45
|
+
if (stacked()) {
|
|
46
|
+
registerMax("area-" + index(), calculateCrossStackMax(series()));
|
|
47
|
+
}
|
|
48
|
+
return () => registerMax("area-" + index(), 0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function current(): DataSeries | undefined {
|
|
52
|
+
return series()[index()];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The sum of the series below this one, at point j
|
|
56
|
+
function below(j: number): number {
|
|
57
|
+
if (!stacked()) {
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
let total = 0;
|
|
61
|
+
for (let i = 0; i < index(); i++) {
|
|
62
|
+
total += series()[i]?.data[j] ?? 0;
|
|
63
|
+
}
|
|
64
|
+
return total;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function points(): { x: number; y: number }[] {
|
|
68
|
+
const data = current()?.data ?? [];
|
|
69
|
+
// Points are spread by their index across the full plot width, from
|
|
70
|
+
// the left edge to the right
|
|
71
|
+
const step = data.length > 1 ? layout.plotWidth / (data.length - 1) : 0;
|
|
72
|
+
return data.map((value, j) => ({
|
|
73
|
+
x: +(layout.plotX + (data.length > 1 ? j * step : layout.plotWidth / 2)).toFixed(2),
|
|
74
|
+
y: +(layout.plotY + layout.plotHeight - (below(j) + value) * layout.valueHeight).toFixed(2),
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The polygon: out along the top edge, back along the stack below (or
|
|
79
|
+
// the bottom of the plot for the first series)
|
|
80
|
+
function polygon(): string {
|
|
81
|
+
const pts = points();
|
|
82
|
+
if (!pts.length) {
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
if (stacked()) {
|
|
86
|
+
const lower = pts
|
|
87
|
+
.map((p, j) => ({
|
|
88
|
+
x: p.x,
|
|
89
|
+
y: +(layout.plotY + layout.plotHeight - below(j) * layout.valueHeight).toFixed(2),
|
|
90
|
+
}))
|
|
91
|
+
.reverse();
|
|
92
|
+
return [...pts, ...lower].map((p) => `${p.x},${p.y}`).join(" ");
|
|
93
|
+
}
|
|
94
|
+
const bottom = layout.plotY + layout.plotHeight;
|
|
95
|
+
return [
|
|
96
|
+
...pts,
|
|
97
|
+
{ x: pts[pts.length - 1].x, y: bottom },
|
|
98
|
+
{ x: pts[0].x, y: bottom },
|
|
99
|
+
]
|
|
100
|
+
.map((p) => `${p.x},${p.y}`)
|
|
101
|
+
.join(" ");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function fill(): string {
|
|
105
|
+
return $props?.color || colorAt(index());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function opacity(): number {
|
|
109
|
+
return $props?.opacity ?? 0.25;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@render {
|
|
113
|
+
<svg>
|
|
114
|
+
@if (points().length) {
|
|
115
|
+
<polygon points={polygon()} fill={fill()} fill-opacity={opacity()} stroke="none" />
|
|
116
|
+
}
|
|
117
|
+
</svg>
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
|
+
import { calculateStackMax } from "../utils/chartUtils";
|
|
2
3
|
|
|
3
4
|
interface ChartBarsProps {
|
|
4
5
|
/**
|
|
@@ -6,11 +7,17 @@ interface ChartBarsProps {
|
|
|
6
7
|
* reading different series. Defaults to all series.
|
|
7
8
|
*/
|
|
8
9
|
seriesIndex?: number;
|
|
10
|
+
/** Stacks the values in each band into one bar, instead of side by side */
|
|
11
|
+
stacked?: boolean;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
|
-
* Horizontal bar marks for a Chart:
|
|
13
|
-
* drawn
|
|
15
|
+
* Horizontal bar marks for a Chart: every value in the chosen series is
|
|
16
|
+
* drawn as a horizontal bar, grouped within its band down the left.
|
|
17
|
+
*
|
|
18
|
+
* With `stacked`, the values in each band are laid end to end into a
|
|
19
|
+
* single bar instead, and the Chart's value domain grows to fit the
|
|
20
|
+
* largest total.
|
|
14
21
|
*
|
|
15
22
|
* For a column per value grouped under each label, use ChartColumns --
|
|
16
23
|
* those run vertically.
|
|
@@ -20,7 +27,7 @@ export default function ChartBars($props?: ChartBarsProps) {
|
|
|
20
27
|
if (!context) {
|
|
21
28
|
throw new Error("This layer must be contained within a Chart");
|
|
22
29
|
}
|
|
23
|
-
const { layout, series, colorAt } = context;
|
|
30
|
+
const { layout, series, colorAt, registerMax } = context;
|
|
24
31
|
context.registerMark("horizontal");
|
|
25
32
|
|
|
26
33
|
function indexes(): number[] {
|
|
@@ -30,48 +37,89 @@ export default function ChartBars($props?: ChartBarsProps) {
|
|
|
30
37
|
return series().map((_, i) => i);
|
|
31
38
|
}
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const count = Math.max(1, layout.labels.length);
|
|
36
|
-
return (layout.chartBottom - topPad()) / count;
|
|
40
|
+
function stacked(): boolean {
|
|
41
|
+
return $props?.stacked === true;
|
|
37
42
|
}
|
|
38
43
|
|
|
44
|
+
// Stacked bars reach further than any single value, so raise the
|
|
45
|
+
// domain to the largest total (released when this layer unmounts or
|
|
46
|
+
// stops stacking)
|
|
47
|
+
$run(() => {
|
|
48
|
+
if (stacked()) {
|
|
49
|
+
registerMax("bars", calculateStackMax(series()));
|
|
50
|
+
}
|
|
51
|
+
return () => registerMax("bars", 0);
|
|
52
|
+
});
|
|
53
|
+
|
|
39
54
|
// A little space above for breathing room, matching ChartXAxis's labels
|
|
40
55
|
function topPad(): number {
|
|
41
56
|
return layout.textHeight / 3;
|
|
42
57
|
}
|
|
43
58
|
|
|
44
|
-
|
|
59
|
+
// The vertical band each series occupies within the plot
|
|
60
|
+
function bandHeight(): number {
|
|
61
|
+
const count = Math.max(1, layout.labels.length);
|
|
62
|
+
return (layout.plotHeight - topPad()) / count;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function bandY(i: number): number {
|
|
66
|
+
return layout.plotY + topPad() + i * bandHeight();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function barThickness(count: number): number {
|
|
45
70
|
return Math.max(1, (bandHeight() / count) * 0.7);
|
|
46
71
|
}
|
|
47
72
|
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
const max = Math.max(1, layout.maxValue);
|
|
51
|
-
return Math.max(1, (value / max) * plotWidth);
|
|
73
|
+
function barGap(count: number): number {
|
|
74
|
+
return (bandHeight() - barThickness(count) * count) / (count + 1);
|
|
52
75
|
}
|
|
53
76
|
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
function unitWidth(): number {
|
|
78
|
+
return layout.plotWidth / Math.max(1, layout.maxValue);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The values of band i with the sum of the values before each
|
|
82
|
+
function segments(i: number): { value: number; before: number }[] {
|
|
83
|
+
let before = 0;
|
|
84
|
+
return series()[i].data.map((value) => {
|
|
85
|
+
const segment = { value, before };
|
|
86
|
+
before += value;
|
|
87
|
+
return segment;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The x/y/width/height of every bar in band i: sub-bars grouped within
|
|
92
|
+
// the band, or the band's values laid end to end into one bar
|
|
93
|
+
function rects(i: number): { x: number; y: number; width: number; height: number; value: number }[] {
|
|
94
|
+
if (stacked()) {
|
|
95
|
+
const height = barThickness(1);
|
|
96
|
+
const y = bandY(i) + (bandHeight() - height) / 2;
|
|
97
|
+
return segments(i).map((s) => ({
|
|
98
|
+
x: layout.plotX + s.before * unitWidth(),
|
|
99
|
+
y,
|
|
100
|
+
width: Math.max(1, s.value * unitWidth()),
|
|
101
|
+
height,
|
|
102
|
+
value: s.value,
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
105
|
+
const count = series()[i].data.length;
|
|
106
|
+
return series()[i].data.map((value, j) => ({
|
|
107
|
+
x: layout.plotX,
|
|
108
|
+
y: bandY(i) + barGap(count) + j * (barThickness(count) + barGap(count)),
|
|
109
|
+
width: Math.max(1, value * unitWidth()),
|
|
110
|
+
height: barThickness(count),
|
|
111
|
+
value,
|
|
112
|
+
}));
|
|
61
113
|
}
|
|
62
114
|
|
|
63
115
|
@render {
|
|
64
116
|
<svg>
|
|
65
117
|
@for (let i of indexes()) {
|
|
66
|
-
|
|
67
|
-
x={
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
fill={colorAt(i)}
|
|
72
|
-
>
|
|
73
|
-
<title>{`${[layout.labels[i], series()[i].data[0]].filter(Boolean).join("\n")}`}</title>
|
|
74
|
-
</rect>
|
|
118
|
+
@for (let r of rects(i)) {
|
|
119
|
+
<rect x={r.x} y={r.y} width={r.width} height={r.height} fill={colorAt(i)}>
|
|
120
|
+
<title>{`${[layout.labels[i], r.value].filter(Boolean).join("\n")}`}</title>
|
|
121
|
+
</rect>
|
|
122
|
+
}
|
|
75
123
|
}
|
|
76
124
|
</svg>
|
|
77
125
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
|
+
import { calculateStackMax } from "../utils/chartUtils";
|
|
2
3
|
|
|
3
4
|
interface ChartColumnsProps {
|
|
4
5
|
/**
|
|
@@ -6,12 +7,17 @@ interface ChartColumnsProps {
|
|
|
6
7
|
* reading different series. Defaults to all series.
|
|
7
8
|
*/
|
|
8
9
|
seriesIndex?: number;
|
|
10
|
+
/** Stacks the values in each slot into one column, instead of side by side */
|
|
11
|
+
stacked?: boolean;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Vertical column marks for a Chart: every value in the chosen series is
|
|
13
16
|
* drawn as a vertical column, grouped under its label.
|
|
14
17
|
*
|
|
18
|
+
* With `stacked`, the values in each slot are piled into a single column
|
|
19
|
+
* instead, and the Chart's value domain grows to fit the largest total.
|
|
20
|
+
*
|
|
15
21
|
* For one thick bar per series, use ChartBars -- those run horizontally.
|
|
16
22
|
*/
|
|
17
23
|
export default function ChartColumns($props?: ChartColumnsProps) {
|
|
@@ -19,7 +25,7 @@ export default function ChartColumns($props?: ChartColumnsProps) {
|
|
|
19
25
|
if (!context) {
|
|
20
26
|
throw new Error("This layer must be contained within a Chart");
|
|
21
27
|
}
|
|
22
|
-
const { layout, series, colorAt } = context;
|
|
28
|
+
const { layout, series, colorAt, registerMax } = context;
|
|
23
29
|
context.registerMark("vertical");
|
|
24
30
|
|
|
25
31
|
function indexes(): number[] {
|
|
@@ -29,6 +35,56 @@ export default function ChartColumns($props?: ChartColumnsProps) {
|
|
|
29
35
|
return series().map((_, i) => i);
|
|
30
36
|
}
|
|
31
37
|
|
|
38
|
+
function stacked(): boolean {
|
|
39
|
+
return $props?.stacked === true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Stacked columns reach higher than any single value, so raise the
|
|
43
|
+
// domain to the largest total (released when this layer unmounts or
|
|
44
|
+
// stops stacking)
|
|
45
|
+
$run(() => {
|
|
46
|
+
if (stacked()) {
|
|
47
|
+
registerMax("columns", calculateStackMax(series()));
|
|
48
|
+
}
|
|
49
|
+
return () => registerMax("columns", 0);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// The x/y/width/height of every rect in slot i: sub-columns side by
|
|
53
|
+
// side, or the slot's values stacked into one centered column
|
|
54
|
+
function rects(i: number): { x: number; y: number; width: number; height: number; value: number }[] {
|
|
55
|
+
const bottom = plotBottom();
|
|
56
|
+
if (stacked()) {
|
|
57
|
+
const width = columnWidth(1);
|
|
58
|
+
const x = slotX(i) + (layout.itemWidth - width) / 2;
|
|
59
|
+
return segments(i).map((s) => ({
|
|
60
|
+
x,
|
|
61
|
+
width,
|
|
62
|
+
y: bottom - (s.below + s.value) * layout.valueHeight,
|
|
63
|
+
// A small gap between the segments, so the stack reads
|
|
64
|
+
height: Math.max(1, s.value * layout.valueHeight - 2),
|
|
65
|
+
value: s.value,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
const count = series()[i].data.length;
|
|
69
|
+
const width = columnWidth(count);
|
|
70
|
+
return series()[i].data.map((value, j) => ({
|
|
71
|
+
x: columnX(i, j, count),
|
|
72
|
+
width,
|
|
73
|
+
y: bottom - Math.max(1, value * layout.valueHeight),
|
|
74
|
+
height: Math.max(1, value * layout.valueHeight),
|
|
75
|
+
value,
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The bottom edge of the plot, where columns start
|
|
80
|
+
function plotBottom(): number {
|
|
81
|
+
return layout.plotY + layout.plotHeight;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function slotX(i: number): number {
|
|
85
|
+
return layout.plotX + i * layout.itemWidth;
|
|
86
|
+
}
|
|
87
|
+
|
|
32
88
|
function columnWidth(count: number): number {
|
|
33
89
|
return Math.max(1, (layout.itemWidth / count) * 0.7);
|
|
34
90
|
}
|
|
@@ -37,27 +93,28 @@ export default function ChartColumns($props?: ChartColumnsProps) {
|
|
|
37
93
|
return (layout.itemWidth - columnWidth(count) * count) / (count + 1);
|
|
38
94
|
}
|
|
39
95
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
96
|
+
// The x of sub-column j of `count` within slot i: the columns are
|
|
97
|
+
// separated from each other and from the slot edges by the same gap
|
|
98
|
+
function columnX(i: number, j: number, count: number): number {
|
|
99
|
+
return slotX(i) + columnGap(count) + j * (columnWidth(count) + columnGap(count));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// The values of slot i with the sum of the values below each
|
|
103
|
+
function segments(i: number): { value: number; below: number }[] {
|
|
104
|
+
let below = 0;
|
|
105
|
+
return series()[i].data.map((value) => {
|
|
106
|
+
const segment = { value, below };
|
|
107
|
+
below += value;
|
|
108
|
+
return segment;
|
|
109
|
+
});
|
|
47
110
|
}
|
|
48
111
|
|
|
49
112
|
@render {
|
|
50
113
|
<svg>
|
|
51
114
|
@for (let i of indexes()) {
|
|
52
|
-
@for (let
|
|
53
|
-
<rect
|
|
54
|
-
|
|
55
|
-
y={layout.chartBottom - Math.max(1, value * layout.valueHeight)}
|
|
56
|
-
width={columnWidth(series()[i].data.length)}
|
|
57
|
-
height={Math.max(1, value * layout.valueHeight)}
|
|
58
|
-
fill={colorAt(i)}
|
|
59
|
-
>
|
|
60
|
-
<title>{`${[layout.labels[i], value].filter(Boolean).join("\n")}`}</title>
|
|
115
|
+
@for (let r of rects(i)) {
|
|
116
|
+
<rect x={r.x} y={r.y} width={r.width} height={r.height} fill={colorAt(i)}>
|
|
117
|
+
<title>{`${[layout.labels[i], r.value].filter(Boolean).join("\n")}`}</title>
|
|
61
118
|
</rect>
|
|
62
119
|
}
|
|
63
120
|
}
|
|
@@ -9,7 +9,9 @@ interface ChartGridProps {
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Grid lines for a Chart: horizontal lines at each value step and/or
|
|
12
|
-
* vertical lines through each category slot.
|
|
12
|
+
* vertical lines through each category slot. Drawn only within the plot
|
|
13
|
+
* area, inside the space reserved by the axes. The baseline (value 0) is
|
|
14
|
+
* left to the axis line.
|
|
13
15
|
*/
|
|
14
16
|
export default function ChartGrid($props?: ChartGridProps) {
|
|
15
17
|
const context = $context[ChartContextName] as ChartContext;
|
|
@@ -29,22 +31,24 @@ export default function ChartGrid($props?: ChartGridProps) {
|
|
|
29
31
|
<svg>
|
|
30
32
|
@if (hLines()) {
|
|
31
33
|
@for (let [i] of layout.stepLabels.entries()) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
@if (i > 0) {
|
|
35
|
+
<line
|
|
36
|
+
x1={layout.plotX}
|
|
37
|
+
y1={layout.plotY + layout.plotHeight - i * layout.stepValue * layout.valueHeight}
|
|
38
|
+
x2={layout.plotX + layout.plotWidth}
|
|
39
|
+
y2={layout.plotY + layout.plotHeight - i * layout.stepValue * layout.valueHeight}
|
|
40
|
+
stroke="currentcolor"
|
|
41
|
+
/>
|
|
42
|
+
}
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
@if (vLines()) {
|
|
42
46
|
@for (let [i] of layout.labels.entries()) {
|
|
43
47
|
<line
|
|
44
|
-
x1={layout.
|
|
45
|
-
y1={layout.
|
|
46
|
-
x2={layout.
|
|
47
|
-
y2={
|
|
48
|
+
x1={layout.plotX + i * layout.itemWidth + layout.itemWidth / 2}
|
|
49
|
+
y1={layout.plotY}
|
|
50
|
+
x2={layout.plotX + i * layout.itemWidth + layout.itemWidth / 2}
|
|
51
|
+
y2={layout.plotY + layout.plotHeight}
|
|
48
52
|
stroke="currentcolor"
|
|
49
53
|
/>
|
|
50
54
|
}
|