victory-core 36.7.0 → 36.8.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.
Files changed (149) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/victory-core.js +366 -286
  3. package/dist/victory-core.min.js +1 -1
  4. package/es/victory-label/victory-label.js +11 -11
  5. package/es/victory-primitives/arc.d.ts +0 -5
  6. package/es/victory-primitives/arc.js +7 -7
  7. package/es/victory-primitives/background.d.ts +0 -6
  8. package/es/victory-primitives/background.js +8 -8
  9. package/es/victory-primitives/border.d.ts +0 -5
  10. package/es/victory-primitives/border.js +7 -7
  11. package/es/victory-primitives/circle.d.ts +2 -2
  12. package/es/victory-primitives/circle.js +30 -16
  13. package/es/victory-primitives/clip-path.d.ts +1 -8
  14. package/es/victory-primitives/clip-path.js +4 -10
  15. package/es/victory-primitives/line-segment.d.ts +0 -5
  16. package/es/victory-primitives/line-segment.js +7 -7
  17. package/es/victory-primitives/line.d.ts +2 -2
  18. package/es/victory-primitives/line.js +30 -16
  19. package/es/victory-primitives/path.d.ts +1 -1
  20. package/es/victory-primitives/path.js +24 -12
  21. package/es/victory-primitives/point.d.ts +0 -5
  22. package/es/victory-primitives/point.js +7 -7
  23. package/es/victory-primitives/rect.d.ts +2 -2
  24. package/es/victory-primitives/rect.js +30 -16
  25. package/es/victory-primitives/text.d.ts +1 -9
  26. package/es/victory-primitives/text.js +23 -13
  27. package/es/victory-primitives/tspan.js +30 -5
  28. package/es/victory-primitives/whisker.d.ts +0 -6
  29. package/es/victory-primitives/whisker.js +8 -8
  30. package/es/victory-util/helpers.js +4 -3
  31. package/es/victory-util/textsize.js +7 -0
  32. package/es/victory-util/user-props.js +2 -16
  33. package/lib/victory-label/victory-label.js +12 -11
  34. package/lib/victory-primitives/arc.d.ts +0 -5
  35. package/lib/victory-primitives/arc.js +8 -7
  36. package/lib/victory-primitives/background.d.ts +0 -6
  37. package/lib/victory-primitives/background.js +9 -8
  38. package/lib/victory-primitives/border.d.ts +0 -5
  39. package/lib/victory-primitives/border.js +8 -7
  40. package/lib/victory-primitives/circle.d.ts +2 -2
  41. package/lib/victory-primitives/circle.js +34 -18
  42. package/lib/victory-primitives/clip-path.d.ts +1 -8
  43. package/lib/victory-primitives/clip-path.js +5 -12
  44. package/lib/victory-primitives/line-segment.d.ts +0 -5
  45. package/lib/victory-primitives/line-segment.js +8 -7
  46. package/lib/victory-primitives/line.d.ts +2 -2
  47. package/lib/victory-primitives/line.js +34 -18
  48. package/lib/victory-primitives/path.d.ts +1 -1
  49. package/lib/victory-primitives/path.js +25 -12
  50. package/lib/victory-primitives/point.d.ts +0 -5
  51. package/lib/victory-primitives/point.js +8 -7
  52. package/lib/victory-primitives/rect.d.ts +2 -2
  53. package/lib/victory-primitives/rect.js +34 -18
  54. package/lib/victory-primitives/text.d.ts +1 -9
  55. package/lib/victory-primitives/text.js +24 -14
  56. package/lib/victory-primitives/tspan.js +31 -5
  57. package/lib/victory-primitives/whisker.d.ts +0 -6
  58. package/lib/victory-primitives/whisker.js +9 -8
  59. package/lib/victory-util/helpers.js +8 -3
  60. package/lib/victory-util/textsize.js +7 -0
  61. package/lib/victory-util/user-props.js +3 -16
  62. package/package.json +2 -2
  63. package/src/victory-label/victory-label.tsx +10 -11
  64. package/src/victory-primitives/arc.tsx +7 -7
  65. package/src/victory-primitives/background.tsx +8 -8
  66. package/src/victory-primitives/border.tsx +7 -7
  67. package/src/victory-primitives/circle.tsx +24 -14
  68. package/src/victory-primitives/clip-path.tsx +1 -13
  69. package/src/victory-primitives/line-segment.tsx +7 -7
  70. package/src/victory-primitives/line.tsx +25 -14
  71. package/src/victory-primitives/path.tsx +23 -17
  72. package/src/victory-primitives/point.tsx +7 -7
  73. package/src/victory-primitives/rect.tsx +25 -14
  74. package/src/victory-primitives/text.tsx +13 -10
  75. package/src/victory-primitives/tspan.tsx +15 -4
  76. package/src/victory-primitives/whisker.tsx +8 -8
  77. package/src/victory-util/helpers.ts +5 -3
  78. package/src/victory-util/textsize.ts +5 -1
  79. package/src/victory-util/user-props.ts +2 -14
  80. package/es/v37/clone.d.ts +0 -23
  81. package/es/v37/clone.js +0 -32
  82. package/es/v37/index.d.ts +0 -5
  83. package/es/v37/index.js +0 -4
  84. package/es/v37/victory-state/defaults.d.ts +0 -3
  85. package/es/v37/victory-state/defaults.js +0 -2
  86. package/es/v37/victory-state/helpers/get-axis-data.d.ts +0 -3
  87. package/es/v37/victory-state/helpers/get-axis-data.js +0 -14
  88. package/es/v37/victory-state/helpers/get-data.d.ts +0 -15
  89. package/es/v37/victory-state/helpers/get-data.js +0 -87
  90. package/es/v37/victory-state/helpers/get-domain.d.ts +0 -6
  91. package/es/v37/victory-state/helpers/get-domain.js +0 -67
  92. package/es/v37/victory-state/helpers/get-range.d.ts +0 -6
  93. package/es/v37/victory-state/helpers/get-range.js +0 -36
  94. package/es/v37/victory-state/helpers/get-scale.d.ts +0 -6
  95. package/es/v37/victory-state/helpers/get-scale.js +0 -58
  96. package/es/v37/victory-state/index.d.ts +0 -4
  97. package/es/v37/victory-state/index.js +0 -3
  98. package/es/v37/victory-state/types.d.ts +0 -24
  99. package/es/v37/victory-state/types.js +0 -1
  100. package/es/v37/victory-state/victory-provider.d.ts +0 -21
  101. package/es/v37/victory-state/victory-provider.js +0 -148
  102. package/es/v37/victory-state/with-victory-provider.d.ts +0 -7
  103. package/es/v37/victory-state/with-victory-provider.js +0 -43
  104. package/es/v37/with-container.d.ts +0 -5
  105. package/es/v37/with-container.js +0 -32
  106. package/es/v37/with-normalized-props.d.ts +0 -17
  107. package/es/v37/with-normalized-props.js +0 -18
  108. package/lib/v37/clone.d.ts +0 -23
  109. package/lib/v37/clone.js +0 -43
  110. package/lib/v37/index.d.ts +0 -5
  111. package/lib/v37/index.js +0 -57
  112. package/lib/v37/victory-state/defaults.d.ts +0 -3
  113. package/lib/v37/victory-state/defaults.js +0 -10
  114. package/lib/v37/victory-state/helpers/get-axis-data.d.ts +0 -3
  115. package/lib/v37/victory-state/helpers/get-axis-data.js +0 -22
  116. package/lib/v37/victory-state/helpers/get-data.d.ts +0 -15
  117. package/lib/v37/victory-state/helpers/get-data.js +0 -98
  118. package/lib/v37/victory-state/helpers/get-domain.d.ts +0 -6
  119. package/lib/v37/victory-state/helpers/get-domain.js +0 -81
  120. package/lib/v37/victory-state/helpers/get-range.d.ts +0 -6
  121. package/lib/v37/victory-state/helpers/get-range.js +0 -45
  122. package/lib/v37/victory-state/helpers/get-scale.d.ts +0 -6
  123. package/lib/v37/victory-state/helpers/get-scale.js +0 -73
  124. package/lib/v37/victory-state/index.d.ts +0 -4
  125. package/lib/v37/victory-state/index.js +0 -44
  126. package/lib/v37/victory-state/types.d.ts +0 -24
  127. package/lib/v37/victory-state/types.js +0 -1
  128. package/lib/v37/victory-state/victory-provider.d.ts +0 -21
  129. package/lib/v37/victory-state/victory-provider.js +0 -175
  130. package/lib/v37/victory-state/with-victory-provider.d.ts +0 -7
  131. package/lib/v37/victory-state/with-victory-provider.js +0 -56
  132. package/lib/v37/with-container.d.ts +0 -5
  133. package/lib/v37/with-container.js +0 -45
  134. package/lib/v37/with-normalized-props.d.ts +0 -17
  135. package/lib/v37/with-normalized-props.js +0 -29
  136. package/src/v37/clone.tsx +0 -30
  137. package/src/v37/index.tsx +0 -4
  138. package/src/v37/victory-state/defaults.ts +0 -2
  139. package/src/v37/victory-state/helpers/get-axis-data.ts +0 -14
  140. package/src/v37/victory-state/helpers/get-data.ts +0 -78
  141. package/src/v37/victory-state/helpers/get-domain.ts +0 -62
  142. package/src/v37/victory-state/helpers/get-range.ts +0 -39
  143. package/src/v37/victory-state/helpers/get-scale.ts +0 -67
  144. package/src/v37/victory-state/index.ts +0 -3
  145. package/src/v37/victory-state/types.ts +0 -32
  146. package/src/v37/victory-state/victory-provider.tsx +0 -152
  147. package/src/v37/victory-state/with-victory-provider.tsx +0 -53
  148. package/src/v37/with-container.tsx +0 -38
  149. package/src/v37/with-normalized-props.tsx +0 -43
@@ -1,56 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.withVictoryProvider = withVictoryProvider;
7
-
8
- var React = _interopRequireWildcard(require("react"));
9
-
10
- var _victoryProvider = require("./victory-provider");
11
-
12
- var _clone = require("../clone");
13
-
14
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
-
16
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
-
18
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
19
-
20
- /* eslint-disable react/prop-types */
21
-
22
- /**
23
- * Ensures the component is wrapped by a VictoryProvider, if not already.
24
- */
25
- function withVictoryProvider(Comp) {
26
- var WithVictoryProvider = /*#__PURE__*/React.memo(function (props) {
27
- var updateChildProps = (0, _victoryProvider.useVictoryContextMaybe)(function (value) {
28
- return value === null || value === void 0 ? void 0 : value.updateChildProps;
29
- });
30
- var hasParentProvider = !!updateChildProps;
31
- React.useEffect(function () {
32
- if (!hasParentProvider) return;
33
- var id = Symbol("WithVictoryProvider");
34
- updateChildProps(id, props); // eslint-disable-next-line consistent-return
35
-
36
- return function () {
37
- return updateChildProps(id, null);
38
- };
39
- }); // @ts-expect-error "TProps not assignable to LibraryManagedAttributes"
40
-
41
- var result = /*#__PURE__*/React.createElement(Comp, props, props.children);
42
-
43
- if (!hasParentProvider) {
44
- // Wrap with a parent Provider:
45
- result = /*#__PURE__*/React.createElement(_victoryProvider.VictoryProvider, null, /*#__PURE__*/React.createElement(_clone.Clone, _extends({
46
- element: props.containerComponent
47
- }, props), result));
48
- }
49
-
50
- return result;
51
- });
52
- var name = Comp.displayName || Comp.name;
53
- WithVictoryProvider.displayName = name ? "WithVictoryProvider(".concat(name, ")") : "WithVictoryProvider"; // @ts-expect-error "WithVictoryProvider does not overlap with TComp"
54
-
55
- return WithVictoryProvider;
56
- }
@@ -1,5 +0,0 @@
1
- import React from "react";
2
- import { VictoryProviderProps } from "./victory-state";
3
- import { VictoryCommonProps } from "../victory-util/common-props";
4
- export declare function withContainer<Props extends VictoryCommonProps>(WrappedComponent: React.FC<Props>, initialProviderProps?: Partial<VictoryProviderProps>): React.FC<Props>;
5
- //# sourceMappingURL=with-container.d.ts.map
@@ -1,45 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.withContainer = withContainer;
7
-
8
- var _react = _interopRequireDefault(require("react"));
9
-
10
- var _victoryState = require("./victory-state");
11
-
12
- var _victoryContainer = require("../victory-container/victory-container");
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
17
-
18
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
19
-
20
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
-
22
- var defaultProviderProps = {
23
- width: 450,
24
- height: 300,
25
- padding: 50,
26
- data: []
27
- };
28
-
29
- function withContainer(WrappedComponent) {
30
- var initialProviderProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
31
- return function (props) {
32
- var providerProps = _objectSpread(_objectSpread(_objectSpread({}, defaultProviderProps), initialProviderProps), props);
33
-
34
- var _props$standalone = props.standalone,
35
- standalone = _props$standalone === void 0 ? true : _props$standalone,
36
- _props$containerCompo = props.containerComponent,
37
- containerComponent = _props$containerCompo === void 0 ? /*#__PURE__*/_react.default.createElement(_victoryContainer.VictoryContainer, null) : _props$containerCompo;
38
-
39
- if (standalone) {
40
- return /*#__PURE__*/_react.default.createElement(_victoryState.VictoryProvider, providerProps, /*#__PURE__*/_react.default.cloneElement(containerComponent, providerProps, /*#__PURE__*/_react.default.createElement(WrappedComponent, props)));
41
- }
42
-
43
- return /*#__PURE__*/_react.default.createElement(WrappedComponent, props);
44
- };
45
- }
@@ -1,17 +0,0 @@
1
- import { VictoryCommonProps } from "../victory-util/common-props";
2
- import React from "react";
3
- import { VictoryProviderProps } from "./victory-state";
4
- declare type FC<TProps> = (props: TProps) => JSX.Element;
5
- export declare function withNormalizedProps<TProps extends React.PropsWithChildren<VictoryCommonProps>, TDefaultPropKeys extends keyof TProps>(config: {
6
- displayName: string;
7
- propTypes?: React.WeakValidationMap<Omit<TProps, keyof VictoryCommonProps>>;
8
- defaultProps: Pick<TProps, TDefaultPropKeys>;
9
- /** @deprecated */
10
- initialProviderProps?: Partial<VictoryProviderProps>;
11
- }, WrappedComponent: React.FC<TProps>): FC<TProps> & {
12
- displayName: string;
13
- defaultProps: Pick<TProps, TDefaultPropKeys>;
14
- propTypes: React.WeakValidationMap<Omit<TProps, keyof VictoryCommonProps>> | undefined;
15
- };
16
- export {};
17
- //# sourceMappingURL=with-normalized-props.d.ts.map
@@ -1,29 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.withNormalizedProps = withNormalizedProps;
7
-
8
- var _react = _interopRequireDefault(require("react"));
9
-
10
- var _victoryState = require("./victory-state");
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
15
-
16
- function withNormalizedProps(config, WrappedComponent) {
17
- WrappedComponent.displayName = config.displayName;
18
- var WithNormalizedProps = (0, _victoryState.withVictoryProvider)(function (props) {
19
- var normalizedProps = (0, _victoryState.useVictoryContext)(function (v) {
20
- return v;
21
- });
22
- return /*#__PURE__*/_react.default.createElement(WrappedComponent, _extends({}, props, normalizedProps), props.children);
23
- });
24
- return Object.assign(WithNormalizedProps, {
25
- displayName: "WithNormalizedProps(".concat(config.displayName),
26
- defaultProps: config.defaultProps,
27
- propTypes: config.propTypes
28
- });
29
- }
package/src/v37/clone.tsx DELETED
@@ -1,30 +0,0 @@
1
- import React from "react";
2
-
3
- /**
4
- * Syntax sugar for `React.cloneElement`, so that we can use JSX instead
5
- *
6
- * @example
7
- * // Before:
8
- * const children = React.cloneElement(props.groupComponent, { prop2 }, props.children);
9
- * return React.cloneElement(props.containerComponent, { prop1 }, children);
10
- *
11
- * // After:
12
- * return (
13
- * <Clone element={props.containerComponent} prop1={prop1}>
14
- * <Clone element={props.groupComponent} prop2={prop2}>
15
- * {props.children}
16
- * </Clone>
17
- * </Clone>
18
- * );
19
- */
20
- export const Clone = <TProps,>(
21
- props: React.PropsWithChildren<
22
- {
23
- element: React.ReactElement<TProps>;
24
- children?: React.ReactNode | React.ReactNode[];
25
- } & TProps
26
- >,
27
- ) => {
28
- const { children, element, ...rest } = props;
29
- return React.cloneElement(element, rest as unknown as TProps, children);
30
- };
package/src/v37/index.tsx DELETED
@@ -1,4 +0,0 @@
1
- export * from "./victory-state";
2
- export * from "./clone";
3
- export * from "./with-normalized-props";
4
- export * from "./with-container";
@@ -1,2 +0,0 @@
1
- export const DEFAULT_HEIGHT = 300;
2
- export const DEFAULT_WIDTH = 450;
@@ -1,14 +0,0 @@
1
- import { AxisType, Datum, DomainValue } from "../../../types/prop-types";
2
- import { isKeyValueObject } from "../../../victory-util/type-helpers";
3
-
4
- export function getAxisData(data: Datum[], axis: AxisType) {
5
- return data.reduce((acc, datum) => {
6
- if (isKeyValueObject(datum)) {
7
- const value = datum[axis];
8
- if (value && typeof value !== "string") {
9
- acc.push(value);
10
- }
11
- }
12
- return acc;
13
- }, [] as DomainValue[]);
14
- }
@@ -1,78 +0,0 @@
1
- import { get, isNil, orderBy } from "lodash";
2
- import { Datum, DatumValue } from "../../../types/prop-types";
3
- import { isKeyValueObject } from "../../../victory-util/type-helpers";
4
- import { VictoryProviderProps } from "../types";
5
-
6
- type DataProps = Pick<
7
- VictoryProviderProps,
8
- "data" | "x" | "y" | "sortKey" | "sortOrder" | "samples"
9
- >;
10
-
11
- export interface FormattedDatum {
12
- x: DatumValue;
13
- y: DatumValue;
14
- _x: number | Date;
15
- _y: number | Date;
16
- xName?: string;
17
- yName?: string;
18
- [key: string]: DatumValue;
19
- }
20
-
21
- function getValue(datum: Datum, key: string): DatumValue {
22
- if (typeof datum === "object") {
23
- return get(datum, key);
24
- }
25
- return datum;
26
- }
27
-
28
- function getNumericValue(value: DatumValue, fallback: number): number | Date {
29
- if (typeof value === "number" || value instanceof Date) {
30
- return value;
31
- }
32
- return fallback;
33
- }
34
-
35
- export function getData({
36
- x: xAccessor = "x",
37
- y: yAccessor = "y",
38
- sortKey,
39
- sortOrder = "ascending",
40
- data = [],
41
- }: DataProps = {}): FormattedDatum[] {
42
- const formattedData = data.reduce((nonNullData, datum, index) => {
43
- const x = getValue(datum, xAccessor);
44
- const y = getValue(datum, yAccessor);
45
- if (isNil(x) || isNil(y)) {
46
- return nonNullData;
47
- }
48
- const _x = getNumericValue(x, index + 1);
49
- const _y = getNumericValue(y, index + 1);
50
-
51
- // TODO: Get this value if it is different
52
- const xName = typeof x === "string" ? x : undefined;
53
- const yName = typeof y === "string" ? y : undefined;
54
-
55
- const additionalProperties = isKeyValueObject(datum) ? datum : {};
56
-
57
- return [
58
- ...nonNullData,
59
- {
60
- x,
61
- y,
62
- _x,
63
- _y,
64
- // Only set these properties if they exist
65
- ...(xName ? { xName } : {}),
66
- ...(yName ? { yName } : {}),
67
- ...additionalProperties,
68
- },
69
- ];
70
- }, [] as FormattedDatum[]);
71
-
72
- if (sortKey) {
73
- const order = sortOrder === "descending" ? "desc" : "asc";
74
- return orderBy(formattedData, sortKey, order);
75
- }
76
-
77
- return formattedData;
78
- }
@@ -1,62 +0,0 @@
1
- import * as d3Array from "victory-vendor/d3-array";
2
- import { getAxisData } from "./get-axis-data";
3
- import {
4
- getValueForAxis,
5
- isDate,
6
- isTuple,
7
- } from "../../../victory-util/type-helpers";
8
- import { VictoryProviderProps } from "../types";
9
- import {
10
- Tuple,
11
- AxisType,
12
- DomainTuple,
13
- DomainValue,
14
- } from "../../../types/prop-types";
15
-
16
- type DomainProps = Pick<
17
- VictoryProviderProps,
18
- "data" | "domain" | "maxDomain" | "minDomain" | "includeZero"
19
- >;
20
-
21
- // TODO: What should this default value be?
22
- const DEFAULT_MIN = 0;
23
- const DEFAULT_MAX = 1;
24
-
25
- function getDomainFromMinMax(
26
- min: number = DEFAULT_MIN,
27
- max: number = DEFAULT_MAX,
28
- ): Tuple<number> {
29
- // TODO: Victoy currently has some really specific logic in getDomainFromMinMax
30
- // that adds or subtracts a very small number from each domain to avoid the min
31
- // and max being the same value. This has resulted in some weird behavior in the
32
- // past, so we should revisit this.
33
- return [min, max];
34
- }
35
-
36
- export function getDomain(
37
- { data = [], includeZero, ...props }: DomainProps,
38
- axis: AxisType,
39
- ): DomainTuple {
40
- const domainFromProps = getValueForAxis<DomainTuple>(props.domain, axis);
41
- const axisData = getAxisData(data, axis);
42
-
43
- if (includeZero && axis === "y") {
44
- axisData.push(0);
45
- }
46
-
47
- if (isTuple(domainFromProps)) {
48
- return domainFromProps;
49
- }
50
-
51
- const [min, max] = d3Array.extent(axisData) as DomainTuple | Tuple<undefined>;
52
-
53
- const minDomain = getValueForAxis<DomainValue>(props.minDomain, axis) || min;
54
- const maxDomain = getValueForAxis<DomainValue>(props.maxDomain, axis) || max;
55
-
56
- if (isDate(minDomain) && isDate(maxDomain)) {
57
- return [min, max] as Tuple<Date>;
58
- }
59
-
60
- // TODO: There might be an edge case here where we have mixed types
61
- return getDomainFromMinMax(minDomain as number, maxDomain as number);
62
- }
@@ -1,39 +0,0 @@
1
- import { AxisType, Padding, RangeTuple } from "../../../types/prop-types";
2
- import { getPadding } from "../../../victory-util/helpers";
3
- import { getValueForAxis, isTuple } from "../../../victory-util/type-helpers";
4
- import { VictoryProviderProps } from "../types";
5
-
6
- type RangeProps = Pick<
7
- VictoryProviderProps,
8
- "range" | "padding" | "height" | "width"
9
- >;
10
-
11
- // TODO: Should we store these defaults somewhere?
12
- const DEFAULT_HEIGHT = 300;
13
- const DEFAULT_WIDTH = 450;
14
-
15
- // TODO: This does not include polar range
16
- export function getRange(
17
- {
18
- range,
19
- height = DEFAULT_HEIGHT,
20
- width = DEFAULT_WIDTH,
21
- ...props
22
- }: RangeProps,
23
- axis: AxisType,
24
- ) {
25
- const rangeFromProps = getValueForAxis<RangeTuple>(range, axis);
26
-
27
- if (isTuple(rangeFromProps)) {
28
- return rangeFromProps;
29
- }
30
-
31
- // TODO: Convert this to TS
32
- const padding = getPadding({ padding: props.padding }) as Padding;
33
- const rangeForAxis = {
34
- x: [padding.left, width - padding.right],
35
- y: [height - padding.top, padding.bottom],
36
- };
37
-
38
- return rangeForAxis[axis];
39
- }
@@ -1,67 +0,0 @@
1
- import {
2
- scaleLinear,
3
- scaleTime,
4
- scaleLog,
5
- scaleSqrt,
6
- } from "victory-vendor/d3-scale";
7
- import { AxisType, D3ScaleFn, ScalePropType } from "../../../types/prop-types";
8
- import * as Collection from "../../../victory-util/collection";
9
- import {
10
- getValueForAxis,
11
- isFunction,
12
- } from "../../../victory-util/type-helpers";
13
- import { VictoryProviderProps } from "../types";
14
- import { getAxisData } from "./get-axis-data";
15
-
16
- type Scale = ScalePropType | D3ScaleFn;
17
-
18
- type ScaleProps = Pick<VictoryProviderProps, "data" | "scale">;
19
-
20
- const DEFAULT_SCALE = scaleLinear;
21
-
22
- function isD3Scale(scale?: unknown): scale is D3ScaleFn {
23
- return (
24
- isFunction(scale) &&
25
- isFunction(scale().copy) &&
26
- isFunction(scale().domain) &&
27
- isFunction(scale().range)
28
- );
29
- }
30
-
31
- function getD3ScaleFromString(scale: ScalePropType): D3ScaleFn {
32
- switch (scale) {
33
- case "linear":
34
- return scaleLinear;
35
- case "time":
36
- return scaleTime;
37
- case "log":
38
- return scaleLog;
39
- case "sqrt":
40
- return scaleSqrt;
41
- default:
42
- return DEFAULT_SCALE;
43
- }
44
- }
45
-
46
- export function getScale(
47
- { data = [], ...props }: ScaleProps,
48
- axis: AxisType,
49
- ): D3ScaleFn {
50
- const scale = getValueForAxis<Scale>(props.scale, axis);
51
-
52
- if (isD3Scale(scale)) {
53
- return scale;
54
- }
55
-
56
- if (typeof scale === "string") {
57
- return getD3ScaleFromString(scale);
58
- }
59
-
60
- const axisData = getAxisData(data, axis);
61
-
62
- if (Collection.containsDates(axisData)) {
63
- return scaleTime;
64
- }
65
-
66
- return DEFAULT_SCALE;
67
- }
@@ -1,3 +0,0 @@
1
- export * from "./victory-provider";
2
- export * from "./types";
3
- export * from "./with-victory-provider";
@@ -1,32 +0,0 @@
1
- import {
2
- D3ScaleFn,
3
- Datum,
4
- DomainTuple,
5
- RangeTuple,
6
- ScalePropType,
7
- ValueOrAxes,
8
- } from "../../types/prop-types";
9
- import { PaddingProps } from "../../victory-theme/types";
10
-
11
- // These are all the props that are used to calculate data, domain, range, and scale
12
- export interface VictoryCalculatedStateProps {
13
- data?: Datum[];
14
- domain?: ValueOrAxes<DomainTuple>;
15
- height?: number;
16
- includeZero?: boolean;
17
- maxDomain?: ValueOrAxes<number>;
18
- minDomain?: ValueOrAxes<number>;
19
- padding?: PaddingProps;
20
- range?: ValueOrAxes<RangeTuple>;
21
- samples?: number;
22
- scale?: ValueOrAxes<ScalePropType | D3ScaleFn>;
23
- sortKey?: string;
24
- sortOrder?: "ascending" | "descending";
25
- width?: number;
26
- x?: string;
27
- y?: string;
28
- }
29
-
30
- export interface VictoryProviderProps extends VictoryCalculatedStateProps {
31
- children?: React.ReactNode;
32
- }
@@ -1,152 +0,0 @@
1
- import * as React from "react";
2
- import { createContext, useContext } from "react";
3
- import { D3ScaleFn, DomainTuple, ForAxes } from "../../types/prop-types";
4
- import { FormattedDatum, getData } from "./helpers/get-data";
5
- import { getDomain } from "./helpers/get-domain";
6
- import { getRange } from "./helpers/get-range";
7
- import { getScale } from "./helpers/get-scale";
8
- import { VictoryCalculatedStateProps, VictoryProviderProps } from "./types";
9
-
10
- type ScaleType = Required<ForAxes<D3ScaleFn>>;
11
- type DomainType = Required<ForAxes<DomainTuple>>;
12
-
13
- export interface ContextType {
14
- data: FormattedDatum[];
15
- scale: ScaleType;
16
- domain: DomainType;
17
- updateChildProps: (id: symbol, props: VictoryProviderProps | null) => void;
18
- }
19
-
20
- const VictoryContext = createContext<ContextType | null>(null);
21
-
22
- type CollectedProps = { [id: symbol]: VictoryCalculatedStateProps };
23
-
24
- function useNormalizedProps(): ContextType {
25
- const [collectedProps, setCollectedProps] = React.useState<CollectedProps>(
26
- {},
27
- );
28
-
29
- const updateChildProps = React.useCallback(
30
- (id: symbol, newProps: VictoryCalculatedStateProps | null) => {
31
- setCollectedProps((prev) => {
32
- const result = { ...prev };
33
- if (newProps === null) {
34
- delete result[id];
35
- } else {
36
- result[id] = newProps;
37
- }
38
- return result;
39
- });
40
- },
41
- [],
42
- );
43
-
44
- // TEMP: combine all props into a single result:
45
- // TODO: instead, we should intelligently aggregate all these props
46
- const props = Object.values(
47
- collectedProps,
48
- ).reduce<VictoryCalculatedStateProps>(
49
- (result, childProps) => Object.assign(result, childProps),
50
- {},
51
- );
52
-
53
- const data = React.useMemo(() => {
54
- return getData(props);
55
- }, [props]);
56
-
57
- const domain = React.useMemo(() => {
58
- return {
59
- x: getDomain(props, "x"),
60
- y: getDomain(props, "y"),
61
- };
62
- }, [props]);
63
-
64
- const range = React.useMemo(
65
- () => ({
66
- x: getRange(props, "x"),
67
- y: getRange(props, "y"),
68
- }),
69
- [props],
70
- );
71
-
72
- const scale = React.useMemo(() => {
73
- const xBaseScaleFn = getScale(props, "x");
74
- const yBaseScaleFn = getScale(props, "y");
75
-
76
- // @ts-expect-error: This is a valid scale function
77
- const xScaleFn = xBaseScaleFn().domain(domain.x).range(range.x);
78
- // @ts-expect-error: This is a valid scale function
79
- const yScaleFn = yBaseScaleFn().domain(domain.y).range(range.y);
80
-
81
- return {
82
- x: xScaleFn,
83
- y: yScaleFn,
84
- };
85
- }, [props, domain, range]);
86
-
87
- const normalizedProps = React.useMemo(
88
- () => ({
89
- scale,
90
- data,
91
- domain,
92
- updateChildProps,
93
- }),
94
- [scale, data, domain, updateChildProps],
95
- );
96
-
97
- return normalizedProps;
98
- }
99
-
100
- export function VictoryProvider({
101
- children,
102
- // Disabled due to TODO below which will require this obj. spread
103
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
104
- ...providerProps
105
- }: VictoryProviderProps) {
106
- const value = useNormalizedProps();
107
-
108
- // TODO: sync the providerProps
109
-
110
- return (
111
- <VictoryContext.Provider value={value}>{children}</VictoryContext.Provider>
112
- );
113
- }
114
-
115
- export function useVictoryContextMaybe<T>(
116
- selector: (value: ContextType | null) => T,
117
- ): T {
118
- const context = useContext(VictoryContext);
119
- return selector(context);
120
- }
121
-
122
- export function useVictoryContext<T>(selector: (value: ContextType) => T): T {
123
- const context = useContext(VictoryContext);
124
- if (!context) {
125
- throw new Error("useVictoryContext must be used within a VictoryProvider");
126
- }
127
- return selector(context);
128
- }
129
-
130
- export function useScale() {
131
- return useVictoryContext<ScaleType>((value) => value.scale);
132
- }
133
-
134
- export function useData() {
135
- return useVictoryContext<FormattedDatum[]>((value) => value.data);
136
- }
137
-
138
- export function useDomain() {
139
- return useVictoryContext<DomainType>((value) => value.domain);
140
- }
141
-
142
- // This function keeps props in sync betwen the VictoryProvider and child components
143
- export function useVictoryProviderSync(props: VictoryCalculatedStateProps) {
144
- const updateChildProps = useVictoryContext((value) => value.updateChildProps);
145
- const [myId] = React.useState(() =>
146
- Symbol("UniqueIdFor(VictoryProviderChild)"),
147
- );
148
-
149
- React.useEffect(() => {
150
- updateChildProps(myId, props);
151
- }, [updateChildProps, props, myId]);
152
- }