@torpor/ui 1.2.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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,111 @@
|
|
|
1
|
+
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
|
+
|
|
3
|
+
interface ChartLegendProps {
|
|
4
|
+
/** Which side of the plot to reserve (default "bottom") */
|
|
5
|
+
side?: "top" | "bottom";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A legend for a Chart: one color swatch and name per series.
|
|
10
|
+
*
|
|
11
|
+
* Like the axes, it reserves a strip on its side of the plot -- wide enough
|
|
12
|
+
* for the names, wrapping onto more rows when they don't fit one row --
|
|
13
|
+
* and marks and other layers draw only in the space left over.
|
|
14
|
+
*/
|
|
15
|
+
export default function ChartLegend($props?: ChartLegendProps) {
|
|
16
|
+
const context = $context[ChartContextName] as ChartContext;
|
|
17
|
+
if (!context) {
|
|
18
|
+
throw new Error("ChartLegend must be contained within a Chart");
|
|
19
|
+
}
|
|
20
|
+
const { layout, series, colorAt } = context;
|
|
21
|
+
|
|
22
|
+
function side(): "top" | "bottom" {
|
|
23
|
+
return $props?.side ?? "bottom";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function entries(): { name: string; color: string }[] {
|
|
27
|
+
return series().map((s, i) => ({ name: s.name, color: colorAt(i) }));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Rough width of an entry: swatch + gap + the name's measured width +
|
|
31
|
+
// a gap before the next entry
|
|
32
|
+
function entryWidth(name: string): number {
|
|
33
|
+
return layout.textHeight / 3 + layout.textHeight / 6 + name.length * layout.textWidth + layout.textHeight / 3;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function rowHeight(): number {
|
|
37
|
+
return layout.textHeight * 1.5;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The entries laid out into rows that fit the plot width, each row
|
|
41
|
+
// centered in the reserved strip
|
|
42
|
+
function rows(): { entries: { name: string; color: string; x: number }[]; y: number }[] {
|
|
43
|
+
const width = layout.plotWidth;
|
|
44
|
+
const lines: { name: string; color: string }[][] = [[]];
|
|
45
|
+
for (const entry of entries()) {
|
|
46
|
+
const line = lines[lines.length - 1];
|
|
47
|
+
const used = line.reduce((a, b) => a + entryWidth(b.name), 0);
|
|
48
|
+
if (line.length && used + entryWidth(entry.name) > width) {
|
|
49
|
+
lines.push([entry]);
|
|
50
|
+
} else {
|
|
51
|
+
line.push(entry);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Rows anchor to the outer edge of the strip: an axis sharing the
|
|
56
|
+
// side anchors to the plot edge instead, so the two stack without
|
|
57
|
+
// overlapping
|
|
58
|
+
const startY = side() === "bottom" ? layout.calculatedHeight - lines.length * rowHeight() : 0;
|
|
59
|
+
|
|
60
|
+
return lines.map((line, r) => {
|
|
61
|
+
const lineWidth = line.reduce((a, b) => a + entryWidth(b.name), 0);
|
|
62
|
+
let x = layout.plotX + Math.max(0, (width - lineWidth) / 2);
|
|
63
|
+
return {
|
|
64
|
+
entries: line.map((e) => {
|
|
65
|
+
const positioned = { name: e.name, color: e.color, x };
|
|
66
|
+
x += entryWidth(e.name);
|
|
67
|
+
return positioned;
|
|
68
|
+
}),
|
|
69
|
+
y: startY + r * rowHeight() + rowHeight() / 2,
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Reserve one row of strips per line the entries need. Reading the plot
|
|
75
|
+
// width keeps the wrap up to date; the sides it reserves on don't
|
|
76
|
+
// affect the plot width, so this settles.
|
|
77
|
+
$run(() => {
|
|
78
|
+
const rowCount = Math.max(1, rows().length);
|
|
79
|
+
layout.reserve("legend", side(), rowCount * rowHeight());
|
|
80
|
+
return () => layout.release("legend");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
function swatchSize(): number {
|
|
84
|
+
return layout.textHeight / 3;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@render {
|
|
88
|
+
<svg>
|
|
89
|
+
@for (let row of rows()) {
|
|
90
|
+
@for (let entry of row.entries) {
|
|
91
|
+
<rect
|
|
92
|
+
x={entry.x}
|
|
93
|
+
y={row.y - swatchSize() / 2}
|
|
94
|
+
width={swatchSize()}
|
|
95
|
+
height={swatchSize()}
|
|
96
|
+
rx="2"
|
|
97
|
+
fill={entry.color}
|
|
98
|
+
/>
|
|
99
|
+
<text
|
|
100
|
+
fill="currentcolor"
|
|
101
|
+
dominant-baseline="middle"
|
|
102
|
+
x={entry.x + swatchSize() + layout.textHeight / 6}
|
|
103
|
+
y={row.y}
|
|
104
|
+
>
|
|
105
|
+
{entry.name}
|
|
106
|
+
</text>
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
</svg>
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
2
|
import type DataSeries from "./DataSeries";
|
|
3
|
+
import { calculateCrossStackMax } from "../utils/chartUtils";
|
|
3
4
|
|
|
4
5
|
interface ChartLinesProps {
|
|
5
6
|
/** The index of the series to draw (default 0) */
|
|
@@ -8,36 +9,71 @@ interface ChartLinesProps {
|
|
|
8
9
|
color?: string;
|
|
9
10
|
/** Draws a point at each value (default false) */
|
|
10
11
|
showPoints?: boolean;
|
|
12
|
+
/** Stacks this line on top of the ones below it, instead of from the baseline */
|
|
13
|
+
stacked?: boolean;
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
/**
|
|
14
17
|
* Line marks for a Chart: draws the chosen series as a connected line
|
|
15
|
-
* across the
|
|
18
|
+
* across the plot.
|
|
16
19
|
*
|
|
17
20
|
* Add alongside ChartColumns or ChartBars for overlaid layers -- or use two
|
|
18
21
|
* ChartLines with different `seriesIndex` to compare series.
|
|
22
|
+
*
|
|
23
|
+
* With `stacked`, each line sits on top of the stacked series below it --
|
|
24
|
+
* give every stacked line the same treatment and the Chart's value domain
|
|
25
|
+
* grows to fit the largest total.
|
|
19
26
|
*/
|
|
20
27
|
export default function ChartLines($props?: ChartLinesProps) {
|
|
21
28
|
const context = $context[ChartContextName] as ChartContext;
|
|
22
29
|
if (!context) {
|
|
23
30
|
throw new Error("This layer must be contained within a Chart");
|
|
24
31
|
}
|
|
25
|
-
const { layout, series, colorAt } = context;
|
|
26
|
-
context.registerMark("
|
|
32
|
+
const { layout, series, colorAt, registerMax } = context;
|
|
33
|
+
context.registerMark("linear");
|
|
27
34
|
|
|
28
35
|
function index(): number {
|
|
29
36
|
return $props?.seriesIndex ?? 0;
|
|
30
37
|
}
|
|
31
38
|
|
|
39
|
+
function stacked(): boolean {
|
|
40
|
+
return $props?.stacked === true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Stacked lines reach higher than any single series, so raise the
|
|
44
|
+
// domain to the largest stack total (released when this layer unmounts
|
|
45
|
+
// or stops stacking)
|
|
46
|
+
$run(() => {
|
|
47
|
+
if (stacked()) {
|
|
48
|
+
registerMax("line-" + index(), calculateCrossStackMax(series()));
|
|
49
|
+
}
|
|
50
|
+
return () => registerMax("line-" + index(), 0);
|
|
51
|
+
});
|
|
52
|
+
|
|
32
53
|
function current(): DataSeries | undefined {
|
|
33
54
|
return series()[index()];
|
|
34
55
|
}
|
|
35
56
|
|
|
57
|
+
// The sum of the series below this one, at point j
|
|
58
|
+
function below(j: number): number {
|
|
59
|
+
if (!stacked()) {
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
let total = 0;
|
|
63
|
+
for (let i = 0; i < index(); i++) {
|
|
64
|
+
total += series()[i]?.data[j] ?? 0;
|
|
65
|
+
}
|
|
66
|
+
return total;
|
|
67
|
+
}
|
|
68
|
+
|
|
36
69
|
function points(): { x: number; y: number }[] {
|
|
37
70
|
const data = current()?.data ?? [];
|
|
71
|
+
// Points are spread by their index across the full plot width, from
|
|
72
|
+
// the left edge to the right
|
|
73
|
+
const step = data.length > 1 ? layout.plotWidth / (data.length - 1) : 0;
|
|
38
74
|
return data.map((value, j) => ({
|
|
39
|
-
x: +(layout.
|
|
40
|
-
y: +(layout.
|
|
75
|
+
x: +(layout.plotX + (data.length > 1 ? j * step : layout.plotWidth / 2)).toFixed(2),
|
|
76
|
+
y: +(layout.plotY + layout.plotHeight - (below(j) + value) * layout.valueHeight).toFixed(2),
|
|
41
77
|
}));
|
|
42
78
|
}
|
|
43
79
|
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
|
+
import type DataSeries from "./DataSeries";
|
|
3
|
+
|
|
4
|
+
interface ChartPointsProps {
|
|
5
|
+
/** The index of the series to draw (default 0) */
|
|
6
|
+
seriesIndex?: number;
|
|
7
|
+
/** The color to draw with, instead of the series' own */
|
|
8
|
+
color?: string;
|
|
9
|
+
/** The radius of each point in pixels (default 4) */
|
|
10
|
+
radius?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Point marks for a Chart: one circle per value, centered in its category
|
|
15
|
+
* slot. On its own it is a scatter-style layer; with ChartLines it draws
|
|
16
|
+
* the line's markers.
|
|
17
|
+
*/
|
|
18
|
+
export default function ChartPoints($props?: ChartPointsProps) {
|
|
19
|
+
const context = $context[ChartContextName] as ChartContext;
|
|
20
|
+
if (!context) {
|
|
21
|
+
throw new Error("This layer must be contained within a Chart");
|
|
22
|
+
}
|
|
23
|
+
const { layout, series, colorAt } = context;
|
|
24
|
+
context.registerMark("linear");
|
|
25
|
+
|
|
26
|
+
function index(): number {
|
|
27
|
+
return $props?.seriesIndex ?? 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function current(): DataSeries | undefined {
|
|
31
|
+
return series()[index()];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function points(): { x: number; y: number }[] {
|
|
35
|
+
const data = current()?.data ?? [];
|
|
36
|
+
// Points are spread by their index across the full plot width, from
|
|
37
|
+
// the left edge to the right
|
|
38
|
+
const step = data.length > 1 ? layout.plotWidth / (data.length - 1) : 0;
|
|
39
|
+
return data.map((value, j) => ({
|
|
40
|
+
x: +(layout.plotX + (data.length > 1 ? j * step : layout.plotWidth / 2)).toFixed(2),
|
|
41
|
+
y: +(layout.plotY + layout.plotHeight - value * layout.valueHeight).toFixed(2),
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function stroke(): string {
|
|
46
|
+
return $props?.color || colorAt(index());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function radius(): number {
|
|
50
|
+
return $props?.radius ?? 4;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@render {
|
|
54
|
+
<svg>
|
|
55
|
+
@for (let [j, point] of points().entries()) {
|
|
56
|
+
<circle
|
|
57
|
+
cx={point.x}
|
|
58
|
+
cy={point.y}
|
|
59
|
+
r={radius()}
|
|
60
|
+
stroke={stroke()}
|
|
61
|
+
stroke-width="2"
|
|
62
|
+
fill="white"
|
|
63
|
+
>
|
|
64
|
+
<title>{`${[current()?.name, layout.labels[j], current()?.data[j]].filter(Boolean).join("\n")}`}</title>
|
|
65
|
+
</circle>
|
|
66
|
+
}
|
|
67
|
+
</svg>
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import type DataSeries from "./DataSeries";
|
|
2
|
-
import { type ChartLayout } from "../utils/chartLayout";
|
|
2
|
+
import { type ChartLayout, type ChartSide } from "../utils/chartLayout";
|
|
3
3
|
|
|
4
4
|
export const ChartContextName: unique symbol = Symbol.for("torp.Chart");
|
|
5
5
|
|
|
6
|
+
export type { ChartSide };
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* The shared context for layered chart components (ChartXAxis, ChartYAxis,
|
|
8
|
-
* ChartGrid
|
|
10
|
+
* ChartGrid etc), published by the Chart container.
|
|
11
|
+
*
|
|
12
|
+
* Furniture layers (axes, legends) reserve the space they need through the
|
|
13
|
+
* layout; the plot rect is whatever is left over, and mark layers draw only
|
|
14
|
+
* into that, overlapping each other freely.
|
|
9
15
|
*/
|
|
10
16
|
export interface ChartContext {
|
|
11
|
-
/** The plot geometry:
|
|
17
|
+
/** The plot geometry: reservations, the plot rect, scales and derived positions */
|
|
12
18
|
layout: ChartLayout;
|
|
13
19
|
/** The series that layers draw from */
|
|
14
20
|
series: () => DataSeries[];
|
|
@@ -18,8 +24,14 @@ export interface ChartContext {
|
|
|
18
24
|
yLabel: () => string;
|
|
19
25
|
/** Whether the chart draws horizontal marks (bars), set by the mark layers */
|
|
20
26
|
horizontal: () => boolean;
|
|
27
|
+
/** Whether the chart draws slot marks (columns), set by the mark layers */
|
|
28
|
+
vertical: () => boolean;
|
|
29
|
+
/** Whether the chart draws point marks (lines, areas, points), set by the mark layers */
|
|
30
|
+
linear: () => boolean;
|
|
21
31
|
/** Called by mark layers to announce their orientation */
|
|
22
|
-
registerMark: (orientation: "horizontal" | "vertical") => void;
|
|
32
|
+
registerMark: (orientation: "horizontal" | "vertical" | "linear") => void;
|
|
33
|
+
/** Called by aggregated mark layers (stacks) to raise the value domain */
|
|
34
|
+
registerMax: (id: string, value: number) => void;
|
|
23
35
|
/** The color to draw series i with, from its own color or the palette */
|
|
24
36
|
colorAt: (i: number) => string;
|
|
25
37
|
}
|
|
@@ -12,13 +12,17 @@ interface ChartXAxisProps {
|
|
|
12
12
|
* the bottom with each series' label centered under its slot; for
|
|
13
13
|
* horizontal marks (bars) it becomes the value scale, with a tick for each
|
|
14
14
|
* step along the bottom.
|
|
15
|
+
*
|
|
16
|
+
* Adding it to a Chart reserves a strip along the bottom for its labels
|
|
17
|
+
* (and its title, when there is one): marks and other layers draw only in
|
|
18
|
+
* the space left over.
|
|
15
19
|
*/
|
|
16
20
|
export default function ChartXAxis($props?: ChartXAxisProps) {
|
|
17
21
|
const context = $context[ChartContextName] as ChartContext;
|
|
18
22
|
if (!context) {
|
|
19
23
|
throw new Error("ChartXAxis must be contained within a Chart");
|
|
20
24
|
}
|
|
21
|
-
const { layout, xLabel, horizontal } = context;
|
|
25
|
+
const { layout, xLabel, horizontal, vertical, linear } = context;
|
|
22
26
|
|
|
23
27
|
function showAxis(): boolean {
|
|
24
28
|
return $props?.showAxis !== false;
|
|
@@ -29,26 +33,77 @@ export default function ChartXAxis($props?: ChartXAxisProps) {
|
|
|
29
33
|
return xLabel();
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
// Reserve a strip along the bottom: one row for the labels, plus one
|
|
37
|
+
// more for the title when there is one
|
|
38
|
+
$run(() => {
|
|
39
|
+
const size = layout.textHeight * (title() ? 2 : 1);
|
|
40
|
+
layout.reserve("xaxis", "bottom", size);
|
|
41
|
+
return () => layout.release("xaxis");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// The top edge of the strip reserved by this axis, where the plot ends
|
|
45
|
+
function plotBottom(): number {
|
|
46
|
+
return layout.plotY + layout.plotHeight;
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
function unitWidth(): number {
|
|
37
|
-
return plotWidth
|
|
50
|
+
return layout.plotWidth / Math.max(1, layout.maxValue);
|
|
38
51
|
}
|
|
39
52
|
|
|
40
53
|
function tickX(i: number): number {
|
|
41
|
-
return layout.
|
|
54
|
+
return layout.plotX + i * layout.stepValue * unitWidth();
|
|
42
55
|
}
|
|
43
56
|
|
|
57
|
+
// Labels hang just under the axis line, with a small buffer, at the top
|
|
58
|
+
// of the strip; the title sits at the bottom of the strip, under the
|
|
59
|
+
// labels
|
|
44
60
|
function tickY(): number {
|
|
45
|
-
return
|
|
61
|
+
return plotBottom() + layout.textHeight * (2 / 3) + 4;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function titleY(): number {
|
|
65
|
+
return plotBottom() + layout.textHeight * 2;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The x of label i. Charts with slot marks (columns) center each label
|
|
69
|
+
// in its slot; charts with only point marks (lines, areas, points)
|
|
70
|
+
// spread the labels across the plot, matching where the marks are
|
|
71
|
+
function labelX(i: number): number {
|
|
72
|
+
if (horizontal()) {
|
|
73
|
+
return tickX(i);
|
|
74
|
+
}
|
|
75
|
+
const count = layout.labels.length;
|
|
76
|
+
if (linear() && !vertical()) {
|
|
77
|
+
if (count <= 1) {
|
|
78
|
+
return layout.plotX + layout.plotWidth / 2;
|
|
79
|
+
}
|
|
80
|
+
return layout.plotX + (i / (count - 1)) * layout.plotWidth;
|
|
81
|
+
}
|
|
82
|
+
return layout.plotX + layout.itemWidth * i + layout.itemWidth / 2;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Edge ticks and labels turn inward so they don't hang past the plot
|
|
86
|
+
function tickAnchor(i: number, count: number): string {
|
|
87
|
+
if (i === 0) return "start";
|
|
88
|
+
if (i === count - 1) return "end";
|
|
89
|
+
return "middle";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function labelAnchor(i: number): string {
|
|
93
|
+
if (horizontal()) {
|
|
94
|
+
return tickAnchor(i, layout.stepLabels.length);
|
|
95
|
+
}
|
|
96
|
+
const count = layout.labels.length;
|
|
97
|
+
if (linear() && !vertical() && count > 1) {
|
|
98
|
+
return tickAnchor(i, count);
|
|
99
|
+
}
|
|
100
|
+
return "middle";
|
|
46
101
|
}
|
|
47
102
|
|
|
48
103
|
@render {
|
|
49
104
|
<svg>
|
|
50
105
|
@if (showAxis()) {
|
|
51
|
-
<line x1={layout.
|
|
106
|
+
<line x1={layout.plotX} y1={plotBottom() + 0.5} x2={layout.plotX + layout.plotWidth} y2={plotBottom() + 0.5} stroke="currentcolor" />
|
|
52
107
|
}
|
|
53
108
|
@if (!horizontal()) {
|
|
54
109
|
@if (title()) {
|
|
@@ -56,8 +111,8 @@ export default function ChartXAxis($props?: ChartXAxisProps) {
|
|
|
56
111
|
fill="currentcolor"
|
|
57
112
|
text-anchor="middle"
|
|
58
113
|
dominant-baseline="text-after-edge"
|
|
59
|
-
x={layout.
|
|
60
|
-
y={
|
|
114
|
+
x={layout.plotX + layout.plotWidth / 2}
|
|
115
|
+
y={titleY()}
|
|
61
116
|
>
|
|
62
117
|
{title()}
|
|
63
118
|
</text>
|
|
@@ -65,9 +120,9 @@ export default function ChartXAxis($props?: ChartXAxisProps) {
|
|
|
65
120
|
@for (let [i, label] of layout.labels.entries()) {
|
|
66
121
|
<text
|
|
67
122
|
fill="currentcolor"
|
|
68
|
-
text-anchor=
|
|
123
|
+
text-anchor={labelAnchor(i)}
|
|
69
124
|
dominant-baseline="text-after-edge"
|
|
70
|
-
x={
|
|
125
|
+
x={labelX(i)}
|
|
71
126
|
y={tickY()}
|
|
72
127
|
>
|
|
73
128
|
{label}
|
|
@@ -75,7 +130,7 @@ export default function ChartXAxis($props?: ChartXAxisProps) {
|
|
|
75
130
|
}
|
|
76
131
|
} else {
|
|
77
132
|
@for (let [i, label] of layout.stepLabels.entries()) {
|
|
78
|
-
<text text-anchor=
|
|
133
|
+
<text text-anchor={tickAnchor(i, layout.stepLabels.length)} dominant-baseline="text-after-edge" x={tickX(i)} y={tickY()}>
|
|
79
134
|
{label}
|
|
80
135
|
</text>
|
|
81
136
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ChartContext, ChartContextName } from "./ChartTypes";
|
|
2
|
+
import { calculateTickLabelWidth } from "../utils/chartUtils";
|
|
2
3
|
|
|
3
4
|
interface ChartYAxisProps {
|
|
4
5
|
/** Whether to draw the axis line itself (default true) */
|
|
@@ -15,8 +16,9 @@ interface ChartYAxisProps {
|
|
|
15
16
|
* between 0 and the maximum value. For horizontal marks (bars) it carries
|
|
16
17
|
* the category labels, one per band down the left.
|
|
17
18
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
* Adding it to a Chart reserves a strip on its side of the plot: wide enough
|
|
20
|
+
* for its ticks (or the category names), plus the title when one is drawn.
|
|
21
|
+
* Marks and other layers draw only in the space left over.
|
|
20
22
|
*/
|
|
21
23
|
export default function ChartYAxis($props?: ChartYAxisProps) {
|
|
22
24
|
const context = $context[ChartContextName] as ChartContext;
|
|
@@ -38,46 +40,74 @@ export default function ChartYAxis($props?: ChartYAxisProps) {
|
|
|
38
40
|
return yLabel();
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
function
|
|
42
|
-
const
|
|
43
|
-
return
|
|
43
|
+
function maxNameWidth(): number {
|
|
44
|
+
const maxLength = layout.labels.reduce((a, b) => Math.max(a, b.length), 0);
|
|
45
|
+
return maxLength * layout.textWidth;
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
// Reserve a strip on this axis's side: the tick labels plus a gap (and
|
|
49
|
+
// the rotated title's width on the left, where it is drawn)
|
|
50
|
+
$run(() => {
|
|
51
|
+
const gap = layout.textHeight / (side() === "left" ? 2 : 4);
|
|
52
|
+
const labelWidth = horizontal()
|
|
53
|
+
? maxNameWidth()
|
|
54
|
+
: calculateTickLabelWidth(layout.stepLabels, layout.textWidth);
|
|
55
|
+
const titleWidth = side() === "left" && title() ? layout.textHeight : 0;
|
|
56
|
+
layout.reserve("yaxis", side(), gap + labelWidth + titleWidth);
|
|
57
|
+
return () => layout.release("yaxis");
|
|
58
|
+
});
|
|
59
|
+
|
|
46
60
|
function topPad(): number {
|
|
47
61
|
return layout.textHeight / 3;
|
|
48
62
|
}
|
|
49
63
|
|
|
64
|
+
function bandHeight(): number {
|
|
65
|
+
const count = Math.max(1, layout.labels.length);
|
|
66
|
+
return (layout.plotHeight - topPad()) / count;
|
|
67
|
+
}
|
|
68
|
+
|
|
50
69
|
function bandCenter(i: number): number {
|
|
51
|
-
return topPad() + i * bandHeight() + bandHeight() / 2;
|
|
70
|
+
return layout.plotY + topPad() + i * bandHeight() + bandHeight() / 2;
|
|
52
71
|
}
|
|
53
72
|
|
|
73
|
+
// The axis line runs down the edge of the plot on this axis's side,
|
|
74
|
+
// just outside it so marks don't paint over it
|
|
54
75
|
function axisX(): number {
|
|
55
|
-
return side() === "left" ? layout.
|
|
76
|
+
return side() === "left" ? layout.plotX - 0.5 : layout.plotX + layout.plotWidth + 0.5;
|
|
56
77
|
}
|
|
57
78
|
|
|
58
79
|
function tickX(): number {
|
|
59
|
-
|
|
60
|
-
return layout.chartLeft - layout.textHeight / 4;
|
|
61
|
-
}
|
|
62
|
-
return layout.calculatedWidth + layout.textHeight / 4;
|
|
80
|
+
return side() === "left" ? axisX() - layout.textHeight / 4 : axisX() + layout.textHeight / 4;
|
|
63
81
|
}
|
|
64
82
|
|
|
65
83
|
function anchor(): string {
|
|
66
84
|
return side() === "left" ? "end" : "start";
|
|
67
85
|
}
|
|
68
86
|
|
|
87
|
+
// The bottom and top ticks turn inside so they don't hang past the
|
|
88
|
+
// plot's corners
|
|
89
|
+
function tickBaseline(i: number): string {
|
|
90
|
+
if (i === 0) return "auto";
|
|
91
|
+
if (i === layout.stepLabels.length - 1) return "hanging";
|
|
92
|
+
return "middle";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function tickY(i: number): number {
|
|
96
|
+
return layout.plotY + layout.plotHeight - i * layout.stepValue * layout.valueHeight;
|
|
97
|
+
}
|
|
98
|
+
|
|
69
99
|
@render {
|
|
70
100
|
<svg>
|
|
71
101
|
@if (showAxis()) {
|
|
72
|
-
<line x1={axisX()} y1={layout.
|
|
102
|
+
<line x1={axisX()} y1={layout.plotY + layout.plotHeight + 0.5} x2={axisX()} y2={layout.plotY - 0.5} stroke="currentcolor" />
|
|
73
103
|
}
|
|
74
104
|
@if (horizontal()) {
|
|
75
105
|
@for (let [i, label] of layout.labels.entries()) {
|
|
76
106
|
<text
|
|
77
107
|
fill="currentcolor"
|
|
78
|
-
text-anchor=
|
|
108
|
+
text-anchor={anchor()}
|
|
79
109
|
dominant-baseline="middle"
|
|
80
|
-
x={
|
|
110
|
+
x={tickX()}
|
|
81
111
|
y={bandCenter(i)}
|
|
82
112
|
>
|
|
83
113
|
{label}
|
|
@@ -90,14 +120,14 @@ export default function ChartYAxis($props?: ChartYAxisProps) {
|
|
|
90
120
|
text-anchor="middle"
|
|
91
121
|
dominant-baseline="hanging"
|
|
92
122
|
x={0}
|
|
93
|
-
y={layout.
|
|
94
|
-
transform={`rotate(-90, 0, ${layout.
|
|
123
|
+
y={layout.plotY + layout.plotHeight / 2}
|
|
124
|
+
transform={`rotate(-90, 0, ${layout.plotY + layout.plotHeight / 2})`}
|
|
95
125
|
>
|
|
96
126
|
{title()}
|
|
97
127
|
</text>
|
|
98
128
|
}
|
|
99
129
|
@for (let [i, label] of layout.stepLabels.entries()) {
|
|
100
|
-
<text text-anchor={anchor()} dominant-baseline=
|
|
130
|
+
<text text-anchor={anchor()} dominant-baseline={tickBaseline(i)} x={tickX()} y={tickY(i)}>
|
|
101
131
|
{label}
|
|
102
132
|
</text>
|
|
103
133
|
}
|
|
@@ -25,6 +25,8 @@ interface ColumnChartProps {
|
|
|
25
25
|
stepValue?: number;
|
|
26
26
|
showHLines?: boolean;
|
|
27
27
|
showVLines?: boolean;
|
|
28
|
+
/** Stacks the values in each slot into one column per label */
|
|
29
|
+
stacked?: boolean;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -64,7 +66,7 @@ export default function ColumnChart($props: ColumnChartProps) {
|
|
|
64
66
|
<ChartYAxis />
|
|
65
67
|
<ChartGrid hLines={$props.showHLines === true} vLines={$props.showVLines === true} />
|
|
66
68
|
<ChartXAxis />
|
|
67
|
-
<ChartColumns />
|
|
69
|
+
<ChartColumns stacked={$props.stacked === true} />
|
|
68
70
|
</Chart>
|
|
69
71
|
}
|
|
70
72
|
}
|
|
@@ -26,6 +26,8 @@ interface LineChartProps {
|
|
|
26
26
|
showPoints?: boolean;
|
|
27
27
|
showHLines?: boolean;
|
|
28
28
|
showVLines?: boolean;
|
|
29
|
+
/** Stacks each series on top of the one below it */
|
|
30
|
+
stacked?: boolean;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/**
|
|
@@ -66,7 +68,7 @@ export default function LineChart($props: LineChartProps) {
|
|
|
66
68
|
<ChartGrid hLines={$props.showHLines === true} vLines={$props.showVLines === true} />
|
|
67
69
|
<ChartXAxis />
|
|
68
70
|
@for (let i of $props.series.map((_, i) => i)) {
|
|
69
|
-
<ChartLines seriesIndex={i} showPoints={$props.showPoints} />
|
|
71
|
+
<ChartLines seriesIndex={i} showPoints={$props.showPoints} stacked={$props.stacked === true} />
|
|
70
72
|
}
|
|
71
73
|
</Chart>
|
|
72
74
|
}
|