Skip to content

Commit 68985c5

Browse files
committed
Support scaling_factor and offset for axis
1 parent 7d6c227 commit 68985c5

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

packages/app/src/vis-packs/nexus/NxValuesFetcher.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,22 @@ function NxValuesFetcher<T extends NumericLikeType | ComplexType>(
4343
const errors = useDatasetValue(signalDef.errorDataset, selection);
4444
const auxValues = useDatasetsValues(auxDatasets, selection);
4545
const auxErrors = useDatasetsValues(auxErrorDatasets, selection);
46-
const axisValues = useDatasetsValues(axisDatasets);
46+
const rawAxisValues = useDatasetsValues(axisDatasets);
47+
const axisValues = rawAxisValues.map((value, i) => {
48+
const axisScalingFactor = axisDefs[i]?.scalingFactor;
49+
const axisOffset = axisDefs[i]?.offset;
50+
if (value && (axisScalingFactor || axisOffset)) {
51+
const offset = axisOffset ?? 0;
52+
const scalingFactor = axisScalingFactor ?? 1;
53+
const scaledValue: number[] = [];
54+
value.forEach((v) => {
55+
scaledValue.push(scalingFactor * Number(v) + offset);
56+
});
57+
return scaledValue;
58+
}
59+
60+
return value;
61+
});
4762

4863
return render({ title, signal, errors, auxValues, auxErrors, axisValues });
4964
}

packages/app/src/vis-packs/nexus/hooks.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import {
1919
findTitleDataset,
2020
getDatasetInfo,
2121
getDefaultSlice,
22+
getScalingInfo,
2223
getSilxStyle,
2324
} from './utils';
2425

2526
export const useDefaultSlice = createMemo(getDefaultSlice);
2627

2728
export function useNxData(group: GroupWithChildren): NxData {
28-
const { attrValuesStore } = useDataContext();
29+
const { attrValuesStore, valuesStore } = useDataContext();
2930

3031
assertNxDataGroup(group, attrValuesStore);
3132
const signalDataset = findSignalDataset(group, attrValuesStore);
@@ -46,7 +47,11 @@ export function useNxData(group: GroupWithChildren): NxData {
4647
})),
4748
axisDefs: axisDatasets.map(
4849
(dataset) =>
49-
dataset && { dataset, ...getDatasetInfo(dataset, attrValuesStore) },
50+
dataset && {
51+
dataset,
52+
...getDatasetInfo(dataset, attrValuesStore),
53+
...getScalingInfo(group, dataset.name, valuesStore),
54+
},
5055
),
5156
defaultSlice: useDefaultSlice(group, signalDataset.shape, attrValuesStore),
5257
silxStyle: getSilxStyle(group, attrValuesStore),

packages/app/src/vis-packs/nexus/models.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export interface DatasetInfo {
2828
unit: string | undefined;
2929
}
3030

31+
export interface ScalingInfo {
32+
scalingFactor: number | undefined;
33+
offset: number | undefined;
34+
}
35+
3136
export interface DatasetDef<
3237
T extends NumericLikeType | ComplexType = NumericLikeType | ComplexType,
3338
> extends DatasetInfo {
@@ -38,7 +43,7 @@ type WithError<T extends DatasetDef> = T & {
3843
errorDataset?: Dataset<ArrayShape, NumericType>;
3944
};
4045

41-
export type AxisDef = DatasetDef<NumericType>;
46+
export type AxisDef = DatasetDef<NumericType> & ScalingInfo;
4247

4348
export type DefaultSlice = (number | '.')[];
4449
export interface SilxStyle {

packages/app/src/vis-packs/nexus/utils.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import {
2626
} from '@h5web/shared/hdf5-models';
2727
import { getChildEntity } from '@h5web/shared/hdf5-utils';
2828

29-
import { type AttrValuesStore } from '../../providers/models';
29+
import { type AttrValuesStore, type ValuesStore } from '../../providers/models';
3030
import { hasAttribute } from '../../utils';
3131
import {
3232
type AxisDef,
3333
type DatasetInfo,
3434
type DefaultSlice,
35+
type ScalingInfo,
3536
type SilxStyle,
3637
} from './models';
3738

@@ -130,6 +131,38 @@ export function findAuxErrorDataset(
130131
return dataset;
131132
}
132133

134+
export function findScalingFactor(
135+
group: GroupWithChildren,
136+
datasetName: string,
137+
): Dataset<ScalarShape, NumericType> | undefined {
138+
const dataset = getChildEntity(group, `${datasetName}_scaling_factor`);
139+
140+
if (!dataset) {
141+
return undefined;
142+
}
143+
144+
assertDataset(dataset);
145+
assertScalarShape(dataset);
146+
assertNumericType(dataset);
147+
return dataset;
148+
}
149+
150+
export function findOffset(
151+
group: GroupWithChildren,
152+
datasetName: string,
153+
): Dataset<ScalarShape, NumericType> | undefined {
154+
const dataset = getChildEntity(group, `${datasetName}_offset`);
155+
156+
if (!dataset) {
157+
return undefined;
158+
}
159+
160+
assertDataset(dataset);
161+
assertScalarShape(dataset);
162+
assertNumericType(dataset);
163+
return dataset;
164+
}
165+
133166
export function findAssociatedDatasets(
134167
group: GroupWithChildren,
135168
type: 'axes' | 'auxiliary_signals',
@@ -312,6 +345,27 @@ export function getSilxStyle(
312345
}
313346
}
314347

348+
export function getScalingInfo(
349+
group: GroupWithChildren,
350+
datasetName: string,
351+
valuesStore: ValuesStore,
352+
): ScalingInfo {
353+
const scalingFactorDataset = findScalingFactor(group, datasetName);
354+
const scalingFactor = scalingFactorDataset
355+
? Number(valuesStore.get({ dataset: scalingFactorDataset }))
356+
: undefined;
357+
358+
const offsetDataset = findOffset(group, datasetName);
359+
const offset = offsetDataset
360+
? Number(valuesStore.get({ dataset: offsetDataset }))
361+
: undefined;
362+
363+
return {
364+
scalingFactor,
365+
offset,
366+
};
367+
}
368+
315369
export function getDatasetInfo(
316370
dataset: Dataset,
317371
attrValuesStore: AttrValuesStore,

0 commit comments

Comments
 (0)