Skip to content

Commit ee94fb4

Browse files
committed
Refined PlotLine.
1 parent 3159bad commit ee94fb4

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

src/lib/charting/plot/PlotLine.tsx

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,66 @@ type Props = {
1616
}
1717

1818
const PlotLine = (props: Props) => {
19-
const { xc, yc, tvar, name, atIndex, depth, options } = props;
19+
const { xc, yc, tvar, name, atIndex, depth, options } = props
20+
21+
const fillgap = options.fillgaps;
2022

2123
function plot() {
22-
const path = plotLine();
24+
const path = new Path()
25+
26+
const points = collectPoints();
27+
28+
let prevY: number
29+
for (let m = 0; m < points.length; m++) {
30+
const [x, y] = points[m]
31+
32+
if (y !== undefined) {
33+
if (prevY === undefined) {
34+
// new segment
35+
path.moveto(x, y)
36+
37+
} else {
38+
path.lineto(x, y)
39+
}
40+
}
41+
42+
prevY = y
43+
}
2344

2445
return { path }
2546
}
2647

27-
function plotLine(): Path {
28-
const path = new Path()
48+
function collectPoints() {
49+
const points: number[][] = []
2950

30-
let y1: number // for prev
31-
let y2: number // for curr
32-
33-
// For those need connect from one bar to the next, use bar++ instead of
34-
// bar += xc.nBarsCompressed to avoid uncontinuted line.
35-
for (let bar = 1; bar <= xc.nBars; bar += xc.nBarsCompressed) {
36-
// use `undefined` to test if value has been set at least one time
51+
for (let bar = 1; bar <= xc.nBars; bar++) {
3752
let value: number
38-
let high = Number.NEGATIVE_INFINITY;
39-
let low = Number.POSITIVE_INFINITY;
40-
for (let i = 0; i < xc.nBarsCompressed; i++) {
41-
const time = xc.tb(bar + i)
42-
if (tvar.occurred(time)) {
43-
const datas = tvar.getByTime(time);
44-
const data = datas ? datas[atIndex] : undefined;
45-
const v = data ? data.value : NaN;
46-
if (typeof v === "number" && !isNaN(v)) {
47-
value = v;
48-
high = Math.max(high, v);
49-
low = Math.min(low, v);
50-
}
53+
const time = xc.tb(bar)
54+
if (tvar.occurred(time)) {
55+
const datas = tvar.getByTime(time);
56+
const data = datas ? datas[atIndex] : undefined;
57+
const v = data ? data.value : NaN;
58+
if (typeof v === "number" && !isNaN(v)) {
59+
value = v;
5160
}
5261
}
5362

5463
if (value !== undefined && !isNaN(value)) {
55-
y2 = yc.yv(value)
56-
if (xc.nBarsCompressed > 1) {
57-
// draw a vertical line to cover the min to max
58-
const x = xc.xb(bar)
59-
path.moveto(x, yc.yv(low));
60-
path.lineto(x, yc.yv(high));
64+
const x = xc.xb(bar)
65+
const y = yc.yv(value)
6166

62-
} else {
63-
if (y1 !== undefined && !isNaN(y1)) {
64-
// x1 shoud be decided here, it may not equal prev x2:
65-
// think about the case of on calendar day mode
66-
const x1 = xc.xb(bar - xc.nBarsCompressed)
67-
const x2 = xc.xb(bar)
68-
path.moveto(x1, y1);
69-
path.lineto(x2, y2);
70-
}
67+
if (y !== undefined && !isNaN(y)) {
68+
points.push([x, y])
7169
}
72-
y1 = y2;
7370

71+
} else {
72+
if (!fillgap) {
73+
points.push([undefined, undefined])
74+
}
7475
}
7576
}
7677

77-
return path
78+
return points
7879
}
7980

8081
const { path } = plot();

src/lib/charting/plot/PlotStepLine.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ const PlotStepLine = (props: Props) => {
3535
for (let m = 0; m < points.length; m++) {
3636
const [x, y] = points[m]
3737

38-
if (y === undefined) {
39-
prevY = undefined
40-
41-
} else {
38+
if (y !== undefined) {
4239
if (prevY === undefined) {
4340
// new segment
4441
path.moveto(x, y)
@@ -75,8 +72,6 @@ const PlotStepLine = (props: Props) => {
7572
value = v;
7673
}
7774

78-
// console.log(index, data)
79-
8075
if (typeof v === "number" && isNaN(v) === false) {
8176
value = v;
8277
}

0 commit comments

Comments
 (0)