Skip to content
Closed
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ The component is the default export of `@opencor/plot`, and is also exported as
| Type | Definition | Description |
|---|---|---|
| `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)). |
| `PlotTrace` | `{ name, x, y, xValue, yValue, color, lineStyle?, lineWidth?, traceId?, legendRank?, runLabel? }` | A trace to plot (see [Data](#data)). |
| `PlotLineStyle` | `'solid' \| 'dash' \| 'dot' \| 'dashDot' \| 'dashDotDot'` | The style of the line of a trace, i.e. one of the line types of SED-ML's `Line` (except `none`). |
| `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. |
| `PlotCsvExportEvent` | `PlotCsvExportStartedEvent \| PlotCsvExportProgressEvent \| PlotCsvExportFinishedEvent` | The payload of the [`csvExport`](#events) event. Use its `type` to tell which one it is. |
Expand Down Expand Up @@ -106,6 +107,8 @@ The component is the default export of `@opencor/plot`, and is also exported as
| `x`, `y` | `Float64Array` | The X and Y values of the trace. |
| `xValue`, `yValue` | `string` | What the X and Y values are (e.g., the name of a model variable), used in the CSV export. |
| `color` | `string` | The colour of the trace. |
| `lineStyle` | [`PlotLineStyle`](#types) | The optional style of the line of the trace (`'solid'` by default). Like Plotly, its dashes and gaps scale with the width of the line, so that a thick line looks right. |
| `lineWidth` | `number` | The optional width of the line of the trace (in pixels, 2 by default). |
| `traceId` | `string` | An optional identifier that keeps the visibility of the trace when the data is updated. Otherwise, the trace is identified by its `name`, `xValue`, `yValue`, and `runLabel` (and, among traces that have the same ones, by their order), or by its position if it doesn't have a `name`, `xValue`, and `yValue`. |
| `legendRank` | `number` | The optional rank of the trace in the legend (lower ranks first, 1000 by default). |
| `runLabel` | `string` | An optional label that tells apart the X column of the trace from those of other runs in the CSV export. |
Expand Down
43 changes: 42 additions & 1 deletion demo/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="demo">
<main class="demo-main" :style="gridStyle">
<div v-for="(data, index) in datasets" :key="index" class="demo-plot">
<div v-for="(data, index) in styledDatasets" :key="index" class="demo-plot">
<OpenCORPlot :ref="(plot) => setPlotRef(plot, index)"
:data="data"
:theme="instanceTheme(index)"
Expand Down Expand Up @@ -125,6 +125,18 @@
<option value="mixed">Mixed</option>
</select>
</label>
<label class="demo-field">
<span>Line style</span>
<select v-model="lineStyle">
<option v-for="(name, value) in LINE_STYLES" :key="value" :value="value">{{ name }}</option>
</select>
</label>
<label class="demo-field">
<span>Line width</span>
<select v-model.number="lineWidth">
<option v-for="value in LINE_WIDTHS" :key="value" :value="value">{{ formatNumber(value) }}</option>
</select>
</label>
<label class="demo-check">
<input v-model="levelOfDetail" type="checkbox" />
Level of detail
Expand Down Expand Up @@ -166,6 +178,7 @@ import OpenCORPlot, {
type PlotContextMenuEvent,
type PlotCsvExportEvent,
type PlotData,
type PlotLineStyle,
type PlotMargins,
type PlotMemoryUsage,
type PlotTheme
Expand All @@ -188,12 +201,23 @@ const NBS_OF_TRACES = [1, 3, 10, 30, 100];
const NBS_OF_POINTS = [10, 100, 1000, 10000, 100000, 1000000, 10000000];
const NBS_OF_INSTANCES = [1, 2, 4, 9, 16, 25, 36, 49];
const NBS_OF_POINTS_PER_SECOND = [10, 100, 1000, 10000, 100000, 1000000];
const LINE_STYLES: Record<PlotLineStyle | 'mixed', string> = {
solid: 'Solid',
dash: 'Dash',
dot: 'Dot',
dashDot: 'Dash dot',
dashDotDot: 'Dash dot dot',
mixed: 'Mixed'
};
const LINE_WIDTHS = [1, 2, 4, 8];

const kind = vue.ref<DataKind>('smooth');
const nbOfTraces = vue.ref<number>(3);
const nbOfPoints = vue.ref<number>(1000);
const nbOfInstances = vue.ref<number>(1);
const theme = vue.ref<PlotTheme | 'mixed'>('mixed');
const lineStyle = vue.ref<PlotLineStyle | 'mixed'>('solid');
const lineWidth = vue.ref<number>(2);
const levelOfDetail = vue.ref<boolean>(true);
const showLegend = vue.ref<boolean>(true);
const customContextMenu = vue.ref<boolean>(false);
Expand Down Expand Up @@ -472,6 +496,23 @@ vue.watch([live, nbOfPointsPerSecond, xRange], () => {
}
});

// Our data, with the line style and line width of our traces, which, if our line style is mixed, go through all our
// line styles (i.e. solid, dash, dot, etc.), so that they can be compared.
// Note: our traces keep their X and Y values, so changing their line style or line width doesn't copy our data.

const MIXED_LINE_STYLES = Object.keys(LINE_STYLES).filter((value) => value !== 'mixed') as PlotLineStyle[];

const styledDatasets = vue.computed<PlotData[]>(() => {
return datasets.value.map((data) => ({
...data,
traces: data.traces.map((trace, index) => ({
...trace,
lineStyle: lineStyle.value === 'mixed' ? MIXED_LINE_STYLES[index % MIXED_LINE_STYLES.length] : lineStyle.value,
lineWidth: lineWidth.value
}))
}));
});

// Our instances, laid out in a grid that is as square as possible.

const plotRefs: (PlotInstance | null)[] = [];
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type {
PlotCsvExportStartedEvent,
PlotData,
PlotImageExportEvent,
PlotLineStyle,
PlotMargins,
PlotMemoryUsage,
PlotPointCounts,
Expand Down
77 changes: 77 additions & 0 deletions src/common/lineStyleUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from 'bun:test';

import { dashGradient, dotLength, type LineStyle, lineDash, validLineWidth } from './lineStyleUtils';

// validLineWidth().

describe('validLineWidth()', () => {
it('keeps a finite and positive line width', () => {
expect(validLineWidth(1, 2)).toBe(1);
expect(validLineWidth(0.5, 2)).toBe(0.5);
expect(validLineWidth(10, 2)).toBe(10);
});

it('uses the default line width for a missing, non-finite, zero, or negative line width', () => {
expect(validLineWidth(undefined, 2)).toBe(2);
expect(validLineWidth(Number.NaN, 2)).toBe(2);
expect(validLineWidth(Number.POSITIVE_INFINITY, 2)).toBe(2);
expect(validLineWidth(0, 2)).toBe(2);
expect(validLineWidth(-1, 2)).toBe(2);
});
});

// dotLength().

describe('dotLength()', () => {
it('is the line width, but at least three pixels', () => {
expect(dotLength(1)).toBe(3);
expect(dotLength(2)).toBe(3);
expect(dotLength(3)).toBe(3);
expect(dotLength(4)).toBe(4);
expect(dotLength(10)).toBe(10);
});
});

// lineDash().

describe('lineDash()', () => {
it('has no dash pattern for a solid line', () => {
expect(lineDash('solid', 3)).toEqual([]);
expect(lineDash(undefined, 3)).toEqual([]);
});

it('has the dash pattern of each line style, in dot lengths', () => {
expect(lineDash('dash', 3)).toEqual([9, 9]);
expect(lineDash('dot', 3)).toEqual([3, 3]);
expect(lineDash('dashDot', 3)).toEqual([9, 3, 3, 3]);
expect(lineDash('dashDotDot', 3)).toEqual([9, 3, 3, 3, 3, 3]);
});

it('scales its dash pattern with the length of a dot', () => {
expect(lineDash('dash', 1)).toEqual([3, 3]);
expect(lineDash('dashDot', 4)).toEqual([12, 4, 4, 4]);
expect(lineDash('dot', 1.5)).toEqual([1.5, 1.5]);
});

it('draws a solid line for a line style that it does not know about', () => {
expect(lineDash('none' as LineStyle, 3)).toEqual([]);
expect(lineDash('toString' as LineStyle, 3)).toEqual([]);
});
});

// dashGradient().

describe('dashGradient()', () => {
it('has no gradient for a solid line', () => {
expect(dashGradient([])).toBe('');
});

it('alternates dashes, in the current colour, and gaps', () => {
expect(dashGradient([6, 6])).toBe(
'repeating-linear-gradient(to right, currentcolor 0px 6px, transparent 6px 12px)'
);
expect(dashGradient([6, 2, 2, 2])).toBe(
'repeating-linear-gradient(to right, currentcolor 0px 6px, transparent 6px 8px, currentcolor 8px 10px, transparent 10px 12px)'
);
});
});
62 changes: 62 additions & 0 deletions src/common/lineStyleUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// The line style and line width of our traces (see linearPaths() in PlotWidget.vue). OpenCOR's plotting is based on
// SED-ML, so our line styles are the line types of SED-ML's Line (see
// https://identifiers.org/combine.specifications/sed-ml.level-1.version-4), except for its none line type (since a
// trace without a line would be invisible, unless it has markers) and, like Plotly, our dash patterns scale with our
// line width, so that a thick line looks right.
// Note: our dash patterns are in the same unit as our line width (e.g., CSS pixels), and like Plotly, the length of a
// dot is our line width, but at least MIN_DOT_LENGTH, so that our dots can be told apart from our dashes.

export type LineStyle = 'solid' | 'dash' | 'dot' | 'dashDot' | 'dashDotDot';

const MIN_DOT_LENGTH = 3; // Like Plotly, the minimum length of a dot (in CSS pixels).

// Like Plotly, the dash pattern of our line styles, in dot lengths.

const DASH_PATTERNS: Record<LineStyle, number[]> = {
solid: [],
dash: [3, 3],
dot: [1, 1],
dashDot: [3, 1, 1, 1],
dashDotDot: [3, 1, 1, 1, 1, 1]
};

export const validLineWidth = (lineWidth: number | undefined, defaultLineWidth: number): number => {
// Return the given line width, if it is valid (i.e. finite and positive), or the given default line width otherwise.

return lineWidth !== undefined && Number.isFinite(lineWidth) && lineWidth > 0 ? lineWidth : defaultLineWidth;
};

export const dotLength = (lineWidth: number): number => {
// Return the length of a dot for a line of the given width.

return Math.max(lineWidth, MIN_DOT_LENGTH);
};

export const lineDash = (lineStyle: LineStyle | undefined, dotLength: number): number[] => {
// Return the dash pattern of the given line style (or of a solid line if it is not a line style that we know about,
// e.g., SED-ML's none line type), i.e. the lengths of its dashes and gaps, for the given length of a dot.

const pattern = lineStyle && Object.hasOwn(DASH_PATTERNS, lineStyle) ? DASH_PATTERNS[lineStyle] : [];

return pattern.map((length: number) => length * dotLength);
};

export const dashGradient = (dash: number[]): string => {
// Return a CSS gradient that draws the given dash pattern (in CSS pixels) from left to right, using the current
// colour, or an empty string for a solid line.

if (!dash.length) {
return '';
}

const res: string[] = [];
let position = 0;

dash.forEach((length: number, index: number) => {
res.push(`${index % 2 ? 'transparent' : 'currentcolor'} ${position}px ${position + length}px`);

position += length;
});

return `repeating-linear-gradient(to right, ${res.join(', ')})`;
};
Loading