Skip to content

Commit

Permalink
Merge pull request #1521 from silx-kit/fix-min-length-error
Browse files Browse the repository at this point in the history
[HeatmapVis] Fix min axis array length off by 1 in error message
  • Loading branch information
axelboc authored Oct 31, 2023
2 parents d4b4d46 + 7af02a8 commit 04a0f75
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
5 changes: 3 additions & 2 deletions packages/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export {
getDomains,
getCombinedDomain,
extendDomain,
getAxisValues,
getAxisDomain,
getValueToIndexScale,
createBufferAttr,
Expand All @@ -111,13 +110,15 @@ export {
useDomain,
useDomains,
useCombinedDomain,
useAxisValues,
useAxisDomain,
useValueToIndexScale,
useCameraState,
useGeometry,
} from './vis/hooks';

export { getAxisValues } from './vis/line/utils';
export { useAxisValues } from './vis/line/hooks';

export {
getLinearGradient,
getVisDomain,
Expand Down
17 changes: 13 additions & 4 deletions packages/lib/src/vis/heatmap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

import type { CustomDomain, DomainErrors } from '../models';
import { DomainError } from '../models';
import { getAxisValues, SCALES_VALID_MINS } from '../utils';
import { SCALES_VALID_MINS } from '../utils';
import { INTERPOLATORS } from './interpolators';
import type { ColorMap, D3Interpolator, TextureSafeTypedArray } from './models';

Expand Down Expand Up @@ -108,14 +108,23 @@ export function getPixelEdgeValues(
rawValues: NumArray | undefined,
pixelCount: number,
): NumArray {
if (rawValues && rawValues.length === pixelCount) {
const pixelEdgeCount = pixelCount + 1;

if (!rawValues) {
return range(pixelEdgeCount);
}

if (rawValues.length < pixelCount) {
throw new Error(`Expected array to have length ${pixelCount} at least`);
}

if (rawValues.length === pixelCount) {
// Add value at right-hand side of last pixel, assuming raw values are regularly spaced
const deltaCoord = rawValues[1] - rawValues[0];
return [...rawValues, rawValues[rawValues.length - 1] + deltaCoord];
}

// nEdges = nPixels + 1
return getAxisValues(rawValues, pixelCount + 1);
return rawValues.slice(0, pixelEdgeCount);
}

export function getInterpolator(colorMap: ColorMap, reverse: boolean) {
Expand Down
2 changes: 0 additions & 2 deletions packages/lib/src/vis/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type { BufferAttribute } from 'three';
import type H5WebGeometry from './shared/h5webGeometry';
import {
getAxisDomain,
getAxisValues,
getCombinedDomain,
getValueToIndexScale,
} from './utils';
Expand All @@ -26,7 +25,6 @@ export const useValidDomainForScale = createMemo(getValidDomainForScale);
export const useCombinedDomain = createMemo(getCombinedDomain);
export const useValueToIndexScale = createMemo(getValueToIndexScale);
export const useAxisDomain = createMemo(getAxisDomain);
export const useAxisValues = createMemo(getAxisValues);

export function useDomain(
valuesArray: AnyNumArray,
Expand Down
8 changes: 2 additions & 6 deletions packages/lib/src/vis/line/LineVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ import { useMemo } from 'react';
import type { DefaultInteractionsConfig } from '../../interactions/DefaultInteractions';
import DefaultInteractions from '../../interactions/DefaultInteractions';
import ResetZoomButton from '../../toolbar/floating/ResetZoomButton';
import {
useAxisDomain,
useAxisValues,
useCssColors,
useValueToIndexScale,
} from '../hooks';
import { useAxisDomain, useCssColors, useValueToIndexScale } from '../hooks';
import type { AxisParams } from '../models';
import TooltipMesh from '../shared/TooltipMesh';
import VisCanvas from '../shared/VisCanvas';
import { DEFAULT_DOMAIN, extendDomain } from '../utils';
import DataCurve from './DataCurve';
import { useAxisValues } from './hooks';
import styles from './LineVis.module.css';
import type { AuxiliaryParams, TooltipData } from './models';
import { CurveType } from './models';
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/src/vis/line/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createMemo } from '@h5web/shared';

import { getAxisValues } from './utils';

export const useAxisValues = createMemo(getAxisValues);
17 changes: 17 additions & 0 deletions packages/lib/src/vis/line/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { NumArray } from '@h5web/shared';
import { range } from 'd3-array';

export function getAxisValues(
rawValues: NumArray | undefined,
axisLength: number,
): NumArray {
if (!rawValues) {
return range(axisLength);
}

if (rawValues.length < axisLength) {
throw new Error(`Expected array to have length ${axisLength} at least`);
}

return rawValues.slice(0, axisLength);
}
15 changes: 0 additions & 15 deletions packages/lib/src/vis/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,21 +376,6 @@ export function toArray(arr: NumArray): number[] {
return isTypedArray(arr) ? [...arr] : arr;
}

export function getAxisValues(
rawValues: NumArray | undefined,
axisLength: number,
): NumArray {
if (!rawValues) {
return range(axisLength);
}

if (rawValues.length < axisLength) {
throw new Error(`Expected array to have length ${axisLength} at least`);
}

return rawValues.slice(0, axisLength);
}

export function createBufferAttr(
dataLength: number,
itemSize = 3,
Expand Down

0 comments on commit 04a0f75

Please sign in to comment.