Skip to content

Commit 11e26e3

Browse files
committedJun 17, 2020
feat: filtering support!
1 parent d71c92a commit 11e26e3

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed
 

Diff for: ‎src/charting/data/DataSet.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,13 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
129129
if (y > this.mYMax) this.mYMax = y;
130130
}
131131

132+
protected getInternalValues() {
133+
return this.mValues;
134+
135+
}
136+
132137
public getEntryCount() {
133-
return this.mValues.length;
138+
return this.getInternalValues().length;
134139
}
135140

136141
/**
@@ -222,28 +227,29 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
222227
}
223228

224229
public getEntryIndex(e: T) {
225-
return this.mValues.indexOf(e);
230+
return this.getInternalValues().indexOf(e);
226231
}
227232

228233
public getEntryForXValue(xValue, closestToY, rounding = Rounding.CLOSEST): T {
229234
let index = this.getEntryIndexForXValue(xValue, closestToY, rounding);
230-
if (index > -1) return this.mValues[index];
235+
if (index > -1) return this.getInternalValues()[index];
231236
return null;
232237
}
233238

234239
public getEntryForIndex(index) {
235-
return this.mValues[index];
240+
return this.getInternalValues()[index];
236241
}
237242

243+
238244
public getEntryIndexForXValue(xValue, closestToY, rounding) {
239-
if (this.mValues == null || this.mValues.length === 0) return -1;
245+
const values = this.getInternalValues();
246+
if (values == null || values.length === 0) return -1;
240247

241248
let low = 0;
242-
let high = this.mValues.length - 1;
249+
let high = values.length - 1;
243250
let closest = high;
244251
const xProperty = this.xProperty;
245252
const yProperty = this.yProperty;
246-
const values = this.mValues;
247253
while (low < high) {
248254
let m = Math.floor((low + high) / 2);
249255
const d1 = values[m][xProperty] - xValue,
@@ -319,11 +325,11 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
319325
public getEntriesForXValue(xValue) {
320326
const entries = [];
321327

328+
const values = this.getInternalValues();
322329
let low = 0;
323-
let high = this.mValues.length - 1;
330+
let high = values.length - 1;
324331

325332
const xProperty = this.xProperty;
326-
const values = this.mValues;
327333
while (low <= high) {
328334
let m = Math.floor((high + low) / 2);
329335
let entry = values[m];

Diff for: ‎src/charting/data/LineDataSet.ts

+57-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import { DefaultFillFormatter } from '../formatter/DefaultFillFormatter';
77
import { IFillFormatter } from '../formatter/IFillFormatter';
88
import { DashPathEffect, parseDashEffect } from 'nativescript-canvas';
99
import { ColorTemplate } from 'nativescript-chart/utils/ColorTemplate';
10+
import { createLTTB } from 'downsample';
1011

1112
export enum Mode {
1213
LINEAR,
1314
STEPPED,
1415
CUBIC_BEZIER,
15-
HORIZONTAL_BEZIER
16+
HORIZONTAL_BEZIER,
1617
}
1718

1819
export class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet {
@@ -63,6 +64,58 @@ export class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet
6364

6465
private mDrawCircleHole = true;
6566

67+
/**
68+
* the max number allowed point before filtering. <= O means disabled
69+
*/
70+
private mMaxFilterNumber = 0;
71+
72+
public getMaxFilterNumber() {
73+
return this.mMaxFilterNumber;
74+
}
75+
76+
/**
77+
* Sets the max number allowed point before filtering.
78+
*
79+
* @param value: number
80+
*/
81+
public setMaxFilterNumber(value: number) {
82+
this.mMaxFilterNumber = value;
83+
}
84+
85+
protected mFilteredValues: Entry[] = null;
86+
protected mFilterFunction;
87+
public applyFiltering(scaleX: number) {
88+
// console.log('applyFiltering', scaleX, this.mMaxFilterNumber, this.mValues.length, this.mFilteredValues && this.mFilteredValues.length);
89+
if (this.mMaxFilterNumber > 0 && this.mValues.length / scaleX > this.mMaxFilterNumber) {
90+
const filterCount = Math.round(this.mMaxFilterNumber * scaleX);
91+
if (!this.mFilteredValues || this.mFilteredValues.length !== filterCount) {
92+
if (!this.mFilterFunction) {
93+
this.mFilterFunction = createLTTB({
94+
x: this.xProperty,
95+
y: this.yProperty,
96+
} as any);
97+
}
98+
this.mFilteredValues = this.mFilterFunction(this.mValues, filterCount);
99+
}
100+
} else if (this.mFilteredValues) {
101+
this.mFilteredValues = null;
102+
}
103+
}
104+
105+
mIgnoreFiltered = false
106+
protected getInternalValues() {
107+
if (this.mFilteredValues && !this.mIgnoreFiltered) {
108+
return this.mFilteredValues;
109+
}
110+
return this.mValues;
111+
}
112+
setIgnoreFiltered(value) {
113+
this.mIgnoreFiltered = value;
114+
}
115+
isFiltered() {
116+
return !!this.mFilteredValues;
117+
}
118+
66119
constructor(yVals, label, xProperty?, yProperty?) {
67120
super(yVals, label, xProperty, yProperty);
68121

@@ -72,7 +125,7 @@ export class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet
72125
// if (this.mCircleColors == null) {
73126
// this.mCircleColors = [];
74127
// }
75-
this.mCircleColors = []
128+
this.mCircleColors = [];
76129

77130
// default colors
78131
// this.mColors.add(new Color(255, 192, 255, 140));
@@ -183,7 +236,7 @@ export class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet
183236
* @param phase offset, in degrees (normally, use 0)
184237
*/
185238
public enableDashedLine(lineLength, spaceLength, phase) {
186-
this.mDashPathEffect = new DashPathEffect([lineLength,spaceLength], phase);;
239+
this.mDashPathEffect = new DashPathEffect([lineLength, spaceLength], phase);
187240
// this.mDashPathEffect = parseDashEffect(`${lineLength} ${spaceLength} ${phase}`) ;
188241
}
189242

@@ -272,7 +325,7 @@ export class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet
272325
*/
273326
public resetCircleColors() {
274327
// if (this.mCircleColors == null) {
275-
this.mCircleColors = [];
328+
this.mCircleColors = [];
276329
// }
277330
// this.mCircleColors.clear();
278331
}

Diff for: ‎src/charting/interfaces/datasets/ILineDataSet.ts

+3
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,7 @@ export interface ILineDataSet extends ILineRadarDataSet<Entry> {
9292
* @return
9393
*/
9494
getFillFormatter();
95+
96+
applyFiltering(scaleX: number);
97+
setMaxFilterNumber(value: number);
9598
}

0 commit comments

Comments
 (0)
Please sign in to comment.