-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXYPlot.jsx
More file actions
129 lines (113 loc) · 2.91 KB
/
XYPlot.jsx
File metadata and controls
129 lines (113 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, { useMemo } from "react";
import { PlotContext, getScale, useSVGContext } from "./plot-utils";
import { useZoomablePlot } from "./useZoomablePlot";
const constructGridObject = (grid) => {
if (!("right" in grid)) {
grid.right = grid.left;
}
if (!("bottom" in grid)) {
grid.bottom = grid.top;
}
return grid;
};
const constructMargin = (margin) => {
if (typeof margin === "number") {
return { left: margin, right: margin, top: margin, bottom: margin };
}
if (!("left" in margin)) {
margin.left = 40;
}
if (!("right" in margin)) {
margin.right = 10;
}
if (!("top" in margin)) {
margin.top = 10;
}
if (!("bottom" in margin)) {
margin.bottom = 40;
}
return margin;
};
export const XYPlot = ({
grid,
margin,
xType,
xDomain,
xValues,
yType,
yDomain,
yValues,
preserveRatio,
children,
}) => {
const { width, height } = useSVGContext();
if (xValues) {
xType = "ordinal";
xDomain = [-0.5, xValues.length - 0.5];
}
if (yValues) {
yType = "ordinal";
yDomain = [-0.5, yValues.length - 0.5];
}
grid = constructGridObject(grid);
margin = constructMargin(margin);
const xGrid = width / grid.width;
const [outerLeft, outerRight] = [grid.left * xGrid, (grid.right + 1) * xGrid];
const innerWidth = outerRight - margin.right - outerLeft - margin.left;
const [left, right] = [outerLeft + margin.left, outerRight - margin.right];
const yGrid = height / grid.height;
const [outerTop, outerBottom] = [grid.top * yGrid, (grid.bottom + 1) * yGrid];
const innerHeight = outerBottom - margin.bottom - outerTop - margin.top;
const [top, bottom] = [outerTop + margin.top, outerBottom - margin.bottom];
if (innerWidth <= 0) {
throw "plot region width too small";
}
if (innerHeight <= 0) {
throw "plot region height too small";
}
const xRange = useMemo(() => [0, innerWidth], [innerWidth]);
const yRange = useMemo(() => [innerHeight, 0], [innerHeight]);
const [domains, events] = useZoomablePlot(xDomain, yDomain, xRange, yRange, preserveRatio);
const xScale = getScale(domains.xDomain, xRange, xType);
const yScale = getScale(domains.yDomain, yRange, yType);
return (
<PlotContext.Provider
value={{
innerWidth,
innerHeight,
outerLeft,
outerRight,
outerBottom,
outerTop,
left,
right,
bottom,
top,
preserveRatio,
xType,
xDomain,
xRange,
xScale,
xValues,
yType,
yScale,
yDomain,
yRange,
yValues,
...domains,
events,
}}
>
{children}
</PlotContext.Provider>
);
};
XYPlot.defaultProps = {
grid: { left: 0, right: 0, top: 0, bottom: 0, width: 1, height: 1 },
margin: { left: 40, right: 10, top: 10, bottom: 40 },
xDomain: [0, 1],
xType: "linear",
yDomain: [0, 1],
yType: "linear",
preserveRatio: false,
};