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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ The component is the default export of `@opencor/plot`, and is also exported as

| Type | Definition | Description |
|---|---|---|
| `PlotData` | `{ traces, xAxis?, yAxis? }` | The data to plot (see [Data](#data)). |
| `PlotData` | `{ traces, title?, titlePosition?, xAxis?, yAxis? }` | The data to plot (see [Data](#data)). |
| `PlotTitlePosition` | `'top' \| 'left' \| 'right' \| 'bottom'` | Where the title of the plot is (see [Data](#data)). |
| `PlotAxis` | `{ title?, range?, grid?, log? }` | An axis of the plot (see [Data](#data)). |
| `PlotTrace` | `{ name, x, y, xValue, yValue, line?, marker?, traceId?, legendRank?, runLabel? }` | A trace to plot (see [Data](#data)). |
| `PlotLineStyle` | `{ type?, color?, thickness? }` | The style of the line of a trace (see [Data](#data)). |
Expand Down Expand Up @@ -109,6 +110,8 @@ The component is the default export of `@opencor/plot`, and is also exported as
| Property | Type | Description |
|---|---|---|
| `traces` | [`PlotTrace`](#types)`[]` | The traces to plot. |
| `title` | `string` | The title of the plot. |
| `titlePosition` | [`PlotTitlePosition`](#types) | Where the title of the plot is: above the plot (`'top'`, the default) or below it (`'bottom'`), centred on its plotting area, or to its left (`'left'`), centred on its plotting area and reading from bottom to top (like the title of the Y axis), or to its right (`'right'`, i.e. between its plotting area and its legend), centred on its plotting area and reading from top to bottom. |
| `xAxis`, `yAxis` | [`PlotAxis`](#types) | The axes. |

[`PlotAxis`](#types):
Expand Down Expand Up @@ -216,7 +219,7 @@ Also set the [`live`](#props) prop while the data is live, so that the last poin
### Several plots

Plots can be shown together (e.g., stacked or side by side) and made to work as one, using a [`PlotGroup`](#types), i.e. a string or a symbol, which all the plots of a group share:
- **Aligned axes:** the plots of an [`xAxisAlign`](#props) group get the same left and right margins, so that their plotting areas line up when they are stacked, while those of a [`yAxisAlign`](#props) group get the same top and bottom margins, so that their plotting areas line up when they are side by side.
- **Aligned axes:** the plots of an [`xAxisAlign`](#props) group get the same left and right margins, so that their plotting areas line up when they are stacked, while those of a [`yAxisAlign`](#props) group get the same top and bottom margins, so that their plotting areas line up when they are side by side. This is also the case if some of them have a title and others don't, or if their titles are at different positions. In that case, a plot leaves room for the title of the plots that have one on the left (or right), so that its Y axis title (or legend) lines up with theirs.
- **Synchronised axes:** the plots of an [`xAxisSync`](#props) (or [`yAxisSync`](#props)) group share the range and the scale (i.e. linear or logarithmic) of their X (or Y) axis. Zooming, panning, or resetting the zoom of one of them, or making its axis logarithmic (or not), does the same to the others, be it done by the user or using [`setRanges()`](#methods). Until one of them gets zoomed or panned, their axes fit the data of all of them. With a synchronised X axis, hovering one of them also shows the hover guide and tooltip of the others, at the same X value (or, for a plot that has no point there, at its closest point, as when it is hovered itself).

Aligning and synchronising are independent of each other: plots can be aligned without being synchronised (e.g., when they show different X values), and synchronised without being aligned (e.g., when they are not shown next to each other). A plot can also be in different groups for its X and Y axes, and the groups of X axes are separate from those of Y axes, so the same group can be used for both.
Expand Down Expand Up @@ -390,7 +393,7 @@ Set any of these CSS variables on an ancestor of the component. Each has a light
| Variable | Used for |
|---|---|
| `--opencor-plot-background` | The background of the plot. |
| `--opencor-plot-text-color` | The axis titles, the legend, and the ring around the last point of a live trace. |
| `--opencor-plot-text-color` | The plot and axis titles, the legend, and the ring around the last point of a live trace. |
| `--opencor-plot-muted-text-color` | Tick labels, the hover guide line, hidden traces in the legend, and secondary text in overlays. |
| `--opencor-plot-primary-color` | The zoom box (and, by default, the keyboard focus ring). |
| `--opencor-plot-grid-color` | Grid lines. |
Expand Down
30 changes: 28 additions & 2 deletions demo/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<option value="stacked">Stacked</option>
</select>
</label>
<label class="demo-field">
<span>Title</span>
<select v-model="titlePosition">
<option v-for="(name, value) in TITLE_POSITIONS" :key="value" :value="value">{{ name }}</option>
</select>
</label>
<label class="demo-field">
<span>Theme</span>
<select v-model="theme">
Expand Down Expand Up @@ -277,7 +283,8 @@ import OpenCORPlot, {
type PlotMarkerType,
type PlotMemoryUsage,
type PlotRanges,
type PlotTheme
type PlotTheme,
type PlotTitlePosition
} from '../index';
import {
BENCHMARK_INTERACTIONS,
Expand Down Expand Up @@ -305,6 +312,7 @@ const nbOfInstances = vue.ref<number>(1);
const theme = vue.ref<PlotTheme | 'mixed'>('mixed');
const levelOfDetail = vue.ref<boolean>(true);
const customContextMenu = vue.ref<boolean>(false);
const titlePosition = vue.ref<PlotTitlePosition | 'none' | 'mixed'>('none'); // Where our plot titles are.
const layout = vue.ref<'grid' | 'stacked'>('grid'); // Whether our instances are in a grid or stacked (in one column).
const alignedXAxes = vue.ref<boolean>(true); // Whether the X axes of our instances line up (see ALIGN_GROUP).
const linkedXAxes = vue.ref<boolean>(true); // Whether the X axes of our instances are synchronised (see SYNC_GROUP).
Expand Down Expand Up @@ -737,14 +745,25 @@ vue.watch(nbOfInstances, (newNbOfInstances: number) => {
instanceLegends.value = instanceLegends.value.slice(0, newNbOfInstances);
});

// Our data, as given to our instances, i.e. with our axis, line, and marker options applied to it.
// Our data, as given to our instances, i.e. with our title, axis, line, and marker options applied to it.
// Note #1: we only create new data objects when our data or our options change, since giving new data to our instances
// makes them update their plot.
// Note #2: with mixed lines (or markers), each of our traces has another type of line (or marker) other than none, so
// that they can be told apart and compared.
// Note #3: with default markers, our traces have no marker style, i.e. they only have markers if they have fewer than
// 20 points, so our other marker options don't apply.
// Note #4: with mixed titles, each of our instances has its title at another position, so that we can check that their
// plotting areas still line up.

const TITLE_POSITIONS: Record<typeof titlePosition.value, string> = {
none: 'None',
top: 'Top',
left: 'Left',
right: 'Right',
bottom: 'Bottom',
mixed: 'Mixed'
};
const MIXED_TITLE_POSITIONS: PlotTitlePosition[] = ['top', 'left', 'right', 'bottom'];
const Y_RANGES: Record<typeof yRange.value, [number | null, number | null] | undefined> = {
auto: undefined,
fromZero: [0, null],
Expand Down Expand Up @@ -823,6 +842,13 @@ const plottedDatasets = vue.computed<PlotData[]>(() => {

return datasets.value.map((data, index) => ({
...data,
title: titlePosition.value === 'none' ? undefined : `Plot <i>#${index + 1}</i>`,
titlePosition:
titlePosition.value === 'none'
? undefined
: titlePosition.value === 'mixed'
? MIXED_TITLE_POSITIONS[index % MIXED_TITLE_POSITIONS.length]
: titlePosition.value,
xAxis: { ...data.xAxis, ...instanceAxesSettings(index).x },
yAxis: { ...data.yAxis, range, ...instanceAxesSettings(index).y },
traces: data.traces.map((trace, traceIndex) => ({
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type {
PlotPointCounts,
PlotRanges,
PlotRangesToSet,
PlotTitlePosition,
PlotTrace
} from './src/widgets/PlotWidget.vue';
export { default, default as OpenCORPlot } from './src/widgets/PlotWidget.vue';
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.20261204.0",
"version": "0.20261204.1",
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down
75 changes: 75 additions & 0 deletions src/common/alignUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ describe('sameMargins()', () => {
expect(sameMargins({ start: 1, end: 2 }, { start: 1, end: 3 })).toBe(false);
});

it('returns whether the outer part of some start margins is the same', () => {
expect(sameMargins({ start: 1, end: 2, outerStart: 1 }, { start: 1, end: 2, outerStart: 1 })).toBe(true);
expect(sameMargins({ start: 1, end: 2, outerStart: 0 }, { start: 1, end: 2 })).toBe(true);
expect(sameMargins({ start: 1, end: 2, outerStart: 1 }, { start: 1, end: 2 })).toBe(false);
expect(sameMargins({ start: 1, end: 2, innerEnd: 1 }, { start: 1, end: 2, innerEnd: 1 })).toBe(true);
expect(sameMargins({ start: 1, end: 2, innerEnd: 0 }, { start: 1, end: 2 })).toBe(true);
expect(sameMargins({ start: 1, end: 2, innerEnd: 1 }, { start: 1, end: 2 })).toBe(false);
});

it('returns that no margins are only the same as no margins', () => {
expect(sameMargins(undefined, undefined)).toBe(true);
expect(sameMargins({ start: 1, end: 2 }, undefined)).toBe(false);
Expand Down Expand Up @@ -102,6 +111,72 @@ describe('MarginsAligner', () => {
expect(plot3.nbOfCalls()).toBe(1);
});

it('shares the outer part of the start margins separately from the rest of them', () => {
// Our first plot has a wide Y axis, while our second plot has a narrow Y axis and a plot title on the left. So,
// both plots must leave room for a plot title on the left and a wide Y axis, so that their Y axis titles line up.

const group = Symbol('group');
const plot1 = testAligner(group, { start: 60, end: 20 });
const plot2 = testAligner(group, { start: 50, end: 10, outerStart: 24 });

for (const plot of [plot1, plot2]) {
expect(plot.aligner.sharedMargins()).toEqual({ start: 84, end: 20, outerStart: 24 });
}

// Our second plot no longer has a plot title on the left.

plot2.aligner.setNaturalMargins({ start: 26, end: 10 });

for (const plot of [plot1, plot2]) {
expect(plot.aligner.sharedMargins()).toEqual({ start: 60, end: 20 });
}

expect(plot1.nbOfCalls()).toBe(3);
expect(plot2.nbOfCalls()).toBe(2);
});

it('shares the inner part of the end margins separately from the rest of them', () => {
// Our first plot has a wide legend, while our second plot has a narrow legend and a plot title on the right
// (between its plotting area and its legend). So, both plots must leave room for a plot title on the right and for
// a wide legend, so that their legends line up.

const group = Symbol('group');
const plot1 = testAligner(group, { start: 10, end: 100 });
const plot2 = testAligner(group, { start: 10, end: 80, innerEnd: 24 });

for (const plot of [plot1, plot2]) {
expect(plot.aligner.sharedMargins()).toEqual({ start: 10, end: 124, innerEnd: 24 });
}

// Our second plot no longer has a plot title on the right.

plot2.aligner.setNaturalMargins({ start: 10, end: 56 });

for (const plot of [plot1, plot2]) {
expect(plot.aligner.sharedMargins()).toEqual({ start: 10, end: 100 });
}

expect(plot1.nbOfCalls()).toBe(3);
expect(plot2.nbOfCalls()).toBe(2);
});

it('shares the X axis part of the bottom margins separately from the rest of them', () => {
// Our first plot has an X axis without a title and a plot title at the bottom (below its X axis), while our second
// plot has an X axis with a title. So, both plots must leave room for an X axis with a title and for a plot title
// at the bottom, so that their plot titles line up.

const group = Symbol('group');
const plot1 = testAligner(group, { start: 0, end: 44, innerEnd: 20 });
const plot2 = testAligner(group, { start: 0, end: 37, innerEnd: 37 });

for (const plot of [plot1, plot2]) {
expect(plot.aligner.sharedMargins()).toEqual({ start: 0, end: 61, innerEnd: 37 });
}

expect(plot1.nbOfCalls()).toBe(2);
expect(plot2.nbOfCalls()).toBe(1);
});

it('only lets its plot know when its shared margins change', () => {
const group = Symbol('group');
const plot1 = testAligner(group, { start: 30, end: 40 });
Expand Down
50 changes: 44 additions & 6 deletions src/common/alignUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@ export type AlignAxis = 'x' | 'y';

/**
* The margins (in pixels) of a plot along an axis, i.e. on either side of its plotting area: its left and right margins
* along the X axis (i.e. the space taken by its Y axis and its legend), or its top and bottom margins along the Y axis
* (i.e. the space taken by its X axis).
* along the X axis (i.e. the space taken by its plot title, if it is on the left or right, its Y axis, and its legend),
* or its top and bottom margins along the Y axis (i.e. the space taken by its plot title, if it is at the top or
* bottom, and its X axis).
*/

export interface Margins {
/** The margin before the plotting area, i.e. its left margin along the X axis, or its top margin along the Y axis. */
start: number;
/** The margin after the plotting area, i.e. its right margin along the X axis, or its bottom one along the Y axis. */
end: number;
/**
* The outer part of the margin before the plotting area (0 by default), i.e. the part of it that is furthest from the
* plotting area (e.g., the space taken by a plot title on the left). It is shared separately from the rest of the
* margin, so that what is in the rest of the margin (e.g., the title of the Y axis) also lines up.
*/
outerStart?: number;
/**
* The inner part of the margin after the plotting area (0 by default), i.e. the part of it that is closest to the
* plotting area (e.g., the space taken by a plot title on the right, or by the X axis). It is shared separately from
* the rest of the margin, so that what is in the rest of the margin (e.g., the legend, or a plot title at the bottom)
* also lines up.
*/
innerEnd?: number;
}

export const sameMargins = (firstMargins: Margins | undefined, secondMargins: Margins | undefined): boolean => {
Expand All @@ -30,7 +44,12 @@ export const sameMargins = (firstMargins: Margins | undefined, secondMargins: Ma
return firstMargins === secondMargins;
}

return firstMargins.start === secondMargins.start && firstMargins.end === secondMargins.end;
return (
firstMargins.start === secondMargins.start &&
firstMargins.end === secondMargins.end &&
(firstMargins.outerStart ?? 0) === (secondMargins.outerStart ?? 0) &&
(firstMargins.innerEnd ?? 0) === (secondMargins.innerEnd ?? 0)
);
};

// Our groups for each axis, i.e. the aligners (i.e. plots) that are in each of them.
Expand Down Expand Up @@ -138,19 +157,38 @@ export class MarginsAligner {
private static updateMembers(members: Set<MarginsAligner>): void {
// Give the biggest natural margins of the given members to all of them, ignoring the members that don't know their
// natural margins yet (e.g., a plot that hasn't been laid out yet), and then tell the members which shared margins
// have changed about them.
// have changed about them. The outer part of our shared start margin is the biggest outer part of the natural start
// margins and the rest of it is the biggest rest of them, so that what is in the outer part of the start margin of
// one plot (e.g., its plot title) doesn't end up being where something else is in the start margin of another plot
// (e.g., its Y axis title). The same goes for the inner part of our shared end margin (e.g., a plot title on the
// right, which is between the plotting area and the legend, or the X axis, which is between the plotting area and a
// plot title at the bottom).
// Note: we first give all our members their new shared margins and only then tell them about them, in case one of
// them was to straight away join or leave a group, or change its natural margins. Indeed, this would update
// the shared margins of the members of its group, which we would otherwise, for the members that we have yet
// to tell, override with our (now outdated) shared margins.

let margins: Margins | undefined;
let innerStart = 0;
let outerStart = 0;
let innerEnd = 0;
let outerEnd = 0;

for (const member of members) {
if (member.natural) {
const memberOuterStart = member.natural.outerStart ?? 0;
const memberInnerEnd = member.natural.innerEnd ?? 0;

innerStart = Math.max(innerStart, member.natural.start - memberOuterStart);
outerStart = Math.max(outerStart, memberOuterStart);
innerEnd = Math.max(innerEnd, memberInnerEnd);
outerEnd = Math.max(outerEnd, member.natural.end - memberInnerEnd);

margins = {
start: Math.max(margins?.start ?? 0, member.natural.start),
end: Math.max(margins?.end ?? 0, member.natural.end)
start: innerStart + outerStart,
end: innerEnd + outerEnd,
...(outerStart ? { outerStart } : {}),
...(innerEnd ? { innerEnd } : {})
};
}
}
Expand Down
Loading