From 10ca857a0fd9c33bed47b5c44f6a592cf53e3883 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Thu, 6 May 2021 10:08:14 -0400 Subject: [PATCH 01/14] don't mutate domain on zoom or pan event --- packages/victory-zoom-container/src/zoom-helpers.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/victory-zoom-container/src/zoom-helpers.js b/packages/victory-zoom-container/src/zoom-helpers.js index d74fe57c6..b7d49a69d 100644 --- a/packages/victory-zoom-container/src/zoom-helpers.js +++ b/packages/victory-zoom-container/src/zoom-helpers.js @@ -265,7 +265,6 @@ const RawZoomHelpers = { startX: x, startY: y, parentSVG, - domain: currentDomain, currentDomain, originalDomain, cachedZoomDomain: zoomDomain, @@ -307,7 +306,6 @@ const RawZoomHelpers = { (targetProps.zoomActive && !this.checkDomainEquality(originalDomain, lastDomain)); const mutatedProps = { - domain: currentDomain, currentDomain, originalDomain, cachedZoomDomain: zoomDomain, From ee239a7fdf3b0bc72c94713b9b9f9ed863f90855 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Mon, 10 May 2021 10:56:47 -0400 Subject: [PATCH 02/14] first pass: calculate axis offset x and y inside axis rather than chart --- packages/victory-axis/src/helper-methods.js | 119 ++++++++++++++----- packages/victory-chart/src/helper-methods.js | 30 ++--- 2 files changed, 101 insertions(+), 48 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 9c97a4f6a..26b2fa08d 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -169,37 +169,90 @@ const getLabelPadding = (props, style) => { /*eslint-enable no-magic-numbers*/ }; +// eslint-disable-next-line complexity const getOffset = (props, calculatedValues) => { const { - style, + // style, padding, - isVertical, + // isVertical, orientation, - labelPadding, - stringTicks, - ticks, + // labelPadding, + // stringTicks, + // ticks, scale, - axis + // axis, + domain } = calculatedValues; const { polar, horizontal } = props; - const sharedProps = { scale: { [axis]: scale }, polar, horizontal, ticks, stringTicks }; - const xPadding = orientation === "right" ? padding.right : padding.left; - const yPadding = orientation === "top" ? padding.top : padding.bottom; - const fontSize = style.axisLabel.fontSize || 14; // eslint-disable-line no-magic-numbers - const offsetX = props.offsetX !== null && props.offsetX !== undefined ? props.offsetX : xPadding; - const offsetY = props.offsetY !== null && props.offsetY !== undefined ? props.offsetY : yPadding; - const tickSizes = ticks.map((data, index) => { - const tick = stringTicks ? props.tickValues[data - 1] : data; - const tickStyle = Helpers.evaluateStyle(style.ticks, assign({}, sharedProps, { tick, index })); - return tickStyle.size || 0; - }); - const totalPadding = fontSize + 2 * Math.max(...tickSizes) + labelPadding; - const minimumPadding = 1.2 * fontSize; // eslint-disable-line no-magic-numbers - const x = isVertical ? totalPadding : minimumPadding; - const y = isVertical ? minimumPadding : totalPadding; + // const sharedProps = { scale: { [axis]: scale }, polar, horizontal, ticks, stringTicks }; + // const xPadding = orientation === "right" ? padding.right : padding.left; + // const yPadding = orientation === "top" ? padding.top : padding.bottom; + // const fontSize = style.axisLabel.fontSize || 14; // eslint-disable-line no-magic-numbers + // const offsetX = props.offsetX !== null && props.offsetX !== undefined ? props.offsetX : xPadding; + // const offsetY = props.offsetY !== null && props.offsetY !== undefined ? props.offsetY : yPadding; + // const tickSizes = ticks.map((data, index) => { + // const tick = stringTicks ? props.tickValues[data - 1] : data; + // const tickStyle = Helpers.evaluateStyle(style.ticks, assign({}, sharedProps, { tick, index })); + // return tickStyle.size || 0; + // }); + // const totalPadding = fontSize + 2 * Math.max(...tickSizes) + labelPadding; + // const minimumPadding = 1.2 * fontSize; // eslint-disable-line no-magic-numbers + // const x = isVertical ? totalPadding : minimumPadding; + // const y = isVertical ? minimumPadding : totalPadding; + + const { top, bottom, left, right } = padding; + + // eslint-disable-next-line func-style + function getAltOrientation(ax, originSign, ho) { + const sign = originSign || "positive"; + const orientations = { + positive: { x: "bottom", y: "left" }, + negative: { x: "top", y: "right" } + }; + const horizontalOrientations = { + positive: { x: "left", y: "bottom" }, + negative: { x: "right", y: "top" } + }; + return ho ? horizontalOrientations[sign][ax] : orientations[sign][ax]; + } + + const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); + const originSign = { + x: Axis.getOriginSign(origin.x, domain.x), + y: Axis.getOriginSign(origin.y, domain.y) + }; + const altOrientations = { + x: getAltOrientation("x", originSign.y, horizontal), + y: getAltOrientation("y", originSign.x, horizontal) + }; + const orientations = { + x: + orientation === "bottom" || orientation === "top" + ? orientation + : altOrientations.x, + y: + orientation === "left" || orientation === "right" + ? orientation + : altOrientations.y + }; + const orientationOffset = { + y: orientations.x === "bottom" ? bottom : top, + x: orientations.y === "left" ? left : right + }; + const originOffset = { + x: orientations.y === "left" ? 0 : props.width, + y: orientations.x === "bottom" ? props.height : 0 + }; + const originPosition = { + x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), + y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) + }; + return { - x: offsetX !== null && offsetX !== undefined ? offsetX : x, - y: offsetY !== null && offsetY !== undefined ? offsetY : y + // x: offsetX !== null && offsetX !== undefined ? offsetX : x, + // y: offsetY !== null && offsetY !== undefined ? offsetY : y + x: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, + y: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y }; }; @@ -296,10 +349,10 @@ const getCalculatedValues = (props) => { const stringTicks = Axis.stringTicks(props) ? props.tickValues : undefined; const axis = Axis.getAxis(props); const orientation = getOrientation(props); - const scale = getScale(props); - const domain = Axis.getDomain(props); - const ticks = Axis.getTicks(props, scale, props.crossAxis); - const tickFormat = Axis.getTickFormat(props, scale); + // const scale = getScale(props); + // const domain = Axis.getDomain(props); + const ticks = Axis.getTicks(props, getScale(props), props.crossAxis); + const tickFormat = Axis.getTickFormat(props, getScale(props)); const anchors = getAnchors(orientation, isVertical); return { @@ -311,10 +364,10 @@ const getCalculatedValues = (props) => { labelPadding, stringTicks, anchors, - scale, + scale: props.scale, ticks, tickFormat, - domain + domain: props.domain }; }; @@ -337,7 +390,7 @@ const getBaseProps = (props, fallbackProps) => { const otherAxis = axis === "x" ? "y" : "x"; const { width, height, standalone, theme, polar, padding, horizontal } = props; const { globalTransform, gridOffset, gridEdge } = getLayoutProps(props, calculatedValues); - const sharedProps = { scale: { [axis]: scale }, polar, horizontal, ticks, stringTicks }; + const sharedProps = { scale: { [axis]: scale[axis] }, polar, horizontal, ticks, stringTicks }; const axisProps = getAxisProps(props, calculatedValues, globalTransform); const axisLabelProps = getAxisLabelProps(props, calculatedValues, globalTransform); const initialChildProps = { @@ -361,14 +414,14 @@ const getBaseProps = (props, fallbackProps) => { ); const tickLayout = { position: getTickPosition(styles, orientation, isVertical), - transform: getTickTransform(scale(tickValue), globalTransform, isVertical) + transform: getTickTransform(scale[axis](tickValue), globalTransform, isVertical) }; const gridLayout = { edge: gridEdge, transform: { - x: isVertical ? -gridOffset.x + globalTransform.x : scale(tickValue) + globalTransform.x, - y: isVertical ? scale(tickValue) + globalTransform.y : gridOffset.y + globalTransform.y + x: isVertical ? -gridOffset.x + globalTransform.x : scale[axis](tickValue) + globalTransform.x, + y: isVertical ? scale[axis](tickValue) + globalTransform.y : gridOffset.y + globalTransform.y } }; childProps[index] = { diff --git a/packages/victory-chart/src/helper-methods.js b/packages/victory-chart/src/helper-methods.js index 9859dc31f..9b15a588f 100644 --- a/packages/victory-chart/src/helper-methods.js +++ b/packages/victory-chart/src/helper-methods.js @@ -29,8 +29,8 @@ function getAxisProps(child, props, calculatedProps) { innerRadius: props.innerRadius, domain, scale, - offsetY: childProps.offsetY !== undefined ? childProps.offsetY : axisOffset.y, - offsetX: childProps.offsetX !== undefined ? childProps.offsetX : axisOffset.x, + // offsetY: childProps.offsetY !== undefined ? childProps.offsetY : axisOffset.y, + // offsetX: childProps.offsetX !== undefined ? childProps.offsetX : axisOffset.x, crossAxis, orientation }; @@ -93,19 +93,6 @@ function getStyles(props) { }; } -function getOrientation(axis, originSign, horizontal) { - const sign = originSign || "positive"; - const orientations = { - positive: { x: "bottom", y: "left" }, - negative: { x: "top", y: "right" } - }; - const horizontalOrientations = { - positive: { x: "left", y: "bottom" }, - negative: { x: "right", y: "top" } - }; - return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; -} - function getCalculatedProps(props, childComponents) { const style = getStyles(props); props = Helpers.modifyProps(props, fallbackProps, "chart"); @@ -292,6 +279,19 @@ const getHorizontalAxisOffset = (props, calculatedProps, orientation) => { }; }; +function getOrientation(axis, originSign, horizontal) { + const sign = originSign || "positive"; + const orientations = { + positive: { x: "bottom", y: "left" }, + negative: { x: "top", y: "right" } + }; + const horizontalOrientations = { + positive: { x: "left", y: "bottom" }, + negative: { x: "right", y: "top" } + }; + return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; +} + const createStringMap = (props, childComponents, allStrings) => { const x = !allStrings.x || allStrings.x.length === 0 From 1cdb1be2852f13486f5a29ce0cba7c7f38e95a40 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Mon, 10 May 2021 10:58:08 -0400 Subject: [PATCH 03/14] linter cleanup --- packages/victory-axis/src/helper-methods.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 26b2fa08d..6f64d127f 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -226,14 +226,8 @@ const getOffset = (props, calculatedValues) => { y: getAltOrientation("y", originSign.x, horizontal) }; const orientations = { - x: - orientation === "bottom" || orientation === "top" - ? orientation - : altOrientations.x, - y: - orientation === "left" || orientation === "right" - ? orientation - : altOrientations.y + x: orientation === "bottom" || orientation === "top" ? orientation : altOrientations.x, + y: orientation === "left" || orientation === "right" ? orientation : altOrientations.y }; const orientationOffset = { y: orientations.x === "bottom" ? bottom : top, @@ -420,8 +414,12 @@ const getBaseProps = (props, fallbackProps) => { const gridLayout = { edge: gridEdge, transform: { - x: isVertical ? -gridOffset.x + globalTransform.x : scale[axis](tickValue) + globalTransform.x, - y: isVertical ? scale[axis](tickValue) + globalTransform.y : gridOffset.y + globalTransform.y + x: isVertical + ? -gridOffset.x + globalTransform.x + : scale[axis](tickValue) + globalTransform.x, + y: isVertical + ? scale[axis](tickValue) + globalTransform.y + : gridOffset.y + globalTransform.y } }; childProps[index] = { From 51c552f6826278fff5001f9c7023c501bc283fa3 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Mon, 10 May 2021 15:53:48 -0400 Subject: [PATCH 04/14] calculate horizontal offset in axis --- packages/victory-axis/src/helper-methods.js | 86 +++++++++--- packages/victory-chart/src/helper-methods.js | 139 +++++++++---------- 2 files changed, 133 insertions(+), 92 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 6f64d127f..09df7cc24 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -156,6 +156,20 @@ const getAnchors = (orientation, isVertical) => { }; }; +// eslint-disable-next-line func-style +function getAltOrientation(axis, originSign, horizontal) { + const sign = originSign || "positive"; + const orientations = { + positive: { x: "bottom", y: "left" }, + negative: { x: "top", y: "right" } + }; + const horizontalOrientations = { + positive: { x: "left", y: "bottom" }, + negative: { x: "right", y: "top" } + }; + return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; +} + const getLabelPadding = (props, style) => { const labelStyle = style.axisLabel || {}; if (labelStyle.padding !== undefined && labelStyle.padding !== null) { @@ -202,20 +216,6 @@ const getOffset = (props, calculatedValues) => { const { top, bottom, left, right } = padding; - // eslint-disable-next-line func-style - function getAltOrientation(ax, originSign, ho) { - const sign = originSign || "positive"; - const orientations = { - positive: { x: "bottom", y: "left" }, - negative: { x: "top", y: "right" } - }; - const horizontalOrientations = { - positive: { x: "left", y: "bottom" }, - negative: { x: "right", y: "top" } - }; - return ho ? horizontalOrientations[sign][ax] : orientations[sign][ax]; - } - const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); const originSign = { x: Axis.getOriginSign(origin.x, domain.x), @@ -229,29 +229,75 @@ const getOffset = (props, calculatedValues) => { x: orientation === "bottom" || orientation === "top" ? orientation : altOrientations.x, y: orientation === "left" || orientation === "right" ? orientation : altOrientations.y }; + + // padding const orientationOffset = { y: orientations.x === "bottom" ? bottom : top, x: orientations.y === "left" ? left : right }; + // if left & bottom - this is 0 for x and 300 (height) for y const originOffset = { x: orientations.y === "left" ? 0 : props.width, y: orientations.x === "bottom" ? props.height : 0 }; + // distance from top left corner + const originPosition = { + x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), + y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) + }; + + const x = originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x; + const y = originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y; + const offsetX = props.offsetX !== null && props.offsetX !== undefined ? x - props.offsetX : x; + const offsetY = props.offsetY !== null && props.offsetY !== undefined ? y - props.offsetY : y; + + return { + x: offsetX, + y: offsetY + }; +}; + +// eslint-disable-next-line complexity +const getHorizontalOffset = (props, calculatedValues) => { + const { scale, orientation, domain, padding } = calculatedValues; + const { polar, horizontal } = props; + const { top, bottom, left, right } = padding; + const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); + const originSign = { + x: Axis.getOriginSign(origin.x, domain.x), + y: Axis.getOriginSign(origin.y, domain.y) + }; + const altOrientations = { + x: getAltOrientation("x", originSign.y, horizontal), + y: getAltOrientation("y", originSign.x, horizontal) + }; + const orientations = { + y: orientation === "bottom" || orientation === "top" ? orientation : altOrientations.x, + x: orientation === "left" || orientation === "right" ? orientation : altOrientations.y + }; + + // make the axes line up, and cross when appropriate + const orientationOffset = { + x: orientations.y === "bottom" ? bottom : top, + y: orientations.x === "left" ? left : right + }; + const originOffset = { + y: orientations.x === "left" ? 0 : props.width, + x: orientations.y === "bottom" ? props.height : 0 + }; + const originPosition = { x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) }; return { - // x: offsetX !== null && offsetX !== undefined ? offsetX : x, - // y: offsetY !== null && offsetY !== undefined ? offsetY : y - x: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, - y: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y + y: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, + x: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y }; }; const getTransform = (props, calculatedValues, offset) => { - // const { orientation, axis } = calculatedValues; const axisValue = Axis.getAxisValue(props, axis); return { @@ -315,7 +361,7 @@ const getGridOffset = (props, calculatedValues, offset) => { }; const getLayoutProps = (modifiedProps, calculatedValues) => { - const offset = getOffset(modifiedProps, calculatedValues); + const offset = modifiedProps.horizontal ? getHorizontalOffset(modifiedProps, calculatedValues) : getOffset(modifiedProps, calculatedValues); return { globalTransform: getTransform(modifiedProps, calculatedValues, offset), gridOffset: getGridOffset(modifiedProps, calculatedValues, offset), diff --git a/packages/victory-chart/src/helper-methods.js b/packages/victory-chart/src/helper-methods.js index 9b15a588f..04a759f23 100644 --- a/packages/victory-chart/src/helper-methods.js +++ b/packages/victory-chart/src/helper-methods.js @@ -17,9 +17,6 @@ function getAxisProps(child, props, calculatedProps) { const axis = child.type.getAxis(childProps); const crossAxis = childProps.crossAxis === false ? false : true; const orientation = childProps.orientation || orientations[axis]; - const axisOffset = horizontal - ? getHorizontalAxisOffset(props, calculatedProps, orientation) - : getAxisOffset(props, calculatedProps, orientation); return { stringMap, horizontal, @@ -29,8 +26,6 @@ function getAxisProps(child, props, calculatedProps) { innerRadius: props.innerRadius, domain, scale, - // offsetY: childProps.offsetY !== undefined ? childProps.offsetY : axisOffset.y, - // offsetX: childProps.offsetX !== undefined ? childProps.offsetX : axisOffset.x, crossAxis, orientation }; @@ -211,73 +206,73 @@ const getDomain = (props, axis, childComponents) => { return invertDomain ? domain.concat().reverse() : domain; }; -const getAxisOffset = (props, calculatedProps, orientation) => { - const { scale, origin, domain, padding } = calculatedProps; - const { top, bottom, left, right } = padding; - const orientations = { - x: - orientation === "bottom" || orientation === "top" - ? orientation - : calculatedProps.orientations.x, - y: - orientation === "left" || orientation === "right" - ? orientation - : calculatedProps.orientations.y - }; - // make the axes line up, and cross when appropriate - const orientationOffset = { - y: orientations.x === "bottom" ? bottom : top, - x: orientations.y === "left" ? left : right - }; - const originOffset = { - x: orientations.y === "left" ? 0 : props.width, - y: orientations.x === "bottom" ? props.height : 0 - }; - - const originPosition = { - x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), - y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) - }; - - return { - x: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, - y: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y - }; -}; - -const getHorizontalAxisOffset = (props, calculatedProps, orientation) => { - const { scale, origin, domain, padding } = calculatedProps; - const { top, bottom, left, right } = padding; - const orientations = { - y: - orientation === "bottom" || orientation === "top" - ? orientation - : calculatedProps.orientations.x, - x: - orientation === "left" || orientation === "right" - ? orientation - : calculatedProps.orientations.y - }; - // make the axes line up, and cross when appropriate - const orientationOffset = { - x: orientations.y === "bottom" ? bottom : top, - y: orientations.x === "left" ? left : right - }; - const originOffset = { - y: orientations.x === "left" ? 0 : props.width, - x: orientations.y === "bottom" ? props.height : 0 - }; - - const originPosition = { - x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), - y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) - }; - - return { - y: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, - x: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y - }; -}; +// const getAxisOffset = (props, calculatedProps, orientation) => { +// const { scale, origin, domain, padding } = calculatedProps; +// const { top, bottom, left, right } = padding; +// const orientations = { +// x: +// orientation === "bottom" || orientation === "top" +// ? orientation +// : calculatedProps.orientations.x, +// y: +// orientation === "left" || orientation === "right" +// ? orientation +// : calculatedProps.orientations.y +// }; +// // make the axes line up, and cross when appropriate +// const orientationOffset = { +// y: orientations.x === "bottom" ? bottom : top, +// x: orientations.y === "left" ? left : right +// }; +// const originOffset = { +// x: orientations.y === "left" ? 0 : props.width, +// y: orientations.x === "bottom" ? props.height : 0 +// }; + +// const originPosition = { +// x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), +// y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) +// }; + +// return { +// x: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, +// y: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y +// }; +// }; + +// const getHorizontalAxisOffset = (props, calculatedProps, orientation) => { +// const { scale, origin, domain, padding } = calculatedProps; +// const { top, bottom, left, right } = padding; +// const orientations = { +// y: +// orientation === "bottom" || orientation === "top" +// ? orientation +// : calculatedProps.orientations.x, +// x: +// orientation === "left" || orientation === "right" +// ? orientation +// : calculatedProps.orientations.y +// }; +// // make the axes line up, and cross when appropriate +// const orientationOffset = { +// x: orientations.y === "bottom" ? bottom : top, +// y: orientations.x === "left" ? left : right +// }; +// const originOffset = { +// y: orientations.x === "left" ? 0 : props.width, +// x: orientations.y === "bottom" ? props.height : 0 +// }; + +// const originPosition = { +// x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), +// y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) +// }; + +// return { +// y: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, +// x: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y +// }; +// }; function getOrientation(axis, originSign, horizontal) { const sign = originSign || "positive"; From 44467b04679f4b54a9f16e1a2e2215387b7e973d Mon Sep 17 00:00:00 2001 From: jhumbug Date: Mon, 10 May 2021 16:29:32 -0400 Subject: [PATCH 05/14] get scale and domain from helpers --- packages/victory-axis/src/helper-methods.js | 25 ++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 09df7cc24..f809ca4b6 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -1,5 +1,5 @@ import { assign, defaults } from "lodash"; -import { Helpers, Scale, Axis } from "victory-core"; +import { Helpers, Scale, Axis, Domain } from "victory-core"; const orientationSign = { top: -1, @@ -389,8 +389,23 @@ const getCalculatedValues = (props) => { const stringTicks = Axis.stringTicks(props) ? props.tickValues : undefined; const axis = Axis.getAxis(props); const orientation = getOrientation(props); - // const scale = getScale(props); - // const domain = Axis.getDomain(props); + + const range = { + x: Helpers.getRange(props, "x"), + y: Helpers.getRange(props, "y") + }; + const domain = { + x: Domain.getDomain(props, "x"), + y: Domain.getDomain(props, "y") + }; + const scale = { + x: Scale.getBaseScale(props, "x") + .domain(domain.x) + .range(props.horizontal ? range.y : range.x), + y: Scale.getBaseScale(props, "y") + .domain(domain.y) + .range(props.horizontal ? range.x : range.y) + }; const ticks = Axis.getTicks(props, getScale(props), props.crossAxis); const tickFormat = Axis.getTickFormat(props, getScale(props)); const anchors = getAnchors(orientation, isVertical); @@ -404,10 +419,10 @@ const getCalculatedValues = (props) => { labelPadding, stringTicks, anchors, - scale: props.scale, + scale, ticks, tickFormat, - domain: props.domain + domain }; }; From 0290f536e234ab070f852ef3c4878ee6549f68c5 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Tue, 11 May 2021 10:07:44 -0400 Subject: [PATCH 06/14] cleanup --- packages/victory-axis/src/helper-methods.js | 108 ++++++++----------- packages/victory-chart/src/helper-methods.js | 68 ------------ 2 files changed, 46 insertions(+), 130 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index f809ca4b6..a050ba325 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -156,20 +156,6 @@ const getAnchors = (orientation, isVertical) => { }; }; -// eslint-disable-next-line func-style -function getAltOrientation(axis, originSign, horizontal) { - const sign = originSign || "positive"; - const orientations = { - positive: { x: "bottom", y: "left" }, - negative: { x: "top", y: "right" } - }; - const horizontalOrientations = { - positive: { x: "left", y: "bottom" }, - negative: { x: "right", y: "top" } - }; - return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; -} - const getLabelPadding = (props, style) => { const labelStyle = style.axisLabel || {}; if (labelStyle.padding !== undefined && labelStyle.padding !== null) { @@ -183,51 +169,38 @@ const getLabelPadding = (props, style) => { /*eslint-enable no-magic-numbers*/ }; +const getDefaultOrientations = (axis, originSign, horizontal) => { + const sign = originSign || "positive"; + const orientations = { + positive: { x: "bottom", y: "left" }, + negative: { x: "top", y: "right" } + }; + const horizontalOrientations = { + positive: { x: "left", y: "bottom" }, + negative: { x: "right", y: "top" } + }; + return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; +}; + // eslint-disable-next-line complexity const getOffset = (props, calculatedValues) => { - const { - // style, - padding, - // isVertical, - orientation, - // labelPadding, - // stringTicks, - // ticks, - scale, - // axis, - domain - } = calculatedValues; + const { scale, orientation, domain, padding } = calculatedValues; const { polar, horizontal } = props; - // const sharedProps = { scale: { [axis]: scale }, polar, horizontal, ticks, stringTicks }; - // const xPadding = orientation === "right" ? padding.right : padding.left; - // const yPadding = orientation === "top" ? padding.top : padding.bottom; - // const fontSize = style.axisLabel.fontSize || 14; // eslint-disable-line no-magic-numbers - // const offsetX = props.offsetX !== null && props.offsetX !== undefined ? props.offsetX : xPadding; - // const offsetY = props.offsetY !== null && props.offsetY !== undefined ? props.offsetY : yPadding; - // const tickSizes = ticks.map((data, index) => { - // const tick = stringTicks ? props.tickValues[data - 1] : data; - // const tickStyle = Helpers.evaluateStyle(style.ticks, assign({}, sharedProps, { tick, index })); - // return tickStyle.size || 0; - // }); - // const totalPadding = fontSize + 2 * Math.max(...tickSizes) + labelPadding; - // const minimumPadding = 1.2 * fontSize; // eslint-disable-line no-magic-numbers - // const x = isVertical ? totalPadding : minimumPadding; - // const y = isVertical ? minimumPadding : totalPadding; - const { top, bottom, left, right } = padding; - const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); const originSign = { x: Axis.getOriginSign(origin.x, domain.x), y: Axis.getOriginSign(origin.y, domain.y) }; - const altOrientations = { - x: getAltOrientation("x", originSign.y, horizontal), - y: getAltOrientation("y", originSign.x, horizontal) - }; const orientations = { - x: orientation === "bottom" || orientation === "top" ? orientation : altOrientations.x, - y: orientation === "left" || orientation === "right" ? orientation : altOrientations.y + x: + orientation === "bottom" || orientation === "top" + ? orientation + : getDefaultOrientations("x", originSign.y, horizontal), + y: + orientation === "left" || orientation === "right" + ? orientation + : getDefaultOrientations("y", originSign.x, horizontal) }; // padding @@ -235,7 +208,8 @@ const getOffset = (props, calculatedValues) => { y: orientations.x === "bottom" ? bottom : top, x: orientations.y === "left" ? left : right }; - // if left & bottom - this is 0 for x and 300 (height) for y + // if left & bottom - this is 0 for x and height for y + // else - width for x and 0 for y const originOffset = { x: orientations.y === "left" ? 0 : props.width, y: orientations.x === "bottom" ? props.height : 0 @@ -267,13 +241,15 @@ const getHorizontalOffset = (props, calculatedValues) => { x: Axis.getOriginSign(origin.x, domain.x), y: Axis.getOriginSign(origin.y, domain.y) }; - const altOrientations = { - x: getAltOrientation("x", originSign.y, horizontal), - y: getAltOrientation("y", originSign.x, horizontal) - }; const orientations = { - y: orientation === "bottom" || orientation === "top" ? orientation : altOrientations.x, - x: orientation === "left" || orientation === "right" ? orientation : altOrientations.y + y: + orientation === "bottom" || orientation === "top" + ? orientation + : getDefaultOrientations("x", originSign.y, horizontal), + x: + orientation === "left" || orientation === "right" + ? orientation + : getDefaultOrientations("y", originSign.x, horizontal) }; // make the axes line up, and cross when appropriate @@ -291,9 +267,14 @@ const getHorizontalOffset = (props, calculatedValues) => { y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) }; + const x = originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x; + const y = originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y; + const offsetX = props.offsetX !== null && props.offsetX !== undefined ? x - props.offsetX : x; + const offsetY = props.offsetY !== null && props.offsetY !== undefined ? y - props.offsetY : y; + return { - y: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, - x: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y + y: offsetX, + x: offsetY }; }; @@ -361,7 +342,9 @@ const getGridOffset = (props, calculatedValues, offset) => { }; const getLayoutProps = (modifiedProps, calculatedValues) => { - const offset = modifiedProps.horizontal ? getHorizontalOffset(modifiedProps, calculatedValues) : getOffset(modifiedProps, calculatedValues); + const offset = modifiedProps.horizontal + ? getHorizontalOffset(modifiedProps, calculatedValues) + : getOffset(modifiedProps, calculatedValues); return { globalTransform: getTransform(modifiedProps, calculatedValues, offset), gridOffset: getGridOffset(modifiedProps, calculatedValues, offset), @@ -389,6 +372,10 @@ const getCalculatedValues = (props) => { const stringTicks = Axis.stringTicks(props) ? props.tickValues : undefined; const axis = Axis.getAxis(props); const orientation = getOrientation(props); + const currentAxisScale = getScale(props); + const ticks = Axis.getTicks(props, currentAxisScale, props.crossAxis); + const tickFormat = Axis.getTickFormat(props, currentAxisScale); + const anchors = getAnchors(orientation, isVertical); const range = { x: Helpers.getRange(props, "x"), @@ -406,9 +393,6 @@ const getCalculatedValues = (props) => { .domain(domain.y) .range(props.horizontal ? range.x : range.y) }; - const ticks = Axis.getTicks(props, getScale(props), props.crossAxis); - const tickFormat = Axis.getTickFormat(props, getScale(props)); - const anchors = getAnchors(orientation, isVertical); return { axis, diff --git a/packages/victory-chart/src/helper-methods.js b/packages/victory-chart/src/helper-methods.js index 04a759f23..ce823aaac 100644 --- a/packages/victory-chart/src/helper-methods.js +++ b/packages/victory-chart/src/helper-methods.js @@ -206,74 +206,6 @@ const getDomain = (props, axis, childComponents) => { return invertDomain ? domain.concat().reverse() : domain; }; -// const getAxisOffset = (props, calculatedProps, orientation) => { -// const { scale, origin, domain, padding } = calculatedProps; -// const { top, bottom, left, right } = padding; -// const orientations = { -// x: -// orientation === "bottom" || orientation === "top" -// ? orientation -// : calculatedProps.orientations.x, -// y: -// orientation === "left" || orientation === "right" -// ? orientation -// : calculatedProps.orientations.y -// }; -// // make the axes line up, and cross when appropriate -// const orientationOffset = { -// y: orientations.x === "bottom" ? bottom : top, -// x: orientations.y === "left" ? left : right -// }; -// const originOffset = { -// x: orientations.y === "left" ? 0 : props.width, -// y: orientations.x === "bottom" ? props.height : 0 -// }; - -// const originPosition = { -// x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), -// y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) -// }; - -// return { -// x: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, -// y: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y -// }; -// }; - -// const getHorizontalAxisOffset = (props, calculatedProps, orientation) => { -// const { scale, origin, domain, padding } = calculatedProps; -// const { top, bottom, left, right } = padding; -// const orientations = { -// y: -// orientation === "bottom" || orientation === "top" -// ? orientation -// : calculatedProps.orientations.x, -// x: -// orientation === "left" || orientation === "right" -// ? orientation -// : calculatedProps.orientations.y -// }; -// // make the axes line up, and cross when appropriate -// const orientationOffset = { -// x: orientations.y === "bottom" ? bottom : top, -// y: orientations.x === "left" ? left : right -// }; -// const originOffset = { -// y: orientations.x === "left" ? 0 : props.width, -// x: orientations.y === "bottom" ? props.height : 0 -// }; - -// const originPosition = { -// x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), -// y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) -// }; - -// return { -// y: originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x, -// x: originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y -// }; -// }; - function getOrientation(axis, originSign, horizontal) { const sign = originSign || "positive"; const orientations = { From e817500bfe04f652037853bce03274be1bd9d99a Mon Sep 17 00:00:00 2001 From: jhumbug Date: Tue, 11 May 2021 13:55:45 -0400 Subject: [PATCH 07/14] offload some offset calcs to getCalculatedValues, use current axis domain and scale if available --- packages/victory-axis/src/helper-methods.js | 85 +++++++++------------ 1 file changed, 36 insertions(+), 49 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index a050ba325..dcb511fc0 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -184,37 +184,18 @@ const getDefaultOrientations = (axis, originSign, horizontal) => { // eslint-disable-next-line complexity const getOffset = (props, calculatedValues) => { - const { scale, orientation, domain, padding } = calculatedValues; - const { polar, horizontal } = props; + const { scale, origin, orientations, domain, padding } = calculatedValues; const { top, bottom, left, right } = padding; - const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); - const originSign = { - x: Axis.getOriginSign(origin.x, domain.x), - y: Axis.getOriginSign(origin.y, domain.y) - }; - const orientations = { - x: - orientation === "bottom" || orientation === "top" - ? orientation - : getDefaultOrientations("x", originSign.y, horizontal), - y: - orientation === "left" || orientation === "right" - ? orientation - : getDefaultOrientations("y", originSign.x, horizontal) - }; - // padding + // make the axes line up, and cross when appropriate const orientationOffset = { y: orientations.x === "bottom" ? bottom : top, x: orientations.y === "left" ? left : right }; - // if left & bottom - this is 0 for x and height for y - // else - width for x and 0 for y const originOffset = { x: orientations.y === "left" ? 0 : props.width, y: orientations.x === "bottom" ? props.height : 0 }; - // distance from top left corner const originPosition = { x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) @@ -233,24 +214,8 @@ const getOffset = (props, calculatedValues) => { // eslint-disable-next-line complexity const getHorizontalOffset = (props, calculatedValues) => { - const { scale, orientation, domain, padding } = calculatedValues; - const { polar, horizontal } = props; + const { scale, origin, orientations, domain, padding } = calculatedValues; const { top, bottom, left, right } = padding; - const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); - const originSign = { - x: Axis.getOriginSign(origin.x, domain.x), - y: Axis.getOriginSign(origin.y, domain.y) - }; - const orientations = { - y: - orientation === "bottom" || orientation === "top" - ? orientation - : getDefaultOrientations("x", originSign.y, horizontal), - x: - orientation === "left" || orientation === "right" - ? orientation - : getDefaultOrientations("y", originSign.x, horizontal) - }; // make the axes line up, and cross when appropriate const orientationOffset = { @@ -261,7 +226,6 @@ const getHorizontalOffset = (props, calculatedValues) => { y: orientations.x === "left" ? 0 : props.width, x: orientations.y === "bottom" ? props.height : 0 }; - const originPosition = { x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) @@ -372,26 +336,47 @@ const getCalculatedValues = (props) => { const stringTicks = Axis.stringTicks(props) ? props.tickValues : undefined; const axis = Axis.getAxis(props); const orientation = getOrientation(props); + const currentAxisDomain = Axis.getDomain(props); const currentAxisScale = getScale(props); const ticks = Axis.getTicks(props, currentAxisScale, props.crossAxis); const tickFormat = Axis.getTickFormat(props, currentAxisScale); const anchors = getAnchors(orientation, isVertical); - const range = { x: Helpers.getRange(props, "x"), y: Helpers.getRange(props, "y") }; const domain = { - x: Domain.getDomain(props, "x"), - y: Domain.getDomain(props, "y") + x: axis === "x" && currentAxisDomain ? currentAxisDomain : Domain.getDomain(props, "x"), + y: axis === "y" && currentAxisDomain ? currentAxisDomain : Domain.getDomain(props, "y") }; const scale = { - x: Scale.getBaseScale(props, "x") - .domain(domain.x) - .range(props.horizontal ? range.y : range.x), - y: Scale.getBaseScale(props, "y") - .domain(domain.y) - .range(props.horizontal ? range.x : range.y) + x: + axis === "x" && currentAxisScale + ? currentAxisScale + : Scale.getBaseScale(props, "x") + .domain(domain.x) + .range(props.horizontal ? range.y : range.x), + y: + axis === "y" && currentAxisScale + ? currentAxisScale + : Scale.getBaseScale(props, "y") + .domain(domain.y) + .range(props.horizontal ? range.x : range.y) + }; + const origin = Axis.getOrigin(domain); + const originSign = { + x: Axis.getOriginSign(origin.x, domain.x), + y: Axis.getOriginSign(origin.y, domain.y) + }; + const orientations = { + x: + orientation === "bottom" || orientation === "top" + ? orientation + : getDefaultOrientations("x", originSign.y, props.horizontal), + y: + orientation === "left" || orientation === "right" + ? orientation + : getDefaultOrientations("y", originSign.x, props.horizontal) }; return { @@ -406,7 +391,9 @@ const getCalculatedValues = (props) => { scale, ticks, tickFormat, - domain + domain, + origin, + orientations }; }; From ee8d26f78644ef0c0488b4e018800ec5025ac536 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Tue, 11 May 2021 14:53:30 -0400 Subject: [PATCH 08/14] calculate crossAxis is VictoryAxis rather than VictoryChart --- packages/victory-axis/src/helper-methods.js | 4 +++- packages/victory-chart/src/helper-methods.js | 10 ++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index dcb511fc0..a2a32a83c 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -327,6 +327,7 @@ const getOrientation = (props) => { return props.dependentAxis ? defaultOrientations.dependent : defaultOrientations.independent; }; +// eslint-disable-next-line complexity const getCalculatedValues = (props) => { const defaultStyles = getStyleObject(props); const style = getStyles(props, defaultStyles); @@ -338,7 +339,8 @@ const getCalculatedValues = (props) => { const orientation = getOrientation(props); const currentAxisDomain = Axis.getDomain(props); const currentAxisScale = getScale(props); - const ticks = Axis.getTicks(props, currentAxisScale, props.crossAxis); + const crossAxis = props.crossAxis === false || props.standalone === true ? false : true; + const ticks = Axis.getTicks(props, currentAxisScale, crossAxis); const tickFormat = Axis.getTickFormat(props, currentAxisScale); const anchors = getAnchors(orientation, isVertical); const range = { diff --git a/packages/victory-chart/src/helper-methods.js b/packages/victory-chart/src/helper-methods.js index ce823aaac..ef8b3d25d 100644 --- a/packages/victory-chart/src/helper-methods.js +++ b/packages/victory-chart/src/helper-methods.js @@ -12,11 +12,7 @@ const fallbackProps = { }; function getAxisProps(child, props, calculatedProps) { - const { domain, scale, stringMap, categories, horizontal, orientations } = calculatedProps; - const childProps = Axis.modifyProps(defaults({ horizontal, theme: props.theme }, child.props)); - const axis = child.type.getAxis(childProps); - const crossAxis = childProps.crossAxis === false ? false : true; - const orientation = childProps.orientation || orientations[axis]; + const { domain, scale, stringMap, categories, horizontal } = calculatedProps; return { stringMap, horizontal, @@ -25,9 +21,7 @@ function getAxisProps(child, props, calculatedProps) { endAngle: props.endAngle, innerRadius: props.innerRadius, domain, - scale, - crossAxis, - orientation + scale }; } From d633f4d77089ef01029ec7626db3bf044bdcc335 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Tue, 11 May 2021 16:16:16 -0400 Subject: [PATCH 09/14] calculate orientation correctly in VictoryAxis --- packages/victory-axis/src/helper-methods.js | 62 +++++++++------------ 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index a2a32a83c..62caba4ef 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -184,17 +184,22 @@ const getDefaultOrientations = (axis, originSign, horizontal) => { // eslint-disable-next-line complexity const getOffset = (props, calculatedValues) => { - const { scale, origin, orientations, domain, padding } = calculatedValues; + const { scale, origin, orientation, orientations, domain, padding } = calculatedValues; const { top, bottom, left, right } = padding; + const calculatedOrientation = { + x: orientation === "bottom" || orientation === "top" ? orientation : orientations.x, + y: orientation === "left" || orientation === "right" ? orientation : orientations.y + }; + // // make the axes line up, and cross when appropriate const orientationOffset = { - y: orientations.x === "bottom" ? bottom : top, - x: orientations.y === "left" ? left : right + x: calculatedOrientation.y === "left" ? left : right, + y: calculatedOrientation.x === "bottom" ? bottom : top }; const originOffset = { - x: orientations.y === "left" ? 0 : props.width, - y: orientations.x === "bottom" ? props.height : 0 + x: calculatedOrientation.y === "left" ? 0 : props.width, + y: calculatedOrientation.x === "bottom" ? props.height : 0 }; const originPosition = { x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), @@ -214,25 +219,30 @@ const getOffset = (props, calculatedValues) => { // eslint-disable-next-line complexity const getHorizontalOffset = (props, calculatedValues) => { - const { scale, origin, orientations, domain, padding } = calculatedValues; + const { scale, origin, orientation, orientations, domain, padding } = calculatedValues; const { top, bottom, left, right } = padding; + const calculatedOrientation = { + y: orientation === "bottom" || orientation === "top" ? orientation : orientations.x, + x: orientation === "left" || orientation === "right" ? orientation : orientations.y + }; + // make the axes line up, and cross when appropriate const orientationOffset = { - x: orientations.y === "bottom" ? bottom : top, - y: orientations.x === "left" ? left : right + x: calculatedOrientation.y === "bottom" ? bottom : top, + y: calculatedOrientation.x === "left" ? left : right }; const originOffset = { - y: orientations.x === "left" ? 0 : props.width, - x: orientations.y === "bottom" ? props.height : 0 + y: calculatedOrientation.x === "left" ? 0 : props.width, + x: calculatedOrientation.y === "bottom" ? props.height : 0 }; const originPosition = { x: origin.x === domain.x[0] || origin.x === domain.x[1] ? 0 : scale.x(origin.x), y: origin.y === domain.y[0] || origin.y === domain.y[1] ? 0 : scale.y(origin.y) }; - const x = originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x; - const y = originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y; + const y = originPosition.x ? Math.abs(originOffset.x - originPosition.x) : orientationOffset.x; + const x = originPosition.y ? Math.abs(originOffset.y - originPosition.y) : orientationOffset.y; const offsetX = props.offsetX !== null && props.offsetX !== undefined ? x - props.offsetX : x; const offsetY = props.offsetY !== null && props.offsetY !== undefined ? y - props.offsetY : y; @@ -316,33 +326,19 @@ const getLayoutProps = (modifiedProps, calculatedValues) => { }; }; -const getOrientation = (props) => { - if (props.orientation) { - return props.orientation; - } - const defaultOrientations = { - dependent: props.horizontal ? "bottom" : "left", - independent: props.horizontal ? "left" : "bottom" - }; - return props.dependentAxis ? defaultOrientations.dependent : defaultOrientations.independent; -}; - // eslint-disable-next-line complexity const getCalculatedValues = (props) => { const defaultStyles = getStyleObject(props); const style = getStyles(props, defaultStyles); const padding = Helpers.getPadding(props); - const isVertical = Axis.isVertical(props); const labelPadding = getLabelPadding(props, style); const stringTicks = Axis.stringTicks(props) ? props.tickValues : undefined; const axis = Axis.getAxis(props); - const orientation = getOrientation(props); const currentAxisDomain = Axis.getDomain(props); const currentAxisScale = getScale(props); const crossAxis = props.crossAxis === false || props.standalone === true ? false : true; const ticks = Axis.getTicks(props, currentAxisScale, crossAxis); const tickFormat = Axis.getTickFormat(props, currentAxisScale); - const anchors = getAnchors(orientation, isVertical); const range = { x: Helpers.getRange(props, "x"), y: Helpers.getRange(props, "y") @@ -371,16 +367,12 @@ const getCalculatedValues = (props) => { y: Axis.getOriginSign(origin.y, domain.y) }; const orientations = { - x: - orientation === "bottom" || orientation === "top" - ? orientation - : getDefaultOrientations("x", originSign.y, props.horizontal), - y: - orientation === "left" || orientation === "right" - ? orientation - : getDefaultOrientations("y", originSign.x, props.horizontal) + x: getDefaultOrientations("x", originSign.y, props.horizontal), + y: getDefaultOrientations("y", originSign.x, props.horizontal) }; - + const orientation = props.orientation || orientations[axis]; + const isVertical = Axis.isVertical(Object.assign({}, props, { orientation })); + const anchors = getAnchors(orientation, isVertical); return { axis, style, From 931070aace82bafa9e2038b580988e8a4f66c902 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Tue, 11 May 2021 16:43:07 -0400 Subject: [PATCH 10/14] fix to grid offset and horizontal offset --- packages/victory-axis/src/helper-methods.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 62caba4ef..445e3d3ed 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -247,8 +247,8 @@ const getHorizontalOffset = (props, calculatedValues) => { const offsetY = props.offsetY !== null && props.offsetY !== undefined ? y - props.offsetY : y; return { - y: offsetX, - x: offsetY + x: offsetX, + y: offsetY }; }; @@ -305,13 +305,13 @@ const getGridEdge = (props, calculatedValues) => { return { x, y }; }; -const getGridOffset = (props, calculatedValues, offset) => { - const { padding, orientation } = calculatedValues; +const getGridOffset = (calculatedValues, offset) => { + const { padding, orientation, crossAxis } = calculatedValues; const xPadding = orientation === "right" ? padding.right : padding.left; const yPadding = orientation === "top" ? padding.top : padding.bottom; return { - x: props.crossAxis ? offset.x - xPadding : 0, - y: props.crossAxis ? offset.y - yPadding : 0 + x: crossAxis ? offset.x - xPadding : 0, + y: crossAxis ? offset.y - yPadding : 0 }; }; @@ -321,7 +321,7 @@ const getLayoutProps = (modifiedProps, calculatedValues) => { : getOffset(modifiedProps, calculatedValues); return { globalTransform: getTransform(modifiedProps, calculatedValues, offset), - gridOffset: getGridOffset(modifiedProps, calculatedValues, offset), + gridOffset: getGridOffset(calculatedValues, offset), gridEdge: getGridEdge(modifiedProps, calculatedValues) }; }; @@ -387,7 +387,8 @@ const getCalculatedValues = (props) => { tickFormat, domain, origin, - orientations + orientations, + crossAxis }; }; From 17b24c7bcc97d547139588a78b488a15ce7028b4 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Wed, 12 May 2021 09:37:38 -0400 Subject: [PATCH 11/14] factor in standalone axis offset and domain/scale/props --- packages/victory-axis/src/helper-methods.js | 151 ++++++++++++++------ 1 file changed, 107 insertions(+), 44 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 445e3d3ed..dd66b6a0d 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -1,5 +1,5 @@ import { assign, defaults } from "lodash"; -import { Helpers, Scale, Axis, Domain } from "victory-core"; +import { Helpers, Scale, Axis } from "victory-core"; const orientationSign = { top: -1, @@ -182,6 +182,41 @@ const getDefaultOrientations = (axis, originSign, horizontal) => { return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; }; +const getStandaloneOffset = (props, calculatedValues) => { + const { + style, + scale, + orientation, + padding, + axis, + ticks, + stringTicks, + isVertical, + labelPadding + } = calculatedValues; + const { polar, horizontal } = props; + const sharedProps = { scale: { [axis]: scale }, polar, horizontal, ticks, stringTicks }; + const xPadding = orientation === "right" ? padding.right : padding.left; + const yPadding = orientation === "top" ? padding.top : padding.bottom; + const offsetX = props.offsetX !== null && props.offsetX !== undefined ? props.offsetX : xPadding; + const offsetY = props.offsetY !== null && props.offsetY !== undefined ? props.offsetY : yPadding; + const fontSize = style.axisLabel.fontSize || 14; // eslint-disable-line no-magic-numbers + const tickSizes = ticks.map((data, index) => { + const tick = stringTicks ? props.tickValues[data - 1] : data; + const tickStyle = Helpers.evaluateStyle(style.ticks, assign({}, sharedProps, { tick, index })); + return tickStyle.size || 0; + }); + const totalPadding = fontSize + 2 * Math.max(...tickSizes) + labelPadding; + const minimumPadding = 1.2 * fontSize; // eslint-disable-line no-magic-numbers + const x = isVertical ? totalPadding : minimumPadding; + const y = isVertical ? minimumPadding : totalPadding; + + return { + x: offsetX !== null && offsetX !== undefined ? offsetX : x, + y: offsetY !== null && offsetY !== undefined ? offsetY : y + }; +}; + // eslint-disable-next-line complexity const getOffset = (props, calculatedValues) => { const { scale, origin, orientation, orientations, domain, padding } = calculatedValues; @@ -191,7 +226,7 @@ const getOffset = (props, calculatedValues) => { x: orientation === "bottom" || orientation === "top" ? orientation : orientations.x, y: orientation === "left" || orientation === "right" ? orientation : orientations.y }; - // + // make the axes line up, and cross when appropriate const orientationOffset = { x: calculatedOrientation.y === "left" ? left : right, @@ -316,9 +351,14 @@ const getGridOffset = (calculatedValues, offset) => { }; const getLayoutProps = (modifiedProps, calculatedValues) => { - const offset = modifiedProps.horizontal - ? getHorizontalOffset(modifiedProps, calculatedValues) - : getOffset(modifiedProps, calculatedValues); + let offset; + if (modifiedProps.standalone) { + offset = getStandaloneOffset(modifiedProps, calculatedValues); + } else { + offset = modifiedProps.horizontal + ? getHorizontalOffset(modifiedProps, calculatedValues) + : getOffset(modifiedProps, calculatedValues); + } return { globalTransform: getTransform(modifiedProps, calculatedValues, offset), gridOffset: getGridOffset(calculatedValues, offset), @@ -326,6 +366,17 @@ const getLayoutProps = (modifiedProps, calculatedValues) => { }; }; +const getOrientation = (props) => { + if (props.orientation) { + return props.orientation; + } + const defaultOrientations = { + dependent: props.horizontal ? "bottom" : "left", + independent: props.horizontal ? "left" : "bottom" + }; + return props.dependentAxis ? defaultOrientations.dependent : defaultOrientations.independent; +}; + // eslint-disable-next-line complexity const getCalculatedValues = (props) => { const defaultStyles = getStyleObject(props); @@ -334,61 +385,73 @@ const getCalculatedValues = (props) => { const labelPadding = getLabelPadding(props, style); const stringTicks = Axis.stringTicks(props) ? props.tickValues : undefined; const axis = Axis.getAxis(props); - const currentAxisDomain = Axis.getDomain(props); - const currentAxisScale = getScale(props); + const axisDomain = Axis.getDomain(props); + const axisScale = getScale(props); + const xAxisDomain = axis === "x" ? axisDomain : undefined; + const yAxisDomain = axis === "y" ? axisDomain : undefined; + const xAxisScale = axis === "x" ? axisScale : undefined; + const yAxisScale = axis === "y" ? axisScale : undefined; const crossAxis = props.crossAxis === false || props.standalone === true ? false : true; - const ticks = Axis.getTicks(props, currentAxisScale, crossAxis); - const tickFormat = Axis.getTickFormat(props, currentAxisScale); + const ticks = Axis.getTicks(props, axisScale, crossAxis); + const tickFormat = Axis.getTickFormat(props, axisScale); const range = { x: Helpers.getRange(props, "x"), y: Helpers.getRange(props, "y") }; + // use full domain if passed in from parent, + // otherwise use the just the one axis available const domain = { - x: axis === "x" && currentAxisDomain ? currentAxisDomain : Domain.getDomain(props, "x"), - y: axis === "y" && currentAxisDomain ? currentAxisDomain : Domain.getDomain(props, "y") + x: props.domain && props.domain.x ? props.domain.x : xAxisDomain, + y: props.domain && props.domain.y ? props.domain.y : yAxisDomain }; + // use full scale if passed in from parent, + // otherwise use the just the one axis available const scale = { - x: - axis === "x" && currentAxisScale - ? currentAxisScale - : Scale.getBaseScale(props, "x") - .domain(domain.x) - .range(props.horizontal ? range.y : range.x), - y: - axis === "y" && currentAxisScale - ? currentAxisScale - : Scale.getBaseScale(props, "y") - .domain(domain.y) - .range(props.horizontal ? range.x : range.y) - }; - const origin = Axis.getOrigin(domain); - const originSign = { - x: Axis.getOriginSign(origin.x, domain.x), - y: Axis.getOriginSign(origin.y, domain.y) - }; - const orientations = { - x: getDefaultOrientations("x", originSign.y, props.horizontal), - y: getDefaultOrientations("y", originSign.x, props.horizontal) - }; - const orientation = props.orientation || orientations[axis]; + x: domain.x + ? Scale.getBaseScale(props, "x") + .domain(domain.x) + .range(props.horizontal ? range.y : range.x) + : xAxisScale, + y: domain.y + ? Scale.getBaseScale(props, "y") + .domain(domain.y) + .range(props.horizontal ? range.x : range.y) + : yAxisScale + }; + const origin = domain.x && domain.y ? Axis.getOrigin(domain) : undefined; + const originSign = origin + ? { + x: Axis.getOriginSign(origin.x, domain.x), + y: Axis.getOriginSign(origin.y, domain.y) + } + : undefined; + const orientations = originSign + ? { + x: getDefaultOrientations("x", originSign.y, props.horizontal), + y: getDefaultOrientations("y", originSign.x, props.horizontal) + } + : undefined; + const orientation = orientations + ? props.orientation || orientations[axis] + : getOrientation(props); const isVertical = Axis.isVertical(Object.assign({}, props, { orientation })); const anchors = getAnchors(orientation, isVertical); return { + anchors, axis, - style, - padding, - orientation, + crossAxis, + domain, isVertical, labelPadding, - stringTicks, - anchors, + orientation, + orientations, + origin, + padding, scale, - ticks, + stringTicks, + style, tickFormat, - domain, - origin, - orientations, - crossAxis + ticks }; }; From 36c063be1f8c39ada40a9f340d5865ab10b1a01b Mon Sep 17 00:00:00 2001 From: jhumbug Date: Wed, 12 May 2021 10:28:50 -0400 Subject: [PATCH 12/14] fix for checking how to calculate scale --- packages/victory-axis/src/helper-methods.js | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index dd66b6a0d..3033040c7 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -407,16 +407,18 @@ const getCalculatedValues = (props) => { // use full scale if passed in from parent, // otherwise use the just the one axis available const scale = { - x: domain.x - ? Scale.getBaseScale(props, "x") - .domain(domain.x) - .range(props.horizontal ? range.y : range.x) - : xAxisScale, - y: domain.y - ? Scale.getBaseScale(props, "y") - .domain(domain.y) - .range(props.horizontal ? range.x : range.y) - : yAxisScale + x: + props.domain && props.domain.x + ? Scale.getBaseScale(props, "x") + .domain(props.domain.x) + .range(props.horizontal ? range.y : range.x) + : xAxisScale, + y: + props.domain && props.domain.y + ? Scale.getBaseScale(props, "y") + .domain(props.domain.y) + .range(props.horizontal ? range.x : range.y) + : yAxisScale }; const origin = domain.x && domain.y ? Axis.getOrigin(domain) : undefined; const originSign = origin From d5bacbbdc8324e90918acecec071318a6ba2f872 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Wed, 12 May 2021 16:01:18 -0400 Subject: [PATCH 13/14] use domain x/y check rather than standalone prop for single axis layout --- packages/victory-axis/src/helper-methods.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/victory-axis/src/helper-methods.js b/packages/victory-axis/src/helper-methods.js index 3033040c7..c9b1e5219 100644 --- a/packages/victory-axis/src/helper-methods.js +++ b/packages/victory-axis/src/helper-methods.js @@ -352,12 +352,12 @@ const getGridOffset = (calculatedValues, offset) => { const getLayoutProps = (modifiedProps, calculatedValues) => { let offset; - if (modifiedProps.standalone) { - offset = getStandaloneOffset(modifiedProps, calculatedValues); - } else { + if (calculatedValues.domain.x && calculatedValues.domain.y) { offset = modifiedProps.horizontal ? getHorizontalOffset(modifiedProps, calculatedValues) : getOffset(modifiedProps, calculatedValues); + } else { + offset = getStandaloneOffset(modifiedProps, calculatedValues); } return { globalTransform: getTransform(modifiedProps, calculatedValues, offset), From 45520db1014bdf11ae15cf934bba898ef17cce46 Mon Sep 17 00:00:00 2001 From: jhumbug Date: Wed, 12 May 2021 16:09:50 -0400 Subject: [PATCH 14/14] removed orientations from VictoryChart --- packages/victory-chart/src/helper-methods.js | 26 +------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/packages/victory-chart/src/helper-methods.js b/packages/victory-chart/src/helper-methods.js index ef8b3d25d..72ec0f0c8 100644 --- a/packages/victory-chart/src/helper-methods.js +++ b/packages/victory-chart/src/helper-methods.js @@ -109,16 +109,6 @@ function getCalculatedProps(props, childComponents) { const origin = polar ? Helpers.getPolarOrigin(props) : Axis.getOrigin(domain); - const originSign = { - x: Axis.getOriginSign(origin.x, domain.x), - y: Axis.getOriginSign(origin.y, domain.y) - }; - - const orientations = { - x: getOrientation("x", originSign.y, horizontal), - y: getOrientation("y", originSign.x, horizontal) - }; - const padding = Helpers.getPadding(props); return { @@ -130,8 +120,7 @@ function getCalculatedProps(props, childComponents) { stringMap, style, origin, - padding, - orientations + padding }; } @@ -200,19 +189,6 @@ const getDomain = (props, axis, childComponents) => { return invertDomain ? domain.concat().reverse() : domain; }; -function getOrientation(axis, originSign, horizontal) { - const sign = originSign || "positive"; - const orientations = { - positive: { x: "bottom", y: "left" }, - negative: { x: "top", y: "right" } - }; - const horizontalOrientations = { - positive: { x: "left", y: "bottom" }, - negative: { x: "right", y: "top" } - }; - return horizontal ? horizontalOrientations[sign][axis] : orientations[sign][axis]; -} - const createStringMap = (props, childComponents, allStrings) => { const x = !allStrings.x || allStrings.x.length === 0