diff --git a/README.md b/README.md index c0dfac2..cc81ba3 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Contents: - [Types](#types) - [Data](#data) - [Large and live data](#large-and-live-data) + - [Several plots](#several-plots) - [Props](#props) - [Events](#events) - [Methods](#methods) @@ -73,8 +74,10 @@ The component is the default export of `@opencor/plot`, and is also exported as |---|---|---| | `PlotData` | `{ traces, xAxisTitle?, yAxisTitle?, xAxisRange?, yAxisRange? }` | The data to plot (see [Data](#data)). | | `PlotTrace` | `{ name, x, y, xValue, yValue, color, traceId?, legendRank?, runLabel? }` | A trace to plot (see [Data](#data)). | -| `PlotMargins` | `{ left: number; right: number }` | The left and right margins (in pixels) of a plot, i.e. the space taken by its Y axis and legend (see the [`margins`](#props) prop and the [`marginsUpdated`](#events) event). | | `PlotTheme` | `'system' \| 'light' \| 'dark'` | The value of the [`theme`](#props) prop. | +| `PlotGroup` | `string \| symbol` | A group of plots, i.e. the value of the [`xAxisAlign`](#props), [`xAxisSync`](#props), [`yAxisAlign`](#props), and [`yAxisSync`](#props) props. A symbol is unique, so that unrelated parts of a page can't end up in the same group by using the same string. | +| `PlotRanges` | `{ x: [number, number]; y: [number, number]; zoomed: { x: boolean; y: boolean } }` | The payload of the [`rangesUpdated`](#events) event, i.e. the current ranges of the axes (in actual units, even along a logarithmic axis) and whether each axis is zoomed (rather than autoranged). To restore them later, give `{ x: zoomed.x ? x : null, y: zoomed.y ? y : null }` to [`setRanges()`](#methods), so that an autoranged axis stays autoranged. | +| `PlotRangesToSet` | `{ x?: [number, number] \| null; y?: [number, number] \| null }` | The ranges to give to [`setRanges()`](#methods), i.e. the range (in actual units, even along a logarithmic axis) into which to zoom an axis, `null` to reset the zoom of an axis, or nothing to leave an axis as it is. | | `PlotCsvExportEvent` | `PlotCsvExportStartedEvent \| PlotCsvExportProgressEvent \| PlotCsvExportFinishedEvent` | The payload of the [`csvExport`](#events) event. Use its `type` to tell which one it is. | | `PlotCsvExportStartedEvent` | `{ type: 'started' }` | An export to CSV has started. | | `PlotCsvExportProgressEvent` | `{ type: 'progress'; percent: number }` | An export to CSV has made some progress: `percent` goes from 0 to 100 (an integer). | @@ -138,6 +141,76 @@ data.value = { Also set the [`live`](#props) prop while the data is live, so that the last point of each visible trace gets marked, making its latest value easy to spot (even where a trace overlaps itself, e.g. in a phase plot). +### 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. +- **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. + +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. + +For example, to stack a plot of the membrane potential of a cell and a plot of its membrane current, both against time, with their plotting areas lined up and, unless the user unchecks a checkbox, zooming or panning the time axis of one plot doing the same to the other: + +```vue + + + +``` + +Here, the plotting areas line up even though the Y axis of the current plot needs more room for its tick labels (e.g., `-2000` rather than `-80`). Both plots show the same 10 seconds, and hovering one of them also shows the values of the other at the same time. + +A symbol is unique, so that unrelated parts of a page can't end up in the same group by using the same string. A plot joins or leaves a group whenever its props change (e.g., to let the user choose whether some plots are synchronised), and leaves its groups when it is unmounted. A plot that joins a group with a synchronised axis takes on the range and scale of that axis. + +To keep track of the ranges of the axes of a plot (e.g., to restore them later), handle its [`rangesUpdated`](#events) event. + ### Props | Prop | Type | Default | Description | @@ -145,7 +218,10 @@ Also set the [`live`](#props) prop while the data is live, so that the last poin | `data` | [`PlotData`](#types) | (required) | The data to plot. | | `showLegend` | `boolean` | `true` | Whether to show the legend. The legend takes at most 40% of the width of the plot (with long trace names truncated, and shown in full when hovered or in the context menu of their legend item), and is hidden if the plot is too narrow for it to be useful. | | `theme` | [`PlotTheme`](#types) | `'system'` | Whether to use light or dark mode, or whatever the system uses. | -| `margins` | [`PlotMargins`](#types) | | Margins shared with other plots, so that their plotting areas line up (see [`marginsUpdated`](#events)). | +| `xAxisAlign` | [`PlotGroup`](#types) | | The group of plots whose X axes are to line up with this plot's, i.e. whose plotting areas are to have the same left and right edges (e.g., when they are stacked). | +| `xAxisSync` | [`PlotGroup`](#types) | | The group of plots whose X axis is to be synchronised with this plot's, i.e. which share the range and the scale of their X axis, so that zooming, panning, or resetting the zoom of one of them does the same to the others, as does making its X axis logarithmic (or not). Their X axes always show the same range, which, until one of them gets zoomed or panned, fits the data of all of them. Hovering one of them also shows the hover guide and tooltip of the others at the same X value (next to their guide line, at the same relative height as the mouse pointer or finger), so that the values of all of them can be read at once. For a trace whose X values are not sorted (e.g., a phase plot), the others show the point that is closest to where the mouse pointer would be, i.e. at their guide line and at that relative height, if any is close enough to it. | +| `yAxisAlign` | [`PlotGroup`](#types) | | The group of plots whose Y axes are to line up with this plot's, i.e. whose plotting areas are to have the same top and bottom edges (e.g., when they are side by side). | +| `yAxisSync` | [`PlotGroup`](#types) | | The group of plots whose Y axis is to be synchronised with this plot's, i.e. which share the range and the scale of their Y axis, so that zooming, panning, or resetting the zoom of one of them does the same to the others, as does making its Y axis logarithmic (or not). Their Y axes always show the same range, which, until one of them gets zoomed or panned, fits the data of all of them. | | `appendTo` | `HTMLElement \| string` | `body` | Where to append our overlays (context menu, hover tooltip, etc.). | | `customContextMenu` | `boolean` | `false` | Whether to emit a [`contextMenu`](#events) event rather than show our own context menu. To have no context menu at all, set it and don't handle the `contextMenu` event. | | `levelOfDetail` | `boolean` | `true` | Whether to only go through the points that are needed for a trace to look the same when drawing it (see [Large and live data](#large-and-live-data)). Hovering a trace whose X values are sorted always does. Only meant to be disabled to see how much it helps. | @@ -157,9 +233,9 @@ To draw traces without our level of detail, use `:levelOfDetail="false"`. Our pa | Event | Payload | Description | |---|---|---| -| `marginsUpdated` | [`PlotMargins`](#types) | The margins that the plot needs have changed. To line up several plots, pass the largest left and right margins of all of them to each plot's [`margins`](#props) prop. | | `csvExport` | [`PlotCsvExportEvent`](#types) | An export to CSV has `'started'`, made `'progress'` (with a `percent`), or `'finished'` (with whether it was a `success`), so that its progress can be shown. | | `imageExport` | [`PlotImageExportEvent`](#types) | The plot has been copied to the clipboard or exported to an image (or has failed to be), so that the user can be told about it. | +| `rangesUpdated` | [`PlotRanges`](#types) | The range of an axis has changed, or whether it is zoomed has. It is emitted at most once per frame, including while the plot is being zoomed, panned, or animated, and while its data gets updated. | | `contextMenu` | [`PlotContextMenuEvent`](#types) | Only with [`customContextMenu`](#props): the context menu was requested at `clientX`/`clientY`, with the `items` that our context menu would have shown. If `usingKeyboard` is true, the custom menu should take the keyboard focus. The custom menu should call the `command` of the chosen item while handling the click (or tap, or key press) that chose it, and it shouldn't move the keyboard focus afterwards (calling [`contextMenuHidden()`](#methods) instead), since some commands (e.g., to edit the range of an axis) give the keyboard focus to an editor that closes when it loses it, and iOS only shows its virtual keyboard for such an editor while handling a user interaction. If it calls that `command` while handling a key press (e.g., Enter or Space), it should also prevent that key's default action (using `preventDefault()`), since the key would otherwise also be typed into such an editor, replacing its selected value. | ### Methods @@ -168,8 +244,8 @@ Available through a template ref: | Method | Description | |---|---| -| `resetMargins()` | Emit [`marginsUpdated`](#events) again, e.g. after the shared margins have been reset. | | `contextMenuHidden()` | Tell the plot that a custom context menu (see [`contextMenu`](#events)) has been hidden, so that it can get the keyboard focus back (unless something else, e.g. an editor opened by the chosen item, has it). | +| `setRanges(ranges, options?)` | Zoom the axes into the given ranges and/or reset their zoom (see [`PlotRangesToSet`](#types)), as if the user had done it (so, a synchronised axis gets updated in all the plots of its group), animating the change if `options.animate` is set (`false` by default). A range that can't be plotted (e.g., with a non-positive end along a logarithmic axis, or with two ends that are the same) is ignored. | | `pointCounts()` | The number of points of the visible traces (`nbOfPoints`), and how many of them were last drawn (`nbOfDrawnPoints`), as a [`PlotPointCounts`](#types). Meant for testing and benchmarking. | | `memoryUsage()` | The memory (in bytes) used by the component itself, not counting the data, as a [`PlotMemoryUsage`](#types): by its levels of detail (`levelOfDetail`) and by the logarithm of its values along logarithmic axes (`logValues`). Meant for testing and benchmarking. | @@ -186,6 +262,8 @@ Like with [Plotly](https://plotly.com/javascript/): 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. +With synchronised axes (see [Several plots](#several-plots)), zooming, panning, resetting the zoom, editing the range of an axis, and making an axis logarithmic (or not) apply to all the plots of the group of that axis, be it done with the mouse, by touch, with the keyboard, or from the context menu. + 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. diff --git a/demo/App.vue b/demo/App.vue index ed384d6..cef9fbd 100644 --- a/demo/App.vue +++ b/demo/App.vue @@ -9,10 +9,13 @@ :live="liveRunning" :showLegend="showLegend" :customContextMenu="customContextMenu" - :margins="sharedMargins ? margins : undefined" + :xAxisAlign="alignedXAxes ? ALIGN_GROUP : undefined" + :xAxisSync="linkedXAxes ? SYNC_GROUP : undefined" + :yAxisAlign="alignedYAxes ? ALIGN_GROUP : undefined" + :yAxisSync="linkedYAxes ? SYNC_GROUP : undefined" @contextMenu="(event) => onContextMenu(event, index)" @csvExport="onCsvExport" - @marginsUpdated="(newMargins) => onMarginsUpdated(newMargins, index)" + @rangesUpdated="(ranges) => onRangesUpdated(ranges, index)" /> @@ -113,8 +116,20 @@ + + +