Skip to content

Commit

Permalink
Show NX axis labels as hints in axis mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed May 23, 2023
1 parent 67c1e83 commit 360949e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 10 deletions.
14 changes: 10 additions & 4 deletions packages/app/src/dimension-mapper/AxisMapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ToggleGroup } from '@h5web/lib';
import type { Axis } from '@h5web/shared';
import type { Axis, AxisMapping } from '@h5web/shared';
import { isNumber } from 'lodash';

import styles from './DimensionMapper.module.css';
Expand All @@ -8,12 +8,13 @@ import type { DimensionMapping } from './models';
interface Props {
axis: Axis;
rawDims: number[];
axisLabels: AxisMapping<string> | undefined;
mapperState: DimensionMapping;
onChange: (mapperState: DimensionMapping) => void;
}

function AxisMapper(props: Props) {
const { axis, rawDims, mapperState, onChange } = props;
const { axis, rawDims, axisLabels, mapperState, onChange } = props;
const selectedDim = mapperState.indexOf(axis);

if (selectedDim === -1) {
Expand Down Expand Up @@ -42,8 +43,13 @@ function AxisMapper(props: Props) {
}
}}
>
{Object.keys(rawDims).map((dimKey) => (
<ToggleGroup.Btn key={dimKey} label={`D${dimKey}`} value={dimKey} />
{Object.keys(rawDims).map((dimKey, index) => (
<ToggleGroup.Btn
key={dimKey}
label={`D${dimKey}`}
value={dimKey}
hint={axisLabels?.[index]}
/>
))}
</ToggleGroup>
</div>
Expand Down
6 changes: 5 additions & 1 deletion packages/app/src/dimension-mapper/DimensionMapper.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AxisMapping } from '@h5web/shared';
import { isNumber } from 'lodash';

import AxisMapper from './AxisMapper';
Expand All @@ -7,12 +8,13 @@ import SlicingSlider from './SlicingSlider';

interface Props {
rawDims: number[];
axisLabels?: AxisMapping<string>;
mapperState: DimensionMapping;
onChange: (d: DimensionMapping) => void;
}

function DimensionMapper(props: Props) {
const { rawDims, mapperState, onChange } = props;
const { rawDims, axisLabels, mapperState, onChange } = props;

return (
<div className={styles.mapper}>
Expand All @@ -32,12 +34,14 @@ function DimensionMapper(props: Props) {
<AxisMapper
axis="x"
rawDims={rawDims}
axisLabels={axisLabels}
mapperState={mapperState}
onChange={onChange}
/>
<AxisMapper
axis="y"
rawDims={rawDims}
axisLabels={axisLabels}
mapperState={mapperState}
onChange={onChange}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function NxComplexImageContainer(props: VisContainerProps) {

const { shape: dims } = signalDef.dataset;
const [dimMapping, setDimMapping] = useDimMappingState(dims, 2);

const axisLabels = axisDefs.map((def) => def?.label);
const xAxisDef = axisDefs[dimMapping.indexOf('x')];
const yAxisDef = axisDefs[dimMapping.indexOf('y')];

Expand All @@ -37,6 +39,7 @@ function NxComplexImageContainer(props: VisContainerProps) {
<>
<DimensionMapper
rawDims={dims}
axisLabels={axisLabels}
mapperState={dimMapping}
onChange={setDimMapping}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ function NxComplexSpectrumContainer(props: VisContainerProps) {

const nxData = useNxData(entity);
assertComplexNxData(nxData);

const { signalDef, axisDefs, silxStyle } = nxData;
const signalDims = signalDef.dataset.shape;

const [dimMapping, setDimMapping] = useDimMappingState(signalDims, 1);

const axisLabels = axisDefs.map((def) => def?.label);
const xDimIndex = dimMapping.indexOf('x');

const config = useComplexLineConfig();
Expand All @@ -38,6 +41,7 @@ function NxComplexSpectrumContainer(props: VisContainerProps) {
<>
<DimensionMapper
rawDims={signalDims}
axisLabels={axisLabels}
mapperState={dimMapping}
onChange={setDimMapping}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function NxImageContainer(props: VisContainerProps) {

const { shape: dims } = signalDef.dataset;
const [dimMapping, setDimMapping] = useDimMappingState(dims, 2);

const axisLabels = axisDefs.map((def) => def?.label);
const xAxisDef = axisDefs[dimMapping.indexOf('x')];
const yAxisDef = axisDefs[dimMapping.indexOf('y')];

Expand All @@ -35,6 +37,7 @@ function NxImageContainer(props: VisContainerProps) {
<>
<DimensionMapper
rawDims={dims}
axisLabels={axisLabels}
mapperState={dimMapping}
onChange={setDimMapping}
/>
Expand All @@ -50,7 +53,7 @@ function NxImageContainer(props: VisContainerProps) {
dataset={signalDef.dataset}
value={signal}
dimMapping={dimMapping}
axisLabels={axisDefs.map((def) => def?.label)}
axisLabels={axisLabels}
axisValues={axisValues}
title={title}
toolbarContainer={toolbarContainer}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ function NxRgbContainer(props: VisContainerProps) {

const mappableDims = dims.slice(0, -1);
const [dimMapping, setDimMapping] = useDimMappingState(mappableDims, 2);

const axisLabels = axisDefs.map((def) => def?.label);
const config = useRgbConfig();

return (
<>
<DimensionMapper
rawDims={mappableDims}
axisLabels={axisLabels}
mapperState={dimMapping}
onChange={setDimMapping}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function NxSpectrumContainer(props: VisContainerProps) {
}

const [dimMapping, setDimMapping] = useDimMappingState(signalDims, 1);

const axisLabels = axisDefs.map((def) => def?.label);
const xDimIndex = dimMapping.indexOf('x');

const config = useLineConfig({
Expand All @@ -45,6 +47,7 @@ function NxSpectrumContainer(props: VisContainerProps) {
<>
<DimensionMapper
rawDims={signalDims}
axisLabels={axisLabels}
mapperState={dimMapping}
onChange={setDimMapping}
/>
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/toolbar/controls/ToggleGroup.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
padding: 0 !important; /* FIX style ordering issue with Vite */
}

.btn[data-hint] {
text-decoration: underline dotted;
}

.btnLike {
composes: btnLike from '../Toolbar.module.css';
}
Expand Down
15 changes: 11 additions & 4 deletions packages/lib/src/toolbar/controls/ToggleGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { ComponentType, ReactElement, SVGAttributes } from 'react';
import type {
ComponentType,
HTMLAttributes,
ReactElement,
SVGAttributes,
} from 'react';
import { createContext, useContext } from 'react';

import styles from './ToggleGroup.module.css';
Expand All @@ -24,16 +29,17 @@ function useToggleGroupProps(): ToggleGroupProps {
return context;
}

interface BtnProps {
interface BtnProps extends HTMLAttributes<HTMLButtonElement> {
label: string;
value: string;
icon?: ComponentType<SVGAttributes<SVGElement>>;
iconOnly?: boolean;
hint?: string;
disabled?: boolean;
}

function Btn(props: BtnProps) {
const { label, value, icon: Icon, iconOnly, disabled = false } = props;
const { label, value, icon: Icon, hint, iconOnly, disabled = false } = props;
const {
role,
value: selectedValue,
Expand All @@ -46,9 +52,10 @@ function Btn(props: BtnProps) {
disabled={disabled || isGroupDisabled}
className={styles.btn}
type="button"
title={iconOnly ? label : undefined}
title={hint || (iconOnly ? label : undefined)}
role={role === 'tablist' ? 'tab' : 'radio'}
data-raised
data-hint={hint || undefined}
aria-label={iconOnly ? label : undefined}
aria-checked={value === selectedValue}
onClick={() => {
Expand Down

0 comments on commit 360949e

Please sign in to comment.