grommet 2.32.2 → 2.33.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 (63) hide show
  1. package/all/stories/CustomThemed/ExtendTheme.tsx +2 -4
  2. package/components/Chart/Chart.js +20 -20
  3. package/components/Chart/calcs.js +163 -92
  4. package/components/Chart/index.d.ts +10 -4
  5. package/components/Chart/propTypes.js +10 -1
  6. package/components/Chart/stories/{Vertical.js → Horizontal.js} +34 -6
  7. package/components/Chart/utils.js +68 -64
  8. package/components/DataChart/DataChart.js +114 -68
  9. package/components/DataChart/Detail.js +55 -21
  10. package/components/DataChart/XAxis.js +33 -6
  11. package/components/DataChart/XGuide.js +14 -1
  12. package/components/DataChart/YAxis.js +58 -39
  13. package/components/DataChart/YGuide.js +13 -10
  14. package/components/DataChart/index.d.ts +2 -0
  15. package/components/DataChart/propTypes.js +3 -1
  16. package/components/DataChart/stories/Horizontal.js +63 -0
  17. package/components/DataChart/utils.js +27 -2
  18. package/components/DataFilter/DataFilter.js +25 -2
  19. package/components/DataSort/DataSort.js +1 -1
  20. package/components/DataTable/buildState.js +6 -1
  21. package/components/DataTableColumns/DataTableColumns.js +2 -2
  22. package/components/DateInput/DateInput.js +22 -2
  23. package/components/Footer/stories/DoubleFooter.js +6 -2
  24. package/components/Footer/stories/Sitemap.js +0 -2
  25. package/components/Footer/stories/Social.js +2 -2
  26. package/components/Menu/Menu.js +9 -6
  27. package/components/Select/Select.js +1 -1
  28. package/components/SelectMultiple/SelectMultipleContainer.js +5 -5
  29. package/components/Tabs/Tabs.js +2 -1
  30. package/es6/all/stories/CustomThemed/ExtendTheme.tsx +2 -4
  31. package/es6/components/Chart/Chart.js +20 -20
  32. package/es6/components/Chart/calcs.js +164 -93
  33. package/es6/components/Chart/index.d.ts +10 -4
  34. package/es6/components/Chart/propTypes.js +10 -1
  35. package/es6/components/Chart/stories/{Vertical.js → Horizontal.js} +32 -4
  36. package/es6/components/Chart/utils.js +66 -63
  37. package/es6/components/DataChart/DataChart.js +115 -69
  38. package/es6/components/DataChart/Detail.js +55 -21
  39. package/es6/components/DataChart/XAxis.js +33 -6
  40. package/es6/components/DataChart/XGuide.js +14 -1
  41. package/es6/components/DataChart/YAxis.js +59 -40
  42. package/es6/components/DataChart/YGuide.js +13 -10
  43. package/es6/components/DataChart/index.d.ts +2 -0
  44. package/es6/components/DataChart/propTypes.js +3 -1
  45. package/es6/components/DataChart/stories/Horizontal.js +57 -0
  46. package/es6/components/DataChart/utils.js +22 -0
  47. package/es6/components/DataFilter/DataFilter.js +27 -3
  48. package/es6/components/DataSort/DataSort.js +1 -1
  49. package/es6/components/DataTable/buildState.js +6 -1
  50. package/es6/components/DataTableColumns/DataTableColumns.js +2 -2
  51. package/es6/components/DateInput/DateInput.js +22 -2
  52. package/es6/components/Footer/stories/DoubleFooter.js +6 -2
  53. package/es6/components/Footer/stories/Sitemap.js +0 -2
  54. package/es6/components/Footer/stories/Social.js +2 -2
  55. package/es6/components/Menu/Menu.js +9 -6
  56. package/es6/components/Select/Select.js +1 -1
  57. package/es6/components/SelectMultiple/SelectMultipleContainer.js +5 -5
  58. package/es6/components/Tabs/Tabs.js +2 -1
  59. package/es6/themes/base.d.ts +5 -1
  60. package/grommet.min.js +1 -1
  61. package/grommet.min.js.map +1 -1
  62. package/package.json +37 -40
  63. package/themes/base.d.ts +5 -1
@@ -272,15 +272,15 @@ var Menu = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
272
272
  }));
273
273
  } : content));
274
274
  var menuItem = function menuItem(item, index) {
275
- var _theme$menu$item, _theme$menu$item2, _theme$menu$item3;
275
+ var _theme$menu$item, _theme$menu$item2, _theme$menu$item3, _theme$menu$item4;
276
276
  // Determine whether the label is done as a child or
277
277
  // as an option Button kind property.
278
278
  var child = !theme.button.option ? /*#__PURE__*/_react["default"].createElement(_Box.Box, {
279
- align: "start",
279
+ align: ((_theme$menu$item = theme.menu.item) == null ? void 0 : _theme$menu$item.align) || 'start',
280
280
  pad: "small",
281
281
  direction: "row",
282
- gap: item.gap || ((_theme$menu$item = theme.menu.item) == null ? void 0 : _theme$menu$item.gap),
283
- justify: item.justify || ((_theme$menu$item2 = theme.menu.item) == null ? void 0 : _theme$menu$item2.justify)
282
+ gap: item.gap || ((_theme$menu$item2 = theme.menu.item) == null ? void 0 : _theme$menu$item2.gap),
283
+ justify: item.justify || ((_theme$menu$item3 = theme.menu.item) == null ? void 0 : _theme$menu$item3.justify)
284
284
  }, item.reverse && item.label, item.icon, !item.reverse && item.label) : undefined;
285
285
 
286
286
  // if we have a child, turn on plain, and hoverIndicator
@@ -306,7 +306,7 @@ var Menu = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
306
306
  kind: !child ? 'option' : undefined,
307
307
  hoverIndicator: !child ? undefined : 'background'
308
308
  }, theme.menu.item, {
309
- justify: item.justify || ((_theme$menu$item3 = theme.menu.item) == null ? void 0 : _theme$menu$item3.justify)
309
+ justify: item.justify || ((_theme$menu$item4 = theme.menu.item) == null ? void 0 : _theme$menu$item4.justify)
310
310
  }, !child ? item : _extends({}, item, {
311
311
  gap: undefined,
312
312
  icon: undefined,
@@ -391,7 +391,10 @@ var Menu = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
391
391
  overflow: "auto",
392
392
  role: "menu",
393
393
  a11yTitle: a11y
394
- }, menuContent), !initialAlignTop && (alignControlMirror === 'bottom' || align.bottom === 'bottom') ? controlMirror : undefined))
394
+ }, menuContent), !initialAlignTop && (
395
+ // don't show controlMirror if caller is using
396
+ // align.bottom === 'top'
397
+ alignControlMirror === 'bottom' && !align.bottom === 'top' || align.bottom === 'bottom') ? controlMirror : undefined))
395
398
  }), content));
396
399
  });
397
400
  exports.Menu = Menu;
@@ -223,7 +223,7 @@ var Select = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
223
223
  // element to show, trumps inputValue
224
224
  var selectValue = (0, _react.useMemo)(function () {
225
225
  if (valueLabel instanceof Function) {
226
- if (value) return valueLabel(value);
226
+ if (value || value === 0 || value === false) return valueLabel(value);
227
227
  } else if (valueLabel) return valueLabel;else if ( /*#__PURE__*/_react["default"].isValidElement(value)) return value; // deprecated
228
228
  return undefined;
229
229
  }, [value, valueLabel]);
@@ -101,7 +101,7 @@ var SelectMultipleContainer = /*#__PURE__*/(0, _react.forwardRef)(function (_ref
101
101
  }, []);
102
102
  (0, _react.useEffect)(function () {
103
103
  var optionsNode = optionsRef.current;
104
- if (optionsNode.children) {
104
+ if (optionsNode != null && optionsNode.children) {
105
105
  var optionNode = optionsNode.children[activeIndex];
106
106
  if (optionNode) optionNode.focus();
107
107
  }
@@ -149,7 +149,7 @@ var SelectMultipleContainer = /*#__PURE__*/(0, _react.forwardRef)(function (_ref
149
149
  var onNextOption = (0, _react.useCallback)(function (event) {
150
150
  event.preventDefault();
151
151
  var nextActiveIndex = activeIndex + 1;
152
- if (nextActiveIndex !== options.length) {
152
+ if (nextActiveIndex !== (options == null ? void 0 : options.length)) {
153
153
  setActiveIndex(nextActiveIndex);
154
154
  setKeyboardNavigation(true);
155
155
  }
@@ -196,7 +196,7 @@ var SelectMultipleContainer = /*#__PURE__*/(0, _react.forwardRef)(function (_ref
196
196
  };
197
197
  }, [keyboardNavigation]);
198
198
  var onSelectOption = (0, _react.useCallback)(function (event) {
199
- if (!isDisabled(activeIndex) && activeIndex >= 0 && activeIndex < options.length) {
199
+ if (!isDisabled(activeIndex) && activeIndex >= 0 && activeIndex < (options == null ? void 0 : options.length)) {
200
200
  event.preventDefault(); // prevent submitting forms
201
201
  selectOption(activeIndex)(event);
202
202
  }
@@ -225,7 +225,7 @@ var SelectMultipleContainer = /*#__PURE__*/(0, _react.forwardRef)(function (_ref
225
225
  if (value.length === limit) {
226
226
  var newDisabled = [].concat(disabledProp);
227
227
  // disable everything that is not selected
228
- for (var i = 0; i < options.length; i += 1) {
228
+ for (var i = 0; i < (options == null ? void 0 : options.length); i += 1) {
229
229
  if (!isSelected(i) && !originallyDisabled(i)) {
230
230
  newDisabled.push(options[i]);
231
231
  }
@@ -318,7 +318,7 @@ var SelectMultipleContainer = /*#__PURE__*/(0, _react.forwardRef)(function (_ref
318
318
  setActiveIndex(-1);
319
319
  onSearch(nextSearch);
320
320
  }
321
- }))), helpContent, options.length > 0 ? /*#__PURE__*/_react["default"].createElement(_StyledSelect.OptionsContainer, {
321
+ }))), helpContent, (options == null ? void 0 : options.length) > 0 ? /*#__PURE__*/_react["default"].createElement(_StyledSelect.OptionsContainer, {
322
322
  role: "listbox",
323
323
  tabIndex: "0",
324
324
  ref: optionsRef,
@@ -182,8 +182,9 @@ var Tabs = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
182
182
  }
183
183
  };
184
184
  (0, _react.useEffect)(function () {
185
+ var _tabRefs$activeIndex;
185
186
  // if the active tab isn't visible scroll to it
186
- if (overflow && tabRefs && tabRefs[activeIndex].current && !isVisible(activeIndex)) scrollTo(activeIndex, true);
187
+ if (overflow && tabRefs && (_tabRefs$activeIndex = tabRefs[activeIndex]) != null && _tabRefs$activeIndex.current && !isVisible(activeIndex)) scrollTo(activeIndex, true);
187
188
  }, [overflow, activeIndex, tabRefs, isVisible, scrollTo]);
188
189
  (0, _react.useEffect)(function () {
189
190
  // scroll focus item into view if it is not already visible
@@ -13,7 +13,7 @@ const custom: ThemeType = {
13
13
  },
14
14
  anchor: {
15
15
  extend: (props) => css`
16
- color: ${props.href ? 'green' : 'red'};
16
+ color: ${props['href'] ? 'green' : 'red'};
17
17
  `,
18
18
  },
19
19
  button: {
@@ -44,7 +44,7 @@ const custom: ThemeType = {
44
44
 
45
45
  // Type annotations can only be used in TypeScript files.
46
46
  // Remove ': TReact.FC' is you are not using TypeScript.
47
- const ExtendTheme: React.FC = () => {
47
+ export const Extend = () => {
48
48
  return (
49
49
  <Grommet theme={custom}>
50
50
  <Box pad="small" gap="medium" width="medium">
@@ -62,8 +62,6 @@ const ExtendTheme: React.FC = () => {
62
62
  );
63
63
  };
64
64
 
65
- export const Extend = () => <ExtendTheme />;
66
-
67
65
  export default {
68
66
  title: 'Utilities/Theme/Custom Themed/Extend',
69
67
  };
@@ -24,7 +24,8 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
24
24
  boundsProp = _ref.bounds,
25
25
  color = _ref.color,
26
26
  dash = _ref.dash,
27
- direction = _ref.direction,
27
+ _ref$direction = _ref.direction,
28
+ direction = _ref$direction === void 0 ? 'vertical' : _ref$direction,
28
29
  gap = _ref.gap,
29
30
  id = _ref.id,
30
31
  onClick = _ref.onClick,
@@ -36,8 +37,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
36
37
  pattern = _ref.pattern,
37
38
  point = _ref.point,
38
39
  round = _ref.round,
39
- _ref$size = _ref.size,
40
- sizeProp = _ref$size === void 0 ? defaultSize : _ref$size,
40
+ sizeProp = _ref.size,
41
41
  _ref$thickness = _ref.thickness,
42
42
  thickness = _ref$thickness === void 0 ? 'medium' : _ref$thickness,
43
43
  _ref$type = _ref.type,
@@ -50,11 +50,11 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
50
50
  var values = useMemo(function () {
51
51
  return normalizeValues(valuesProp);
52
52
  }, [valuesProp]);
53
- var vertical = useMemo(function () {
54
- return direction === 'vertical';
53
+ var horizontal = useMemo(function () {
54
+ return direction === 'horizontal';
55
55
  }, [direction]);
56
56
 
57
- // bounds is { x: { min, max }, y: { min, max } }, accounting for direction
57
+ // bounds is { x: { min, max }, y: { min, max } }
58
58
  var bounds = useMemo(function () {
59
59
  return normalizeBounds(boundsProp, values, direction);
60
60
  }, [direction, boundsProp, values]);
@@ -120,7 +120,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
120
120
 
121
121
  // autoSize is how wide or tall we'd pefer based on the number of values
122
122
  var autoSize = strokeWidth * values.length + (values.length - 1) * gapWidth;
123
- var sizeWidth = typeof sizeProp === 'string' ? sizeProp : sizeProp.width || defaultSize.width;
123
+ var sizeWidth = typeof sizeProp === 'string' ? sizeProp : (sizeProp == null ? void 0 : sizeProp.width) || horizontal && defaultSize.height || defaultSize.width;
124
124
  var width;
125
125
  if (sizeWidth === 'full' || sizeWidth === 'fill') {
126
126
  width = containerSize[0];
@@ -129,7 +129,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
129
129
  } else {
130
130
  width = parseMetricToNum(theme.global.size[sizeWidth] || sizeWidth);
131
131
  }
132
- var sizeHeight = typeof sizeProp === 'string' ? sizeProp : sizeProp.height || defaultSize.height;
132
+ var sizeHeight = typeof sizeProp === 'string' ? sizeProp : (sizeProp == null ? void 0 : sizeProp.height) || horizontal && defaultSize.width || defaultSize.height;
133
133
  var height;
134
134
  if (sizeHeight === 'full' || sizeHeight === 'fill') {
135
135
  height = containerSize[1];
@@ -142,7 +142,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
142
142
  width: width,
143
143
  height: height
144
144
  };
145
- }, [containerSize, gap, sizeProp, strokeWidth, theme.global.edgeSize, theme.global.size, values]);
145
+ }, [containerSize, gap, horizontal, sizeProp, strokeWidth, theme.global.edgeSize, theme.global.size, values]);
146
146
 
147
147
  // scale is { x, y }
148
148
  var scale = useMemo(function () {
@@ -193,7 +193,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
193
193
  // rendering helpers, to make rendering code easier to understand
194
194
 
195
195
  var valueCoords = function valueCoords(x, y) {
196
- return vertical ? [y, x] : [x, y];
196
+ return horizontal ? [y, x] : [x, y];
197
197
  };
198
198
 
199
199
  // Converts values to drawing coordinates.
@@ -201,8 +201,8 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
201
201
  var valueToCoordinate = function valueToCoordinate(xValue, yValue) {
202
202
  var y = (yValue - bounds.y.min) * scale.y + inset.top;
203
203
  return [(xValue - bounds.x.min) * scale.x + inset.left,
204
- // vertical grows y top down, horizontal grows y bottom up
205
- vertical ? y : size.height - y];
204
+ // horizontal grows y top down, vertical grows y bottom up
205
+ horizontal ? y : size.height - y];
206
206
  };
207
207
  var useGradient = color && Array.isArray(color);
208
208
  var patternId;
@@ -225,10 +225,10 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
225
225
  valueRest = _objectWithoutPropertiesLoose(valueArg, _excluded2);
226
226
  var key = "p-" + index;
227
227
  // Math.min/max are to handle negative values
228
- var _valueCoords = valueCoords(value[0], value.length === 2 ? Math.min(Math.max(0, vertical ? bounds.x.min : bounds.y.min), value[1]) : Math.min(value[1], value[2])),
228
+ var _valueCoords = valueCoords(value[0], value.length === 2 ? Math.min(Math.max(0, horizontal ? bounds.x.min : bounds.y.min), value[1]) : Math.min(value[1], value[2])),
229
229
  startX = _valueCoords[0],
230
230
  startY = _valueCoords[1];
231
- var _valueCoords2 = valueCoords(value[0], value.length === 2 ? Math.max(Math.min(0, vertical ? bounds.x.max : bounds.y.max), value[1]) : Math.max(value[1], value[2])),
231
+ var _valueCoords2 = valueCoords(value[0], value.length === 2 ? Math.max(Math.min(0, horizontal ? bounds.x.max : bounds.y.max), value[1]) : Math.max(value[1], value[2])),
232
232
  endX = _valueCoords2[0],
233
233
  endY = _valueCoords2[1];
234
234
  var d = "M " + valueToCoordinate(startX, startY).join(',') + (" L " + valueToCoordinate(endX, endY).join(','));
@@ -331,7 +331,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
331
331
  var value = _ref8.value;
332
332
  var _valueCoords6 = valueCoords(value[0],
333
333
  // Math.max() is to account for value[1] being negative
334
- value.length === 2 ? Math.max(0, vertical ? bounds.x.min : bounds.y.min) : value[1]),
334
+ value.length === 2 ? Math.max(0, horizontal ? bounds.x.min : bounds.y.min) : value[1]),
335
335
  x = _valueCoords6[0],
336
336
  y = _valueCoords6[1];
337
337
  d += " L " + valueToCoordinate(x, y).join(',');
@@ -468,7 +468,7 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
468
468
  }, contents);
469
469
  var defs = [];
470
470
  var gradientRect;
471
- if (useGradient && size.height) {
471
+ if (useGradient && (horizontal && size.width || size.height)) {
472
472
  var uniqueGradientId = color.map(function (element) {
473
473
  return element.color;
474
474
  }).join('-');
@@ -479,16 +479,16 @@ var Chart = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
479
479
  id: gradientId,
480
480
  x1: 0,
481
481
  y1: 0,
482
- x2: 0,
483
- y2: 1
482
+ x2: horizontal ? 1 : 0,
483
+ y2: horizontal ? 0 : 1
484
484
  }, color.slice(0).sort(function (c1, c2) {
485
- return c2.value - c1.value;
485
+ return horizontal ? c1.value - c2.value : c2.value - c1.value;
486
486
  }).map(function (_ref10) {
487
487
  var value = _ref10.value,
488
488
  gradientColor = _ref10.color;
489
489
  return /*#__PURE__*/React.createElement("stop", {
490
490
  key: value,
491
- offset: (size.height - (value - bounds.y.min) * scale.y) / size.height,
491
+ offset: horizontal ? (value - bounds.x.min) * scale.x / size.width : (size.height - (value - bounds.y.min) * scale.y) / size.height,
492
492
  stopColor: normalizeColor(gradientColor, theme)
493
493
  });
494
494
  })));
@@ -1,4 +1,4 @@
1
- import { normalizeValues } from './utils';
1
+ import { calcMinMax, normalizeValues } from './utils';
2
2
  var thicknessPad = {
3
3
  xlarge: 'large',
4
4
  large: 'medium',
@@ -9,6 +9,37 @@ var thicknessPad = {
9
9
  export var round = function round(value, decimals) {
10
10
  return Number(Math.round(value + "e" + decimals) + "e-" + decimals);
11
11
  };
12
+
13
+ // Normalize coarseness to an object.
14
+ // Backwards compatible has no coarseness for x-axis.
15
+ var normalizeCoarseness = function normalizeCoarseness(coarseness, direction) {
16
+ var result;
17
+ if (Array.isArray(coarseness)) result = {
18
+ x: coarseness[0],
19
+ y: coarseness[1]
20
+ };else if (typeof coarseness === 'object') result = coarseness;else if (coarseness) result = {
21
+ x: undefined,
22
+ y: coarseness
23
+ };else result = direction === 'horizontal' ? {
24
+ x: 5,
25
+ y: undefined
26
+ } : {
27
+ x: undefined,
28
+ y: 5
29
+ };
30
+ return result;
31
+ };
32
+ var normalizeSteps = function normalizeSteps(steps) {
33
+ var result;
34
+ if (Array.isArray(steps)) result = {
35
+ x: steps[0],
36
+ y: steps[1]
37
+ };else if (typeof steps === 'object') result = steps;else result = {
38
+ x: 1,
39
+ y: 1
40
+ };
41
+ return result;
42
+ };
12
43
  var alignMax = function alignMax(value, interval) {
13
44
  if (value > 0) return value - value % interval + interval;
14
45
  if (value < 0) return value + value % interval;
@@ -19,56 +50,60 @@ var alignMin = function alignMin(value, interval) {
19
50
  if (value < 0) return value - value % interval - interval;
20
51
  return value;
21
52
  };
22
- export var calcBounds = function calcBounds(values, options) {
53
+ var adjustToShowZero = function adjustToShowZero(minArg, maxArg, steps) {
54
+ var min = minArg;
55
+ var max = maxArg;
56
+ if (min < 0 && max > 0 && Math.abs(min) !== Math.abs(max)) {
57
+ // Adjust min and max when crossing 0 to ensure 0 will be shown on
58
+ // the axis based on the number of steps.
59
+ if (steps === 1) {
60
+ var largest = Math.max(Math.abs(min), Math.abs(max));
61
+ min = -largest;
62
+ max = largest;
63
+ } else {
64
+ var stepInterval = (max - min) / steps;
65
+ var minSteps = min / stepInterval;
66
+ var maxSteps = max / stepInterval;
67
+ if (Math.abs(minSteps) < Math.abs(maxSteps)) {
68
+ // more above than below
69
+ stepInterval = max / Math.floor(maxSteps);
70
+ max = stepInterval * Math.floor(maxSteps);
71
+ min = stepInterval * Math.floor(minSteps);
72
+ } else {
73
+ // more below than above
74
+ stepInterval = Math.abs(min / Math.ceil(minSteps));
75
+ min = stepInterval * Math.ceil(minSteps);
76
+ max = stepInterval * Math.ceil(maxSteps);
77
+ }
78
+ }
79
+ }
80
+ return [min, max];
81
+ };
82
+ export var calcBounds = function calcBounds(valuesArg, options) {
23
83
  if (options === void 0) {
24
84
  options = {};
25
85
  }
26
86
  // coarseness influences the rounding of the bounds, the smaller the
27
- // number, the more the bounds will be rounded. e.g. 111 -> 110 -> 100
28
- // Normalize to an array. Backwards compatible has no coarseness for x-axis
29
- var coarseness = Array.isArray(options.coarseness) && options.coarseness || options.coarseness && [undefined, options.coarseness] || [undefined, 5];
30
- var coarseX = coarseness[0],
31
- coarseY = coarseness[1];
32
- // the number of steps is one less than the number of labels
33
- var steps = options.steps || [1, 1];
34
- var stepsY = steps[1];
35
- var calcValues = normalizeValues(values || []);
36
-
37
- // min and max values
38
- var minX;
39
- var maxX;
40
- var minY;
41
- var maxY;
42
- if (calcValues.length) {
43
- // Calculate the max and min values.
44
- calcValues.filter(function (value) {
45
- return value !== undefined;
46
- }).forEach(function (value) {
47
- var x = value.value[0];
48
- if (x !== undefined) {
49
- minX = minX === undefined ? x : Math.min(minX, x);
50
- maxX = maxX === undefined ? x : Math.max(maxX, x);
51
- }
52
- var y = value.value[1];
53
- if (y !== undefined) {
54
- minY = minY === undefined ? y : Math.min(minY, y);
55
- maxY = maxY === undefined ? y : Math.max(maxY, y);
56
- }
57
- // handle ranges of values
58
- var y2 = value.value[2];
59
- if (y2 !== undefined) {
60
- minY = Math.min(minY, y2);
61
- maxY = Math.max(maxY, y2);
62
- }
63
- });
87
+ // number, the more the bounds will be rounded. e.g. 111 -> 110 -> 100.
88
+ var _normalizeCoarseness = normalizeCoarseness(options.coarseness, options.direction),
89
+ coarseX = _normalizeCoarseness.x,
90
+ coarseY = _normalizeCoarseness.y;
64
91
 
65
- // when max === min, offset them so we can show something
66
- if (maxX === minX) {
67
- if (maxX > 0) minX = maxX - 1;else maxX = minX + 1;
68
- }
69
- if (maxY === minY) {
70
- if (maxY > 0) minY = maxY - 1;else maxY = minY + 1;
71
- }
92
+ // the number of steps is one less than the number of labels
93
+ var _normalizeSteps = normalizeSteps(options.steps),
94
+ stepsX = _normalizeSteps.x,
95
+ stepsY = _normalizeSteps.y;
96
+ var values = normalizeValues(valuesArg || []);
97
+ var result;
98
+ if (values.length) {
99
+ // min and max values
100
+ var _calcMinMax = calcMinMax(values, options.direction),
101
+ _calcMinMax$x = _calcMinMax.x,
102
+ minX = _calcMinMax$x.min,
103
+ maxX = _calcMinMax$x.max,
104
+ _calcMinMax$y = _calcMinMax.y,
105
+ minY = _calcMinMax$y.min,
106
+ maxY = _calcMinMax$y.max;
72
107
 
73
108
  // Calculate some reasonable bounds based on the max and min values.
74
109
  // This is so values like 87342.12 don't end up being displayed as the
@@ -85,35 +120,41 @@ export var calcBounds = function calcBounds(values, options) {
85
120
  minY = alignMin(minY, intervalY);
86
121
  maxY = alignMax(maxY, intervalY);
87
122
  }
88
- if (minY < 0 && maxY > 0 && Math.abs(minY) !== Math.abs(maxY)) {
89
- // Adjust min and max when crossing 0 to ensure 0 will be shown on
90
- // the Y axis based on the number of steps.
91
- if (stepsY === 1) {
92
- var largest = Math.max(Math.abs(minY), Math.abs(maxY));
93
- minY = -largest;
94
- maxY = largest;
95
- } else {
96
- var stepInterval = (maxY - minY) / stepsY;
97
- var minSteps = minY / stepInterval;
98
- var maxSteps = maxY / stepInterval;
99
- if (Math.abs(minSteps) < Math.abs(maxSteps)) {
100
- // more above than below
101
- stepInterval = maxY / Math.floor(maxSteps);
102
- maxY = stepInterval * Math.floor(maxSteps);
103
- minY = stepInterval * Math.floor(minSteps);
104
- } else {
105
- // more below than above
106
- stepInterval = Math.abs(minY / Math.ceil(minSteps));
107
- minY = stepInterval * Math.ceil(minSteps);
108
- maxY = stepInterval * Math.ceil(maxSteps);
109
- }
110
- }
123
+ if (options.direction === 'horizontal') {
124
+ var _adjustToShowZero = adjustToShowZero(minX, maxX, stepsX);
125
+ minX = _adjustToShowZero[0];
126
+ maxX = _adjustToShowZero[1];
127
+ } else {
128
+ var _adjustToShowZero2 = adjustToShowZero(minY, maxY, stepsY);
129
+ minY = _adjustToShowZero2[0];
130
+ maxY = _adjustToShowZero2[1];
111
131
  }
132
+
133
+ // if options.direction is present, the results are delivered in { x, y }
134
+ // object structure. If options.direction is not present, the results are
135
+ // delivered in [x, y] array structure, for backwards compatibility
136
+ result = options.direction ? {
137
+ x: {
138
+ min: minX,
139
+ max: maxX
140
+ },
141
+ y: {
142
+ min: minY,
143
+ max: maxY
144
+ }
145
+ } : [[minX, maxX], [minY, maxY]];
146
+ } else {
147
+ result = options.direction ? {
148
+ x: {},
149
+ y: {}
150
+ } : [[], []];
112
151
  }
113
- var bounds;
114
- if (calcValues.length) bounds = [[minX, maxX], [minY, maxY]];else bounds = [[], []];
115
- return bounds;
152
+ return result;
116
153
  };
154
+
155
+ // if options.direction is present, the results are delivered in { x, y }
156
+ // object structure. If options.direction is not present, the results are
157
+ // delivered in [x, y] array structure, for backwards compatibility
117
158
  export var calcs = function calcs(values, options) {
118
159
  if (values === void 0) {
119
160
  values = [];
@@ -121,37 +162,64 @@ export var calcs = function calcs(values, options) {
121
162
  if (options === void 0) {
122
163
  options = {};
123
164
  }
165
+ var horizontal = options.direction === 'horizontal';
166
+
124
167
  // the number of steps is one less than the number of labels
125
- var steps = options.steps || [1, 1];
126
- var stepsX = steps[0],
127
- stepsY = steps[1];
168
+ var _normalizeSteps2 = normalizeSteps(options.steps),
169
+ stepsX = _normalizeSteps2.x,
170
+ stepsY = _normalizeSteps2.y;
171
+
172
+ // bounds is { x: { min, max }, y: { min, max } } when options.direction is
173
+ // present and [[min, max], [min, max]] if not, for backwards compatibility
128
174
  var bounds = options.bounds || calcBounds(values, options);
129
- if (options.min !== undefined) bounds[1][0] = options.min;
130
- if (options.max !== undefined) bounds[1][1] = options.max;
131
- var boundsX = bounds[0],
132
- boundsY = bounds[1];
133
- var boundsXmin = boundsX[0],
134
- boundsXmax = boundsX[1];
135
- var boundsYmin = boundsY[0],
136
- boundsYmax = boundsY[1];
137
- var dimensions = [round(boundsXmax - boundsXmin, 2), round(boundsYmax - boundsYmin, 2)];
138
- var dimensionsX = dimensions[0],
139
- dimensionsY = dimensions[1];
175
+ if (options.min !== undefined) {
176
+ if (options.direction) {
177
+ if (horizontal) bounds.x.min = options.min;else bounds.y.min = options.min;
178
+ } else bounds[1][0] = options.min;
179
+ }
180
+ if (options.max !== undefined) {
181
+ if (options.direction) {
182
+ if (horizontal) bounds.y.max = options.max;else bounds.x.max = options.max;
183
+ } else bounds[1][1] = options.max;
184
+ }
185
+ var _ref = options.direction ? bounds : {
186
+ x: {
187
+ min: bounds[0][0],
188
+ max: bounds[0][1]
189
+ },
190
+ y: {
191
+ min: bounds[1][0],
192
+ max: bounds[1][1]
193
+ }
194
+ },
195
+ _ref$x = _ref.x,
196
+ minX = _ref$x.min,
197
+ maxX = _ref$x.max,
198
+ _ref$y = _ref.y,
199
+ minY = _ref$y.min,
200
+ maxY = _ref$y.max;
201
+ var width = round(maxX - minX, 2);
202
+ var height = round(maxY - minY, 2);
203
+ var dimensions = options.direction ? {
204
+ width: width,
205
+ height: height
206
+ } : [width, height];
140
207
 
141
208
  // Calculate x and y axis values across the specfied number of steps.
142
209
  var yAxis = [];
143
- var y = boundsYmax;
210
+ var y = maxY;
144
211
  // To deal with javascript math limitations, round the step with 4 decimal
145
212
  // places and then push the values with 2 decimal places
146
- var yStepInterval = round(dimensionsY / stepsY, 4);
147
- while (round(y, 2) >= boundsYmin) {
213
+ var yStepInterval = round(height / stepsY, 4);
214
+ while (round(y, 2) >= minY) {
148
215
  yAxis.push(round(y, 2));
149
216
  y -= yStepInterval;
150
217
  }
218
+ if (horizontal) yAxis.reverse();
151
219
  var xAxis = [];
152
- var x = boundsXmin;
153
- var xStepInterval = round(dimensionsX / stepsX, 4);
154
- while (round(x, 2) <= boundsXmax) {
220
+ var x = minX;
221
+ var xStepInterval = round(width / stepsX, 4);
222
+ while (round(x, 2) <= maxX) {
155
223
  xAxis.push(round(x, 2));
156
224
  x += xStepInterval;
157
225
  }
@@ -178,7 +246,10 @@ export var calcs = function calcs(values, options) {
178
246
  }
179
247
  var pad = thicknessPad[thickness];
180
248
  return {
181
- axis: [xAxis, yAxis],
249
+ axis: options.direction ? {
250
+ x: xAxis,
251
+ y: yAxis
252
+ } : [xAxis, yAxis],
182
253
  bounds: bounds,
183
254
  dimensions: dimensions,
184
255
  pad: pad,
@@ -16,7 +16,10 @@ export interface ChartProps {
16
16
  animate?: boolean;
17
17
  gridArea?: GridAreaType;
18
18
  margin?: MarginType;
19
- bounds?: number[][];
19
+ // caller must align with direction
20
+ bounds?:
21
+ | { x: { min: number; max: number }; y: { min: number; max: number } }
22
+ | number[][];
20
23
  color?:
21
24
  | ColorType
22
25
  | { color: ColorType; value: number | number[] }[]
@@ -105,12 +108,15 @@ export interface ChartExtendedProps
105
108
 
106
109
  declare const Chart: React.FC<ChartExtendedProps>;
107
110
 
108
- type Bounds = [[number, number], [number, number]] | [[], []];
111
+ type Bounds =
112
+ | { x: { min: number; max: number }; y: { min: number; max: number } }
113
+ | [[number, number], [number, number]]
114
+ | [[], []];
109
115
 
110
116
  interface CalcsResult {
111
- axis: [number[], number[]];
117
+ axis: { x: number[]; y: number[] } | [number[], number[]];
112
118
  bounds: Bounds;
113
- dimensions: [number, number];
119
+ dimensions: { width: number; height: number } | [number, number];
114
120
  pad: string;
115
121
  thickness: string;
116
122
  }
@@ -6,7 +6,16 @@ var PropType = {};
6
6
  if (process.env.NODE_ENV !== 'production') {
7
7
  PropType = _extends({}, genericProps, {
8
8
  animate: PropTypes.bool,
9
- bounds: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)),
9
+ bounds: PropTypes.oneOfType([PropTypes.shape({
10
+ x: PropTypes.shape({
11
+ min: PropTypes.number,
12
+ max: PropTypes.number
13
+ }),
14
+ y: PropTypes.shape({
15
+ min: PropTypes.number,
16
+ max: PropTypes.number
17
+ })
18
+ }), PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number))]),
10
19
  color: PropTypes.oneOfType([colorPropType, PropTypes.shape({
11
20
  color: colorPropType,
12
21
  // deprecated, use top level 'opacity'