-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot-utils.jsx
More file actions
94 lines (81 loc) · 2.55 KB
/
plot-utils.jsx
File metadata and controls
94 lines (81 loc) · 2.55 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
import React, { createContext, useContext } from "react";
import { scaleLinear, scaleLog } from "d3-scale";
export const SVGContext = createContext({
width: 900,
height: 600
});
export const useSVGContext = () => useContext(SVGContext);
export const PlotContext = createContext({});
// // contains:
// innerWidth, data plot region width
// innerHeight, data plot region height
// outerLeft, outer plot region (relative to svg, including margins)
// outerRight, outer plot region (relative to svg, including margins)
// outerBottom, outer plot region (relative to svg, including margins)
// outerTop, outer plot region (relative to svg, including margins)
// left, inner plot region (relative to svg)
// right, inner plot region (relative to svg)
// bottom, inner plot region (relative to svg)
// top, inner plot region (relative to svg)
// xType, string (possibly optional)
// xDomain, [min,max] (possibly optional)
// xScale, d3 scale object
// yType, string (possibly optional)
// yDomain, [min,max] (possibly optional)
// yScale, d3 scale object
// //
export const usePlotContext = () => useContext(PlotContext);
export const getTickValues = (scale, ordinalValues, tickTotal, tickValues) => {
if (tickValues) {
return tickValues;
} else if (ordinalValues) {
return ordinalValues.map((d, i) => i);
}
return scale.ticks ? scale.ticks(tickTotal) : scale.domain();
};
export function classes() {
return [...arguments].filter(Boolean).join(" ");
}
export const ORIENTATION = {
TOP: "top",
LEFT: "left",
RIGHT: "right",
BOTTOM: "bottom",
VERTICAL: "vertical",
HORIZONTAL: "horizontal"
};
export const DIRECTION = {
VERTICAL: "vertical",
HORIZONTAL: "horizontal"
};
export const GPlotRegion = ({ className, children }) => {
const { left, top } = usePlotContext();
return (
<g className={className} transform={`translate(${left},${top})`}>
{children}
</g>
);
};
export const onDataEvents = (props, data, index) => {
let eventHandlerKeys = Object.keys(props).filter(k => k.startsWith("on"));
let p = {};
for (var i = 0; i < eventHandlerKeys.length; i++) {
let key = eventHandlerKeys[i];
p[key] = e => props[key](e, data, index);
}
return p;
};
export const getScale = (domain, range, type = "linear") => {
if (type === "linear" || type === "ordinal") {
return scaleLinear()
.domain(domain)
.range(range);
} else if (type === "log") {
if (domain[0] <= 0) {
console.warn("log scale cannot be less than or equal to zero");
}
return scaleLog()
.domain(domain)
.range(range);
}
};