Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ Like with [Plotly](https://plotly.com/javascript/):
- **Plotting area:** drag to zoom (along one axis if the drag is mostly horizontal or vertical), Shift+drag to pan, use the mouse wheel to zoom around the mouse pointer, and double-click to reset the zoom.
- **Middle of an axis:** drag to pan along that axis, use the mouse wheel to zoom along it, and double-click to reset its zoom.
- **Ends of an axis and bottom corners:** drag to move an end of the axis, or click an end of the axis to type its value.
- **Legend:** click a trace to show or hide it, double-click it to only show that trace (or all the traces again), and hover it to highlight its trace. Its context menu (right-click, or Shift+F10 once it has the focus) can also show only that trace (or all the traces again).
- **Legend:** click a trace to show or hide it, double-click it to only show that trace (or all the traces again), and hover it to highlight its trace. Its context menu (right-click, or Shift+F10 once it has the focus) can also show only that trace (or all the traces again), or show or hide it if it is the only trace.
- **Touch:** drag with one finger as with a mouse, or use two fingers to zoom and pan.
- **Keyboard** (once the plot has the focus): `+` and `−` to zoom in and out, `0` to reset the zoom, the arrow keys to pan, and Shift+F10 (or the context menu key) to open the context menu (e.g., to type the value of an end of an axis). Each legend item can also get the focus: Enter or Space shows or hides its trace, and Shift+F10 opens its context menu.
- **Escape**, or pressing another mouse button, cancels a drag.

Hovering shows the value of each visible trace at the X value under the mouse pointer. For a trace that is not a function of X (e.g., a phase plot), it shows the point closest to the mouse pointer instead.

The context menu can zoom in and out, reset the zoom, edit the range of an axis, make an axis logarithmic, copy the plot to the clipboard, export it as JPEG, PNG, SVG, or WebP, and export its data to CSV. When requested from a legend item, it can also show only that trace (or all the traces again).
The context menu can zoom in and out, reset the zoom, edit the range of an axis, make an axis logarithmic, copy the plot to the clipboard, export it as JPEG, PNG, SVG, or WebP, and export its data to CSV. When requested from a legend item, it can also show only that trace (or all the traces again), or show or hide it if it is the only trace.

Copied and exported images always use the light colours (white background, dark text), whatever the current theme, since they are typically used in light documents.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"type": "module",
"types": "dist/index.d.ts",
"version": "0.20261125.3",
"version": "0.20261125.4",
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down
175 changes: 175 additions & 0 deletions src/common/traceVisibilityUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { describe, expect, it } from 'bun:test';

import {
isolatedTraceVisibilities,
isSoleVisibleTrace,
legendTraceAction,
toggledTraceVisibilities
} from './traceVisibilityUtils';

// toggledTraceVisibilities().

describe('toggledTraceVisibilities()', () => {
it('toggles the visibility of the given trace, without changing the given visibilities', () => {
const visibilities = [true, false, true];

expect(toggledTraceVisibilities(visibilities, 0)).toEqual([false, false, true]);
expect(toggledTraceVisibilities(visibilities, 1)).toEqual([true, true, true]);
expect(visibilities).toEqual([true, false, true]);
});

it('considers a trace with no visibility as visible', () => {
expect(toggledTraceVisibilities([], 0)).toEqual([false]);
});
});

// isSoleVisibleTrace().

describe('isSoleVisibleTrace()', () => {
it('returns whether the given trace is the only visible one', () => {
expect(isSoleVisibleTrace([false, true, false], 1)).toBe(true);
expect(isSoleVisibleTrace([false, true, true], 1)).toBe(false);
expect(isSoleVisibleTrace([false, false, false], 1)).toBe(false);
expect(isSoleVisibleTrace([true], 0)).toBe(true);
expect(isSoleVisibleTrace([false], 0)).toBe(false);
});
});

// isolatedTraceVisibilities().

describe('isolatedTraceVisibilities()', () => {
it('isolates the given trace or, if it is already the only visible one, shows all the traces again', () => {
expect(isolatedTraceVisibilities([true, true, true], 1)).toEqual([false, true, false]);
expect(isolatedTraceVisibilities([true, false, false], 1)).toEqual([false, true, false]);
expect(isolatedTraceVisibilities([false, true, false], 1)).toEqual([true, true, true]);
});
});

// legendTraceAction().

describe('legendTraceAction()', () => {
describe('with one trace', () => {
it('shows or hides the trace', () => {
expect(legendTraceAction([true], 1, 0)).toEqual({ label: 'Hide Trace', visibilities: [false] });
expect(legendTraceAction([false], 1, 0)).toEqual({ label: 'Show Trace', visibilities: [true] });
});

it('hides the trace when it has no visibility yet', () => {
expect(legendTraceAction([], 1, 0)).toEqual({ label: 'Hide Trace', visibilities: [false] });
});
});

describe('with several traces', () => {
it('isolates the trace when all the traces are visible', () => {
expect(legendTraceAction([true, true], 2, 0)).toEqual({
label: 'Show Only This Trace',
visibilities: [true, false]
});
expect(legendTraceAction([true, true], 2, 1)).toEqual({
label: 'Show Only This Trace',
visibilities: [false, true]
});
expect(legendTraceAction([true, true, true], 3, 0)).toEqual({
label: 'Show Only This Trace',
visibilities: [true, false, false]
});
expect(legendTraceAction([true, true, true], 3, 1)).toEqual({
label: 'Show Only This Trace',
visibilities: [false, true, false]
});
expect(legendTraceAction([true, true, true], 3, 2)).toEqual({
label: 'Show Only This Trace',
visibilities: [false, false, true]
});
});

it('shows all the traces again when the trace is the only visible one', () => {
expect(legendTraceAction([true, false], 2, 0)).toEqual({
label: 'Show All Traces',
visibilities: [true, true]
});
expect(legendTraceAction([false, true], 2, 1)).toEqual({
label: 'Show All Traces',
visibilities: [true, true]
});
expect(legendTraceAction([false, true, false], 3, 1)).toEqual({
label: 'Show All Traces',
visibilities: [true, true, true]
});
expect(legendTraceAction([false, false, true], 3, 2)).toEqual({
label: 'Show All Traces',
visibilities: [true, true, true]
});
});

it('isolates the trace when it is hidden, even if another trace is the only visible one', () => {
expect(legendTraceAction([false, true], 2, 0)).toEqual({
label: 'Show Only This Trace',
visibilities: [true, false]
});
expect(legendTraceAction([true, false, false], 3, 2)).toEqual({
label: 'Show Only This Trace',
visibilities: [false, false, true]
});
});

it('isolates the trace when all the traces are hidden', () => {
expect(legendTraceAction([false, false], 2, 1)).toEqual({
label: 'Show Only This Trace',
visibilities: [false, true]
});
expect(legendTraceAction([false, false, false], 3, 0)).toEqual({
label: 'Show Only This Trace',
visibilities: [true, false, false]
});
});

it('isolates the trace when some other traces are hidden, but not all of them', () => {
expect(legendTraceAction([true, true, false], 3, 0)).toEqual({
label: 'Show Only This Trace',
visibilities: [true, false, false]
});
expect(legendTraceAction([false, true, true, false], 4, 2)).toEqual({
label: 'Show Only This Trace',
visibilities: [false, false, true, false]
});
});

it('isolates the trace or shows all the traces again, for any visibilities of 2 to 6 traces', () => {
// Check all the possible visibilities of our traces, for each of our traces, making sure that the given
// visibilities don't get changed.

for (let nbOfTraces = 2; nbOfTraces <= 6; ++nbOfTraces) {
for (let bits = 0; bits < 1 << nbOfTraces; ++bits) {
const visibilities = Array.from(
{ length: nbOfTraces },
(_value: unknown, i: number) => (bits & (1 << i)) !== 0
);
const visibilitiesCopy = visibilities.slice();

for (let index = 0; index < nbOfTraces; ++index) {
const isSoleVisible = visibilities.every((visible: boolean, i: number) => visible === (i === index));
const action = legendTraceAction(visibilities, nbOfTraces, index);

expect(action.label).toBe(isSoleVisible ? 'Show All Traces' : 'Show Only This Trace');
expect(action.visibilities).toEqual(
visibilities.map((_visible: boolean, i: number) => isSoleVisible || i === index)
);
expect(visibilities).toEqual(visibilitiesCopy);
}
}
}
});
});

it('depends on the number of traces, even when the trace is the only visible one', () => {
// A visible trace that is the only trace can only be hidden, while a visible trace that is the only visible one
// among several traces gets all the traces shown again.

expect(legendTraceAction([true], 1, 0)).toEqual({ label: 'Hide Trace', visibilities: [false] });
expect(legendTraceAction([true, false], 2, 0)).toEqual({
label: 'Show All Traces',
visibilities: [true, true]
});
});
});
54 changes: 54 additions & 0 deletions src/common/traceVisibilityUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// The visibility of our traces, as changed through our legend (see setTraceVisibilities() in PlotWidget.vue).
// Note: a trace with no visibility (e.g., a trace that was just added) is visible.

export const toggledTraceVisibilities = (visibilities: boolean[], index: number): boolean[] => {
// Return the given visibilities, with the visibility of the given trace toggled.

const res = visibilities.slice();

res[index] = !(res[index] ?? true);

return res;
};

export const isSoleVisibleTrace = (visibilities: boolean[], index: number): boolean => {
// Return whether the given trace is the only visible one, given the visibilities of our traces.

return visibilities.every((visible: boolean, i: number) => visible === (i === index));
};

export const isolatedTraceVisibilities = (visibilities: boolean[], index: number): boolean[] => {
// Return the given visibilities, with the given trace isolated (i.e. with all the other traces hidden) or, if it is
// already the only visible trace, with all our traces shown again.

const isSoleVisible = isSoleVisibleTrace(visibilities, index);

return visibilities.map((_visible: boolean, i: number) => isSoleVisible || i === index);
};

export interface LegendTraceAction {
/** The label of the action. */
label: string;
/** The visibilities of our traces once the action has been chosen. */
visibilities: boolean[];
}

export const legendTraceAction = (visibilities: boolean[], nbOfTraces: number, index: number): LegendTraceAction => {
// Return the action that the context menu of the given legend item starts with, i.e. to isolate its trace (or to show
// all our traces again) or, if there is only one trace (in which case isolating it makes no sense), to show or hide
// it.

if (nbOfTraces === 1) {
const visible = visibilities[index] ?? true;

return {
label: visible ? 'Hide Trace' : 'Show Trace',
visibilities: toggledTraceVisibilities(visibilities, index)
};
}

return {
label: isSoleVisibleTrace(visibilities, index) ? 'Show All Traces' : 'Show Only This Trace',
visibilities: isolatedTraceVisibilities(visibilities, index)
};
};
65 changes: 27 additions & 38 deletions src/widgets/PlotWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ import {
logValues,
valuesStatistics
} from '../common/traceValues';
import { isolatedTraceVisibilities, legendTraceAction, toggledTraceVisibilities } from '../common/traceVisibilityUtils';

import '../assets/theme.css';

Expand Down Expand Up @@ -1084,19 +1085,12 @@ const onLegendClick = (event: MouseEvent, index: number): void => {
return;
}

// Use a copy of our visibilities since traceVisibilities is a shallow ref, i.e. we need to assign it a new array for
// our change to be picked up.

const visibilities = traceVisibilities.value.slice();

lastLegendToggle = {
index,
previousVisibility: visibilities[index] ?? true
previousVisibility: traceVisibilities.value[index] ?? true
};

visibilities[index] = !lastLegendToggle.previousVisibility;

setTraceVisibilities(visibilities);
setTraceVisibilities(toggledTraceVisibilities(traceVisibilities.value, index));
};

const onLegendDoubleClick = (index: number): void => {
Expand All @@ -1110,30 +1104,16 @@ const onLegendDoubleClick = (index: number): void => {

lastLegendToggle = undefined;

isolateTrace(
index,
traceVisibilities.value.map((visible: boolean, i: number) =>
i === index && firstClickToggle ? firstClickToggle.previousVisibility : visible
setTraceVisibilities(
isolatedTraceVisibilities(
traceVisibilities.value.map((visible: boolean, i: number) =>
i === index && firstClickToggle ? firstClickToggle.previousVisibility : visible
),
index
)
);
};

const isSoleVisibleTrace = (index: number, visibilities: boolean[] = traceVisibilities.value): boolean => {
// Return whether the given trace is the only visible one, given the visibilities of our traces.

return visibilities.every((visible: boolean, i: number) => visible === (i === index));
};

const isolateTrace = (index: number, visibilities: boolean[] = traceVisibilities.value): void => {
// Isolate the given trace (i.e. hide all the other traces) or, if it is already the only visible trace (given the
// visibilities of our traces), show all our traces again (e.g., when double-clicking a legend item, or using our
// context menu when it is requested from a legend item).

const isSoleVisible = isSoleVisibleTrace(index, visibilities);

setTraceVisibilities(visibilities.map((_visible: boolean, i: number) => isSoleVisible || i === index));
};

const onLegendKeyDown = (event: KeyboardEvent, index: number): void => {
// Show our context menu for the given legend item (so that, for instance, its trace can be isolated using the
// keyboard) when Shift+F10 (or the context menu key) is pressed.
Expand All @@ -1151,7 +1131,9 @@ const onLegendKeyDown = (event: KeyboardEvent, index: number): void => {
// Context menu.

// Note #1: when requested from a legend item, our context menu starts with an item to isolate the trace of that legend
// item (or to show all our traces again), which is also how a trace can be isolated using the keyboard.
// item (or to show all our traces again), which is also how a trace can be isolated using the keyboard. If
// there is only one trace, then isolating it makes no sense, so that item shows or hides that trace instead
// (see legendTraceAction()).
// Note #2: our range editor is opened once our context menu has been hidden (and has given the keyboard focus back), so
// that it gets (and keeps) the keyboard focus, since it gets closed when it loses it.

Expand All @@ -1163,18 +1145,25 @@ const openRangeEditorLater = (type: AxisEnd): void => {
});
};

const legendTraceMenuItem = (index: number): ContextMenuItem => {
// Return the item that our context menu starts with when it is requested from the given legend item.

const action = legendTraceAction(traceVisibilities.value, props.data.traces.length, index);

return {
icon: 'eye',
label: action.label,
command: () => {
setTraceVisibilities(action.visibilities);
}
Comment thread
agarny marked this conversation as resolved.
Resolved
};
};

const contextMenuItems = vue.computed<ContextMenuItem[]>(() => [
...(contextMenuLegendIndex.value === null
? []
: [
{
icon: 'eye' as const,
label: isSoleVisibleTrace(contextMenuLegendIndex.value) ? 'Show All Traces' : 'Show Only This Trace',
command: (
(index: number) => () =>
isolateTrace(index)
)(contextMenuLegendIndex.value)
},
legendTraceMenuItem(contextMenuLegendIndex.value),
{
separator: true as const
}
Expand Down