Skip to content

Commit

Permalink
Extract bar helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Becca Bailey committed Oct 19, 2021
1 parent 34d84ef commit f541def
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 216 deletions.
73 changes: 73 additions & 0 deletions packages/victory-bar/src/bar-helper-methods.js
@@ -0,0 +1,73 @@
import { assign, isNil, isPlainObject } from "lodash";
import { Helpers } from "victory-core";

export const getBarWidth = (barWidth, props) => {
const { scale, data, defaultBarWidth, style } = props;
if (barWidth) {
return Helpers.evaluateProp(barWidth, props);
} else if (style.width) {
return style.width;
}
const range = scale.x.range();
const extent = Math.abs(range[1] - range[0]);
const bars = data.length + 2;
const barRatio = props.barRatio || 0.5;
const defaultWidth =
barRatio * (data.length < 2 ? defaultBarWidth : extent / bars);
return Math.max(1, defaultWidth);
};

const getCornerRadiusFromObject = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
const updateCornerRadius = (corner, fallback) => {
if (!isNil(cornerRadius[corner])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[corner],
props
);
} else if (!isNil(cornerRadius[fallback])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[fallback],
props
);
}
};
updateCornerRadius("topLeft", "top");
updateCornerRadius("topRight", "top");
updateCornerRadius("bottomLeft", "bottom");
updateCornerRadius("bottomRight", "bottom");
return realCornerRadius;
};

export const getCornerRadius = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
if (!cornerRadius) {
return realCornerRadius;
}
if (isPlainObject(cornerRadius)) {
return getCornerRadiusFromObject(cornerRadius, props);
} else {
realCornerRadius.topLeft = Helpers.evaluateProp(cornerRadius, props);
realCornerRadius.topRight = Helpers.evaluateProp(cornerRadius, props);
return realCornerRadius;
}
};

export const getStyle = (style = {}, props) => {
if (props.disableInlineStyles) {
return {};
}
const stroke = style.fill || "black";
const baseStyle = { fill: "black", stroke };
return Helpers.evaluateStyle(assign(baseStyle, style), props);
};
101 changes: 5 additions & 96 deletions packages/victory-bar/src/bar.js
@@ -1,100 +1,9 @@
import React from "react";
import { assign } from "lodash";
import PropTypes from "prop-types";
import { Helpers, CommonProps, Path } from "victory-core";
import { assign, isPlainObject, isNil } from "lodash";

import {
getVerticalBarPath,
getHorizontalBarPath,
getVerticalPolarBarPath,
getCustomBarPath
} from "./path-helper-methods";

const getBarPath = (props, width, cornerRadius) => {
if (props.getPath) {
return getCustomBarPath(props, width);
}

return props.horizontal
? getHorizontalBarPath(props, width, cornerRadius)
: getVerticalBarPath(props, width, cornerRadius);
};

const getPolarBarPath = (props, cornerRadius) => {
// TODO Radial bars
return getVerticalPolarBarPath(props, cornerRadius);
};

const getBarWidth = (barWidth, props) => {
const { scale, data, defaultBarWidth, style } = props;
if (barWidth) {
return Helpers.evaluateProp(barWidth, props);
} else if (style.width) {
return style.width;
}
const range = scale.x.range();
const extent = Math.abs(range[1] - range[0]);
const bars = data.length + 2;
const barRatio = props.barRatio || 0.5;
const defaultWidth =
barRatio * (data.length < 2 ? defaultBarWidth : extent / bars);
return Math.max(1, defaultWidth);
};

const getCornerRadiusFromObject = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
const updateCornerRadius = (corner, fallback) => {
if (!isNil(cornerRadius[corner])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[corner],
props
);
} else if (!isNil(cornerRadius[fallback])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[fallback],
props
);
}
};
updateCornerRadius("topLeft", "top");
updateCornerRadius("topRight", "top");
updateCornerRadius("bottomLeft", "bottom");
updateCornerRadius("bottomRight", "bottom");
return realCornerRadius;
};

const getCornerRadius = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
if (!cornerRadius) {
return realCornerRadius;
}
if (isPlainObject(cornerRadius)) {
return getCornerRadiusFromObject(cornerRadius, props);
} else {
realCornerRadius.topLeft = Helpers.evaluateProp(cornerRadius, props);
realCornerRadius.topRight = Helpers.evaluateProp(cornerRadius, props);
return realCornerRadius;
}
};

const getStyle = (style = {}, props) => {
if (props.disableInlineStyles) {
return {};
}
const stroke = style.fill || "black";
const baseStyle = { fill: "black", stroke };
return Helpers.evaluateStyle(assign(baseStyle, style), props);
};
import React from "react";
import { CommonProps, Helpers, Path } from "victory-core";
import { getStyle, getBarWidth, getCornerRadius } from "./bar-helper-methods";
import { getPolarBarPath, getBarPath } from "./path-helper-methods";

const evaluateProps = (props) => {
/**
Expand Down
8 changes: 4 additions & 4 deletions packages/victory-bar/src/helper-methods.js
@@ -1,11 +1,11 @@
import { assign, isNil } from "lodash";
import {
Helpers,
LabelHelpers,
Collection,
Data,
Domain,
Scale,
Collection
Helpers,
LabelHelpers,
Scale
} from "victory-core";

export const getBarPosition = (props, datum) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/victory-bar/src/index.js
Expand Up @@ -5,5 +5,8 @@ export {
getVerticalBarPath,
getHorizontalBarPath,
getVerticalPolarBarPath,
getCustomBarPath
getCustomBarPath,
getPolarBarPath,
getBarPath
} from "./path-helper-methods";
export { getBarWidth, getStyle, getCornerRadius } from "./bar-helper-methods";
15 changes: 15 additions & 0 deletions packages/victory-bar/src/path-helper-methods.js
Expand Up @@ -407,3 +407,18 @@ export const getVerticalPolarBarPath = (props, cornerRadius) => {
}, "");
return `${path} z`;
};

export const getBarPath = (props, width, cornerRadius) => {
if (props.getPath) {
return getCustomBarPath(props, width);
}

return props.horizontal
? getHorizontalBarPath(props, width, cornerRadius)
: getVerticalBarPath(props, width, cornerRadius);
};

export const getPolarBarPath = (props, cornerRadius) => {
// TODO Radial bars
return getVerticalPolarBarPath(props, cornerRadius);
};
114 changes: 7 additions & 107 deletions packages/victory-canvas/src/canvas-bar.js
@@ -1,113 +1,22 @@
/*global Path2D:false */
import { assign, isNil, isPlainObject, omit } from "lodash";
import { assign, omit } from "lodash";
import React from "react";
import {
Bar,
getCustomBarPath,
getHorizontalBarPath,
getVerticalBarPath,
getVerticalPolarBarPath
getBarPath,
getBarWidth,
getCornerRadius,
getPolarBarPath,
getStyle
} from "victory-bar";
import { Helpers } from "victory-core";
import { useCanvasContext } from "./hooks/use-canvas-context";

const getBarPath = (props, width, cornerRadius) => {
if (props.getPath) {
return getCustomBarPath(props, width);
}

return props.horizontal
? getHorizontalBarPath(props, width, cornerRadius)
: getVerticalBarPath(props, width, cornerRadius);
};

const getPolarBarPath = (props, cornerRadius) => {
return getVerticalPolarBarPath(props, cornerRadius);
};

const getBarWidth = (barWidth, props) => {
const { scale, data, defaultBarWidth, style } = props;
if (barWidth) {
return Helpers.evaluateProp(barWidth, props);
} else if (style.width) {
return style.width;
}
const range = scale.x.range();
const extent = Math.abs(range[1] - range[0]);
const bars = data.length + 2;
const barRatio = props.barRatio || 0.5;
const defaultWidth =
barRatio * (data.length < 2 ? defaultBarWidth : extent / bars);
return Math.max(1, defaultWidth);
};

const getCornerRadiusFromObject = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
const updateCornerRadius = (corner, fallback) => {
if (!isNil(cornerRadius[corner])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[corner],
props
);
} else if (!isNil(cornerRadius[fallback])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[fallback],
props
);
}
};
updateCornerRadius("topLeft", "top");
updateCornerRadius("topRight", "top");
updateCornerRadius("bottomLeft", "bottom");
updateCornerRadius("bottomRight", "bottom");
return realCornerRadius;
};

const getCornerRadius = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
if (!cornerRadius) {
return realCornerRadius;
}
if (isPlainObject(cornerRadius)) {
return getCornerRadiusFromObject(cornerRadius, props);
} else {
realCornerRadius.topLeft = Helpers.evaluateProp(cornerRadius, props);
realCornerRadius.topRight = Helpers.evaluateProp(cornerRadius, props);
return realCornerRadius;
}
};

const getStyle = (style = {}, props) => {
if (props.disableInlineStyles) {
return {};
}
const stroke = style.fill || "black";
const baseStyle = { fill: "black", stroke };
return Helpers.evaluateStyle(assign(baseStyle, style), props);
};

const evaluateProps = (props) => {
/**
* Potential evaluated props of following must be evaluated in this order:
* 1) `style`
* 2) `barWidth`
* 3) `cornerRadius`
*
* Everything else does not have to be evaluated in a particular order:
* `ariaLabel`
* `desc`
* `id`
* `tabIndex`
*/
const style = getStyle(props.style, props);
const barWidth = getBarWidth(props.barWidth, assign({}, props, { style }));
Expand All @@ -116,19 +25,10 @@ const evaluateProps = (props) => {
assign({}, props, { style, barWidth })
);

const ariaLabel = Helpers.evaluateProp(props.ariaLabel, props);
const desc = Helpers.evaluateProp(props.desc, props);
const id = Helpers.evaluateProp(props.id, props);
const tabIndex = Helpers.evaluateProp(props.tabIndex, props);

return assign({}, props, {
ariaLabel,
style,
barWidth,
cornerRadius,
desc,
id,
tabIndex
cornerRadius
});
};

Expand Down
8 changes: 0 additions & 8 deletions packages/victory-canvas/src/canvas-point.js
Expand Up @@ -30,18 +30,15 @@ const getPath = (props) => {
const evaluateProps = (props) => {
/**
* Potential evaluated props are:
* `id`
* `size`
* `style`
* `symbol`
*/
const id = Helpers.evaluateProp(props.id, props);
const size = Helpers.evaluateProp(props.size, props);
const style = Helpers.evaluateStyle(props.style, props);
const symbol = Helpers.evaluateProp(props.symbol, props);

return assign({}, props, {
id,
size,
style,
symbol
Expand Down Expand Up @@ -80,9 +77,4 @@ CanvasPoint.propTypes = {
y: PropTypes.number
};

CanvasPoint.defaultProps = {
role: "presentation",
shapeRendering: "auto"
};

export default CanvasPoint;

0 comments on commit f541def

Please sign in to comment.