diff --git a/packages/lib/src/toolbar/controls/Histogram/Histogram.tsx b/packages/lib/src/toolbar/controls/Histogram/Histogram.tsx index 9f935a8c2..b5f12222c 100644 --- a/packages/lib/src/toolbar/controls/Histogram/Histogram.tsx +++ b/packages/lib/src/toolbar/controls/Histogram/Histogram.tsx @@ -4,7 +4,7 @@ import { AxisBottom, AxisLeft } from '@visx/axis'; import { scaleLinear } from '@visx/scale'; import { useSafeDomain } from '../../../vis/heatmap/hooks'; -import { useToArray, useCombinedDomain, useDomain } from '../../../vis/hooks'; +import { useCombinedDomain, useDomain } from '../../../vis/hooks'; import type { HistogramParams } from '../../../vis/models'; import { H5WEB_SCALES } from '../../../vis/scales'; import Tick from '../../../vis/shared/Tick'; @@ -48,8 +48,6 @@ function Histogram(props: Props) { const [size, ref] = useMeasure(); - const valuesArray = useToArray(values); - if (!size) { return
; } @@ -67,19 +65,24 @@ function Histogram(props: Props) { const markerPositions = safeValue.map(xScale) as Domain; + const rects: JSX.Element[] = []; + values.forEach((d, i) => + rects.push( + + ) + ); + return (
- {valuesArray.map((d, i) => ( - - ))} + {...rects} {colorMap && ( [number, number, number] ) { diff --git a/packages/lib/src/vis/utils.ts b/packages/lib/src/vis/utils.ts index 6c65b7240..ee3f4d718 100644 --- a/packages/lib/src/vis/utils.ts +++ b/packages/lib/src/vis/utils.ts @@ -162,7 +162,7 @@ function unsafeExtendDomain( } export function getValueToIndexScale( - values: number[], + values: NumArray, switchAtMidpoints?: boolean ): ScaleThreshold { const rawThresholds = switchAtMidpoints @@ -170,18 +170,15 @@ export function getValueToIndexScale( : values; // Else, the switch from i-1 to i will happen at values[i] // First threshold (going from 0 to 1) should be for the second value. Scaling the first value should return at 0. - const thresholds = rawThresholds.slice(1); + const thresholds = toArray(rawThresholds.slice(1)); const indices = range(values.length); + if (isDescending(thresholds)) { + thresholds.reverse(); + indices.reverse(); + } // ScaleThreshold only works with ascending values so the scale is reversed for descending values - return scaleThreshold( - isDescending(thresholds) - ? { - domain: [...thresholds].reverse(), - range: [...indices].reverse(), - } - : { domain: thresholds, range: indices } - ); + return scaleThreshold({ domain: thresholds, range: indices }); } export function getCanvasScale( @@ -300,12 +297,12 @@ export function getAxisOffsets( }; } -function isDescending(array: number[]): boolean { +function isDescending(array: NumArray): boolean { return array[array.length - 1] - array[0] < 0; } export function getAxisDomain( - axisValues: number[], + axisValues: NumArray, scaleType: ScaleType = ScaleType.Linear, extensionFactor = 0 ): Domain | undefined { @@ -384,7 +381,7 @@ export function getVisibleDomains( export function getAxisValues( rawValues: NumArray | undefined, axisLength: number -): number[] { +): NumArray { if (!rawValues) { return range(axisLength); } @@ -393,5 +390,5 @@ export function getAxisValues( throw new Error(`Expected array to have length ${axisLength} at least`); } - return toArray(rawValues).slice(0, axisLength); + return rawValues.slice(0, axisLength); }