Skip to content

Commit

Permalink
Merge pull request #1301
Browse files Browse the repository at this point in the history
Normal curve for histograms
  • Loading branch information
bfinzer authored Jun 7, 2024
2 parents 963a7f4 + ee03c39 commit ad692e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion v3/src/components/data-display/components/background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const Background = forwardRef<SVGGElement | HTMLDivElement, IProps>((prop
})
}
marqueeState.setMarqueeRect({x: startX.current, y: startY.current, width: 0, height: 0})
}, [datasetsArray, marqueeState, pixiPointsArrayRef]),
}, [bgRef, datasetsArray, marqueeState, pixiPointsArrayRef]),

onDrag = useCallback((event: { dx: number; dy: number }) => {
if (event.dx !== 0 || event.dy !== 0 && datasetsArray.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const NormalCurveAdornmentComponent = observer(
const helper = useMemo(() => {
return new UnivariateMeasureAdornmentHelper(cellKey, layout, model, plotHeight, plotWidth, containerId)
}, [cellKey, containerId, layout, model, plotHeight, plotWidth])
const isHistogram = graphModel.pointDisplayType === "histogram"
const numericScale = isVertical.current ? helper.xScale : helper.yScale
const countScale = isHistogram ? (isVertical.current ? helper.yScale : helper.xScale) : undefined
const {cellCounts} = useAdornmentCells(model, cellKey)
const isBlockingOtherMeasure = dataConfig &&
helper.blocksOtherMeasure({adornmentsStore, attrId: numericAttrId, dataConfig, isVertical: isVertical.current})
Expand Down Expand Up @@ -174,7 +176,6 @@ export const NormalCurveAdornmentComponent = observer(
* - the line segment representing a specified number of standard errors on each side of the mean
*/
const symbolPathF = (p: { x: number, y: number, width: number, cellHeight: number }, iIsHorizontal: boolean) => {

const normalF = (x: number) => {
return normal(x, amplitude, mean, stdDev)
}
Expand All @@ -184,27 +185,37 @@ export const NormalCurveAdornmentComponent = observer(
return iIsHorizontal ? p.y - tStackCoord : p.x + tStackCoord
}

const countToScreenCoordFromHistogram = (iCount: number) => {
if (!countScale) {
return 0
} else {
return isVertical.current ? countScale(iCount) / cellCounts.y : countScale(iCount) / cellCounts.x
}
}

const countAxisFunc = isHistogram ? countToScreenCoordFromHistogram : countToScreenCoordFromDotPlot,
sqrtTwoPi = Math.sqrt(2 * Math.PI),
pointRadius = graphModel.getPointRadius(),
numCellsNumeric = isVertical.current ? cellCounts.x : cellCounts.y,
overlap = graphModel.pointOverlap,
binWidth = isHistogram ? graphModel.binWidth
: Math.abs(numericScale.invert(pointRadius * 2) - numericScale.invert(0))

if (!countAxisFunc || binWidth === undefined) return ""

let path = ''
/*
let sESegment = '',
sESegmentPixelLength: number
*/


const sqrtTwoPi = Math.sqrt(2 * Math.PI),
// isHistogram = false, //this.getPath('model.plotModel.dotsAreFused'),
countAxisFunc = /*isHistogram ? tCountAxisView.dataToCoordinate.bind(tCountAxisView)
:*/ countToScreenCoordFromDotPlot,
pointRadius = graphModel.getPointRadius(),
numCellsNumeric = isVertical.current ? cellCounts.x : cellCounts.y,
overlap = graphModel.pointOverlap,
binWidth = /*isHistogram ? tParentPlotView.getPath('model.width')
:*/ Math.abs(numericScale.invert(pointRadius * 2) - numericScale.invert(0)),
const
pixelRange = numericScale.range(),
pixelMin = isVertical.current ? pixelRange[0] : pixelRange[1],
pixelMax = isVertical.current ? pixelRange[1] : pixelRange[0],
numeratorForAmplitude = isHistogram ? 1 : numCellsNumeric,
// todo: For a gaussian fit amplitude is a fitted parameter
amplitude = (numCellsNumeric / (stdDev * sqrtTwoPi)) * caseCount * binWidth,
amplitude = (numeratorForAmplitude / (stdDev * sqrtTwoPi)) * caseCount * binWidth,
points = [],
kPixelGap = 1,
meanSegmentPixelLength = countAxisFunc(normalF(mean)) - countAxisFunc(0),
Expand Down Expand Up @@ -272,8 +283,8 @@ export const NormalCurveAdornmentComponent = observer(
.attr("id", `${helper.generateIdString("path")}`)
.attr("data-testid", `${helper.measureSlug}-normal-curve`)
.attr("d", theSymbolPath)
}, [caseCount, cellCounts.x, cellCounts.y, graphModel, helper, isVertical,
layout.plotHeight, mean, numericScale, stdDev, valueRef])
}, [caseCount, cellCounts.x, cellCounts.y, countScale, graphModel, helper, isHistogram, isVertical,
layout.plotHeight, mean, numericScale, stdDev, valueRef])

const addAdornmentElements = useCallback((measure: IMeasureInstance,
selectionsObj: INormalCurveSelections, labelObj: ILabel) => {
Expand Down

0 comments on commit ad692e9

Please sign in to comment.