victory-core 37.1.2 → 37.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.
@@ -1,4 +1,4 @@
1
- import { VictoryThemeDefinition } from "./types";
1
+ import { VictoryThemeDefinition, VictoryThemePalette } from "./types";
2
2
 
3
3
  // *
4
4
  // * Colors
@@ -9,7 +9,7 @@ const lime300 = "#DCE775";
9
9
  const lightGreen500 = "#8BC34A";
10
10
  const teal700 = "#00796B";
11
11
  const cyan900 = "#006064";
12
- const colors = [
12
+ const colorScale = [
13
13
  deepOrange600,
14
14
  yellow200,
15
15
  lime300,
@@ -21,6 +21,39 @@ const blueGrey50 = "#ECEFF1";
21
21
  const blueGrey300 = "#90A4AE";
22
22
  const blueGrey700 = "#455A64";
23
23
  const grey900 = "#212121";
24
+
25
+ const colors: VictoryThemePalette = {
26
+ blue: "#4F7DA1",
27
+ pink: "#E2A37F",
28
+ teal: teal700,
29
+ purple: "#DF948A",
30
+ green: lightGreen500,
31
+ orange: deepOrange600,
32
+ cyan: cyan900,
33
+ red: "#DF5A49",
34
+ yellow: yellow200,
35
+ };
36
+
37
+ const grayscale = [blueGrey50, blueGrey300, blueGrey700, grey900];
38
+ const qualitative = [
39
+ "#334D5C",
40
+ "#45B29D",
41
+ "#EFC94C",
42
+ "#E27A3F",
43
+ "#DF5A49",
44
+ "#4F7DA1",
45
+ "#55DBC1",
46
+ "#EFDA97",
47
+ "#E2A37F",
48
+ "#DF948A",
49
+ ];
50
+ const heatmap = ["#428517", "#77D200", "#D6D305", "#EC8E19", "#C92B05"];
51
+ const warm = ["#940031", "#C43343", "#DC5429", "#FF821D", "#FFAF55"];
52
+ const cool = ["#2746B9", "#0B69D4", "#2794DB", "#31BB76", "#60E83B"];
53
+ const red = ["#FCAE91", "#FB6A4A", "#DE2D26", "#A50F15", "#750B0E"];
54
+ const green = ["#354722", "#466631", "#649146", "#8AB25C", "#A9C97E"];
55
+ const blue = ["#002C61", "#004B8F", "#006BC9", "#3795E5", "#65B4F4"];
56
+
24
57
  // *
25
58
  // * Typography
26
59
  // *
@@ -61,6 +94,17 @@ const strokeLinecap = "round";
61
94
  const strokeLinejoin = "round";
62
95
 
63
96
  export const material: VictoryThemeDefinition = {
97
+ palette: {
98
+ colors,
99
+ grayscale,
100
+ qualitative,
101
+ heatmap,
102
+ warm,
103
+ cool,
104
+ red,
105
+ green,
106
+ blue,
107
+ },
64
108
  area: Object.assign(
65
109
  {
66
110
  style: {
@@ -182,7 +226,7 @@ export const material: VictoryThemeDefinition = {
182
226
  ),
183
227
  group: Object.assign(
184
228
  {
185
- colorScale: colors,
229
+ colorScale,
186
230
  },
187
231
  baseProps,
188
232
  ),
@@ -200,7 +244,7 @@ export const material: VictoryThemeDefinition = {
200
244
  baseProps,
201
245
  ),
202
246
  legend: {
203
- colorScale: colors,
247
+ colorScale,
204
248
  gutter: 10,
205
249
  orientation: "vertical",
206
250
  titleOrientation: "top",
@@ -228,7 +272,7 @@ export const material: VictoryThemeDefinition = {
228
272
  ),
229
273
  pie: Object.assign(
230
274
  {
231
- colorScale: colors,
275
+ colorScale,
232
276
  style: {
233
277
  data: {
234
278
  padding,
@@ -256,7 +300,7 @@ export const material: VictoryThemeDefinition = {
256
300
  ),
257
301
  stack: Object.assign(
258
302
  {
259
- colorScale: colors,
303
+ colorScale,
260
304
  },
261
305
  baseProps,
262
306
  ),
@@ -72,7 +72,33 @@ export interface VictoryAxisCommonProps {
72
72
  export type VerticalAnchorType = "start" | "middle" | "end";
73
73
  // #region Victory Theme
74
74
  // Note: Many SVG attributes are missed in CSSProperties interface
75
+ type VictoryThemePaletteKeys =
76
+ | "blue"
77
+ | "pink"
78
+ | "teal"
79
+ | "purple"
80
+ | "green"
81
+ | "orange"
82
+ | "cyan"
83
+ | "red"
84
+ | "yellow";
85
+
86
+ export type VictoryThemePalette = {
87
+ [key in VictoryThemePaletteKeys]?: string;
88
+ };
89
+
75
90
  export interface VictoryThemeDefinition {
91
+ palette?: {
92
+ colors?: VictoryThemePalette;
93
+ grayscale?: string[];
94
+ qualitative?: string[];
95
+ heatmap?: string[];
96
+ warm?: string[];
97
+ cool?: string[];
98
+ red?: string[];
99
+ blue?: string[];
100
+ green?: string[];
101
+ };
76
102
  area?: {
77
103
  style?: {
78
104
  data?: VictoryStyleObject;
@@ -1,3 +1,8 @@
1
+ import {
2
+ VictoryTheme,
3
+ VictoryThemeDefinition,
4
+ } from "../victory-theme/victory-theme";
5
+
1
6
  /**
2
7
  * Given an object with CSS/SVG transform definitions, return the string value
3
8
  * for use with the `transform` CSS property or SVG attribute. Note that we
@@ -33,29 +38,39 @@ export const toTransformString = function (obj, ...more) {
33
38
  * of 5 hex string values in that color scale. If no 'name' parameter
34
39
  * is given, it will return the Victory default grayscale.
35
40
  * @param {String} name The name of the color scale to return (optional).
41
+ * @param {Object} theme The theme object to retrieve the color scale from (optional).
36
42
  * @returns {Array} An array of 5 hex string values composing a color scale.
37
43
  */
38
- export function getColorScale(name) {
39
- const scales = {
40
- grayscale: ["#cccccc", "#969696", "#636363", "#252525"],
41
- qualitative: [
42
- "#334D5C",
43
- "#45B29D",
44
- "#EFC94C",
45
- "#E27A3F",
46
- "#DF5A49",
47
- "#4F7DA1",
48
- "#55DBC1",
49
- "#EFDA97",
50
- "#E2A37F",
51
- "#DF948A",
52
- ],
53
- heatmap: ["#428517", "#77D200", "#D6D305", "#EC8E19", "#C92B05"],
54
- warm: ["#940031", "#C43343", "#DC5429", "#FF821D", "#FFAF55"],
55
- cool: ["#2746B9", "#0B69D4", "#2794DB", "#31BB76", "#60E83B"],
56
- red: ["#FCAE91", "#FB6A4A", "#DE2D26", "#A50F15", "#750B0E"],
57
- blue: ["#002C61", "#004B8F", "#006BC9", "#3795E5", "#65B4F4"],
58
- green: ["#354722", "#466631", "#649146", "#8AB25C", "#A9C97E"],
44
+ export function getColorScale(
45
+ name?: string,
46
+ theme: VictoryThemeDefinition = VictoryTheme.material,
47
+ ) {
48
+ const {
49
+ palette: {
50
+ grayscale = ["#cccccc", "#969696", "#636363", "#252525"],
51
+ qualitative = [],
52
+ heatmap = [],
53
+ warm = [],
54
+ cool = [],
55
+ red = [],
56
+ blue = [],
57
+ green = [],
58
+ } = {},
59
+ } = theme;
60
+
61
+ const scales: Record<string, string[]> = {
62
+ grayscale,
63
+ qualitative,
64
+ heatmap,
65
+ warm,
66
+ cool,
67
+ red,
68
+ blue,
69
+ green,
59
70
  };
60
- return name ? scales[name] : scales.grayscale;
71
+
72
+ const selectedScale =
73
+ name && scales[name]?.length ? scales[name] : scales.grayscale;
74
+
75
+ return selectedScale;
61
76
  }
@@ -298,7 +298,8 @@ export function getAllEvents(props) {
298
298
  return events || [];
299
299
  }
300
300
 
301
- export function getColor(calculatedProps, child, index) {
301
+ // eslint-disable-next-line max-params
302
+ export function getColor(calculatedProps, child, index, theme) {
302
303
  // check for styles first
303
304
  const { style } = calculatedProps;
304
305
  let { colorScale, color } = calculatedProps;
@@ -313,7 +314,7 @@ export function getColor(calculatedProps, child, index) {
313
314
  }
314
315
  const colors = Array.isArray(colorScale)
315
316
  ? colorScale
316
- : Style.getColorScale(colorScale);
317
+ : Style.getColorScale(colorScale, theme);
317
318
  return color || colors[index % colors.length];
318
319
  }
319
320
 
@@ -323,7 +324,8 @@ export function getStyle(theme, style, role) {
323
324
  return Helpers.getStyles(style, defaultStyle);
324
325
  }
325
326
 
326
- export function getChildStyle(child, index, calculatedProps) {
327
+ // eslint-disable-next-line max-params
328
+ export function getChildStyle(child, index, calculatedProps, theme) {
327
329
  const { style, role } = calculatedProps;
328
330
  const childStyle = child.props.style || {};
329
331
  if (Array.isArray(childStyle)) {
@@ -331,7 +333,9 @@ export function getChildStyle(child, index, calculatedProps) {
331
333
  }
332
334
  const childRole = child.type && child.type.role;
333
335
  const defaultFill =
334
- childRole === "stack" ? undefined : getColor(calculatedProps, child, index);
336
+ childRole === "stack"
337
+ ? undefined
338
+ : getColor(calculatedProps, child, index, theme);
335
339
  const defaultColor =
336
340
  childRole === "line"
337
341
  ? { fill: "none", stroke: defaultFill }
@@ -352,19 +356,13 @@ export function getChildStyle(child, index, calculatedProps) {
352
356
  };
353
357
  }
354
358
 
355
- export function getStringsFromCategories(childComponents, axis) {
359
+ export function getStringsFromChildrenCategories(childComponents, axis) {
356
360
  const iteratee = (child) => {
357
- const childProps = child.props || {};
358
- if (!Domain.isDomainComponent(child) || !childProps.categories) {
361
+ if (!Domain.isDomainComponent(child)) {
359
362
  return null;
360
363
  }
361
- const categories =
362
- childProps.categories && !Array.isArray(childProps.categories)
363
- ? childProps.categories[axis]
364
- : childProps.props.categories;
365
- const categoryStrings =
366
- categories && categories.filter((val) => typeof val === "string");
367
- return categoryStrings ? Collection.removeUndefined(categoryStrings) : [];
364
+ const childProps = child.props || {};
365
+ return Data.getStringsFromCategories(childProps, axis);
368
366
  };
369
367
  return Helpers.reduceChildren(childComponents.slice(0), iteratee);
370
368
  }
@@ -413,15 +411,14 @@ export function getCategoryAndAxisStringsFromChildren(
413
411
  axis,
414
412
  childComponents,
415
413
  ) {
416
- const categories = isPlainObject(props.categories)
417
- ? props.categories[axis]
418
- : props.categories;
414
+ const categories = Data.getStringsFromCategories(props, axis);
419
415
  const axisComponent = Axis.getAxisComponent(childComponents, axis);
420
416
  const axisStrings = axisComponent
421
417
  ? Data.getStringsFromAxes(axisComponent.props, axis)
422
418
  : [];
423
- const categoryStrings =
424
- categories || getStringsFromCategories(childComponents, axis);
419
+ const categoryStrings = categories.length
420
+ ? categories
421
+ : getStringsFromChildrenCategories(childComponents, axis);
425
422
  return uniq([...categoryStrings, ...axisStrings].flat());
426
423
  }
427
424
 
@@ -441,15 +438,9 @@ export function getStringsFromChildren(props, childComponents) {
441
438
 
442
439
  export function getCategories(props, childComponents, allStrings?) {
443
440
  const xPropCategories =
444
- props.categories && !Array.isArray(props.categories)
445
- ? props.categories.x
446
- : props.categories;
447
-
441
+ props.categories && Data.getStringsFromCategories(props, "x");
448
442
  const yPropCategories =
449
- props.categories && !Array.isArray(props.categories)
450
- ? props.categories.y
451
- : props.categories;
452
-
443
+ props.categories && Data.getStringsFromCategories(props, "y");
453
444
  const fallbackRequired = !xPropCategories || !yPropCategories;
454
445
 
455
446
  const fallbackProps = fallbackRequired