Skip to content

Commit c4eacf1

Browse files
Merge pull request #200 from DiamondLightSource/update-traces
Update Trace and Plt classes
2 parents 2307b98 + 90b9fc9 commit c4eacf1

15 files changed

Lines changed: 224 additions & 188 deletions

File tree

src/types/plt.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { newAxis } from "./axis";
22
import { ColorUtils } from "./color";
33
import { newFont } from "./font";
4-
import { Plt } from "./plt";
5-
import { Trace } from "./trace";
4+
import { newPlt } from "./plt";
5+
import { newTrace } from "./trace";
66

77
describe("Plt", () => {
88
it("constructs the plt with values", (): void => {
99
const testValues = {
1010
title: "Testing",
1111
axes: [newAxis({}), newAxis({ color: ColorUtils.RED })],
12-
pvlist: [new Trace({ yPv: "TEST" })],
12+
pvlist: [newTrace({ yPv: "TEST" })],
1313
background: ColorUtils.WHITE,
1414
foreground: ColorUtils.RED,
1515
scroll: false,
@@ -24,11 +24,11 @@ describe("Plt", () => {
2424
start: "10 minutes",
2525
end: "now"
2626
};
27-
const plt = new Plt(testValues);
27+
const plt = newPlt(testValues);
2828
const actualValues = {
2929
title: "Testing",
3030
axes: [newAxis({}), newAxis({ color: ColorUtils.RED })],
31-
pvlist: [new Trace({ yPv: "TEST" })],
31+
pvlist: [newTrace({ yPv: "TEST" })],
3232
backgroundColor: ColorUtils.WHITE.colorString,
3333
foregroundColor: ColorUtils.RED.colorString,
3434
scroll: false,
@@ -54,11 +54,10 @@ describe("Plt", () => {
5454
axes: [],
5555
pvlist: []
5656
});
57-
expect(plt).toBeInstanceOf(Plt);
5857
});
5958

6059
it("construct the trace with only defaults", (): void => {
61-
const plt = new Plt();
60+
const plt = newPlt({});
6261
expect({
6362
...plt,
6463
backgroundColor: plt.backgroundColor.colorString,
@@ -83,6 +82,5 @@ describe("Plt", () => {
8382
start: "1 minute",
8483
end: "now"
8584
});
86-
expect(plt).toBeInstanceOf(Plt);
8785
});
8886
});

src/types/plt.ts

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,59 @@
11
import { Axes, newAxis } from "./axis";
22
import { Color, ColorUtils } from "./color";
33
import { Font, newFont } from "./font";
4-
import { Trace } from "./trace";
4+
import { newTrace, Trace } from "./trace";
55

6-
export class Plt {
7-
public title: string;
8-
public axes: Axes;
9-
public pvlist: Trace[];
10-
public backgroundColor: Color;
11-
public foregroundColor: Color;
12-
public scroll: boolean;
13-
public scrollStep: number;
14-
public updatePeriod: number;
15-
public bufferSize: number;
16-
public start: string;
17-
public end: string;
18-
public showGrid: boolean;
19-
public titleFont: Font;
20-
public scaleFont: Font;
21-
public labelFont: Font;
22-
public legendFont: Font;
23-
24-
/**
25-
* Set default values for properties not yet
26-
* set, otherwise use set property.
27-
*/
28-
public constructor({
29-
title = "",
30-
axes = [newAxis({})],
31-
pvlist = [new Trace()],
32-
background = ColorUtils.WHITE,
33-
foreground = ColorUtils.BLACK,
34-
scroll = true,
35-
grid = false,
36-
scrollStep = 5,
37-
updatePeriod = 0,
38-
bufferSize = 5000,
39-
titleFont = newFont(),
40-
labelFont = newFont(),
41-
legendFont = newFont(),
42-
scaleFont = newFont(),
43-
start = "1 minute",
44-
end = "now"
45-
} = {}) {
46-
this.backgroundColor = background;
47-
this.foregroundColor = foreground;
48-
this.title = title;
49-
this.scroll = scroll;
50-
this.scrollStep = scrollStep;
51-
this.axes = axes;
52-
this.pvlist = pvlist;
53-
this.updatePeriod = updatePeriod;
54-
this.bufferSize = bufferSize;
55-
this.showGrid = grid;
56-
this.start = start;
57-
this.end = end;
58-
this.titleFont = titleFont;
59-
this.scaleFont = scaleFont;
60-
this.labelFont = labelFont;
61-
this.legendFont = legendFont;
62-
}
6+
export interface Plt {
7+
title: string;
8+
axes: Axes;
9+
pvlist: Trace[];
10+
backgroundColor: Color;
11+
foregroundColor: Color;
12+
scroll: boolean;
13+
scrollStep: number;
14+
updatePeriod: number;
15+
bufferSize: number;
16+
start: string;
17+
end: string;
18+
showGrid: boolean;
19+
titleFont: Font;
20+
scaleFont: Font;
21+
labelFont: Font;
22+
legendFont: Font;
6323
}
24+
25+
export const newPlt = (config: {
26+
title?: string;
27+
axes?: Axes;
28+
pvlist?: Trace[];
29+
background?: Color;
30+
foreground?: Color;
31+
scroll?: boolean;
32+
scrollStep?: number;
33+
updatePeriod?: number;
34+
bufferSize?: number;
35+
start?: string;
36+
end?: string;
37+
grid?: boolean;
38+
titleFont?: Font;
39+
scaleFont?: Font;
40+
labelFont?: Font;
41+
legendFont?: Font;
42+
}): Plt => ({
43+
title: config.title ?? "",
44+
axes: config.axes ?? [newAxis({})],
45+
pvlist: config.pvlist ?? [newTrace({})],
46+
backgroundColor: config.background ?? ColorUtils.WHITE,
47+
foregroundColor: config.foreground ?? ColorUtils.BLACK,
48+
scroll: config.scroll ?? true,
49+
showGrid: config.grid ?? false,
50+
scrollStep: config.scrollStep ?? 5,
51+
updatePeriod: config.updatePeriod ?? 0,
52+
bufferSize: config.bufferSize ?? 5000,
53+
titleFont: config.titleFont ?? newFont(),
54+
labelFont: config.labelFont ?? newFont(),
55+
legendFont: config.legendFont ?? newFont(),
56+
scaleFont: config.scaleFont ?? newFont(),
57+
start: config.start ?? "1 minute",
58+
end: config.end ?? "now"
59+
});

src/types/trace.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ColorUtils } from "./color";
2-
import { Trace } from "./trace";
2+
import { newTrace } from "./trace";
33

44
describe("Trace", () => {
55
it("constructs the trace with values", (): void => {
@@ -20,14 +20,13 @@ describe("Trace", () => {
2020
url: "Testing.diamond.ac.uk"
2121
}
2222
};
23-
const trace = new Trace(testValues);
23+
const trace = newTrace(testValues);
2424

2525
expect(trace).toEqual(testValues);
26-
expect(trace).toBeInstanceOf(Trace);
2726
});
2827

2928
it("construct the trace with only defaults", (): void => {
30-
const trace = new Trace();
29+
const trace = newTrace({});
3130
expect({ ...trace, color: "" }).toEqual({
3231
name: "",
3332
axis: 0,
@@ -47,6 +46,5 @@ describe("Trace", () => {
4746
expect(trace.color.colorString).toEqual(
4847
ColorUtils.fromRgba(0, 0, 255).colorString
4948
);
50-
expect(trace).toBeInstanceOf(Trace);
5149
});
5250
});

src/types/trace.ts

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,72 @@ export interface Archiver {
55
url: string;
66
}
77

8-
export class Trace {
9-
public name: string;
10-
public axis: number;
11-
public lineWidth: number;
12-
public lineStyle: number;
13-
public traceType: number;
14-
public color: Color;
15-
public pointType: number;
16-
public pointSize: number;
17-
public visible: boolean;
18-
public yPv: string;
19-
public xPv?: string | null;
20-
public bufferSize?: number;
21-
public plotMode?: number;
22-
public antiAlias?: boolean;
23-
public concatenateData?: boolean;
24-
public updateDelay?: number;
25-
public updateMode?: number;
26-
public archive?: Archiver | undefined;
27-
28-
public constructor({
29-
name = "",
30-
axis = 0,
31-
lineWidth = 0,
32-
lineStyle = 0,
33-
traceType = 2,
34-
color = ColorUtils.fromRgba(0, 0, 255),
35-
pointType = 0,
36-
pointSize = 1,
37-
visible = true,
38-
xPv = "",
39-
yPv = "",
40-
fromOpi = false,
41-
antiAlias = true,
42-
bufferSize = 100,
43-
concatenateData = true,
44-
updateDelay = 100,
45-
updateMode = 0,
46-
plotMode = 0,
47-
archive = { name: "", url: "" }
48-
} = {}) {
49-
// xPV property only exists on XYPlot
50-
if (xPv) this.xPv = xPv;
51-
this.yPv = yPv;
52-
this.axis = axis;
53-
this.name = name || (yPv ? yPv : "");
54-
this.lineWidth = lineWidth;
55-
this.lineStyle = lineStyle;
56-
this.traceType = traceType;
57-
this.color = color;
58-
this.pointType = pointType;
59-
this.pointSize = pointSize;
60-
this.visible = visible;
61-
if (archive) this.archive = archive;
62-
if (fromOpi) {
63-
this.antiAlias = antiAlias;
64-
this.bufferSize = bufferSize;
65-
this.concatenateData = concatenateData;
66-
this.updateDelay = updateDelay;
67-
this.updateMode = updateMode;
68-
this.plotMode = plotMode;
69-
}
70-
}
8+
export interface Trace {
9+
name: string;
10+
axis: number;
11+
lineWidth: number;
12+
lineStyle: number;
13+
traceType: number;
14+
color: Color;
15+
pointType: number;
16+
pointSize: number;
17+
visible: boolean;
18+
yPv: string;
19+
xPv?: string | null;
20+
bufferSize?: number;
21+
plotMode?: number;
22+
antiAlias?: boolean;
23+
concatenateData?: boolean;
24+
updateDelay?: number;
25+
updateMode?: number;
26+
archive?: Archiver | undefined;
7127
}
28+
29+
/**
30+
* Set default values for properties not yet
31+
* set, otherwise use set property. Uses same
32+
* default values as csstudio.opibuilder.xygraph.
33+
*/
34+
export const newTrace = (config: {
35+
name?: string;
36+
axis?: number;
37+
lineWidth?: number;
38+
lineStyle?: number;
39+
traceType?: number;
40+
color?: Color;
41+
pointType?: number;
42+
pointSize?: number;
43+
visible?: boolean;
44+
yPv?: string;
45+
xPv?: string | null;
46+
bufferSize?: number;
47+
plotMode?: number;
48+
antiAlias?: boolean;
49+
concatenateData?: boolean;
50+
updateDelay?: number;
51+
updateMode?: number;
52+
archive?: Archiver | undefined;
53+
fromOpi?: boolean;
54+
}): Trace => ({
55+
name: config.name || (config.yPv ? config.yPv : ""),
56+
axis: config.axis ?? 0,
57+
lineWidth: config.lineWidth ?? 0,
58+
lineStyle: config.lineStyle ?? 0,
59+
traceType: config.traceType ?? 2,
60+
archive: config.archive ?? { name: "", url: "" },
61+
color: config.color ?? ColorUtils.fromRgba(0, 0, 255),
62+
pointType: config.pointType ?? 0,
63+
pointSize: config.pointSize ?? 1,
64+
visible: config.visible ?? true,
65+
...(config.xPv && { xPv: config.xPv ?? "" }),
66+
yPv: config.yPv ?? "",
67+
// Legacy opi props
68+
...(config.fromOpi && {
69+
antiAlias: config.antiAlias ?? true,
70+
bufferSize: config.bufferSize ?? 100,
71+
concatenateData: config.concatenateData ?? true,
72+
updateDelay: config.updateDelay ?? 100,
73+
updateMode: config.updateMode ?? 0,
74+
plotMode: config.plotMode ?? 0
75+
})
76+
});

src/ui/hooks/useArchivedData.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from "react";
22
import { useArchivedData } from "./useArchivedData";
3-
import { Plt } from "../../types/plt";
3+
import { newPlt, Plt } from "../../types/plt";
44
import { vi } from "vitest";
55
import { newAxis } from "../../types/axis";
6-
import { Trace } from "../../types/trace";
6+
import { newTrace } from "../../types/trace";
77
import { act, screen } from "@testing-library/react";
88
import { contextRender } from "../../testResources";
99

@@ -58,9 +58,9 @@ const ArchivedDataTester = (props: { plt: Plt }): JSX.Element => {
5858

5959
describe("useArchivedData", (): void => {
6060
it("returns values if successful archiver call", async () => {
61-
const plt = new Plt({
61+
const plt = newPlt({
6262
pvlist: [
63-
new Trace({
63+
newTrace({
6464
archive: {
6565
name: "Primary",
6666
url: "http://archiver.diamond.ac.uk/retrieval"

0 commit comments

Comments
 (0)