1
+ import { ObservableArray } from '@nativescript/core' ;
1
2
import { Entry } from './Entry' ;
2
3
import { BaseDataSet } from './BaseDataSet' ;
3
4
import { getEntryXValue } from './BaseEntry' ;
5
+ import { Utils } from '../utils/Utils' ;
4
6
5
7
/**
6
8
* Determines how to round DataSet index values for
@@ -24,7 +26,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
24
26
/**
25
27
* the entries that this DataSet represents / holds together
26
28
*/
27
- protected mValues : T [ ] = null ;
29
+ protected mValues : T [ ] | ObservableArray < T > = null ;
28
30
29
31
/**
30
32
* maximum y-value in the value array
@@ -68,7 +70,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
68
70
69
71
if ( this . mValues . length > 0 ) {
70
72
for ( let index = 0 , e : T ; index < this . mValues . length ; index ++ ) {
71
- e = this . mValues [ index ] ;
73
+ e = this . getEntryForIndex ( index ) ;
72
74
this . initEntryData ( e ) ;
73
75
this . calcMinMaxForEntry ( e , index ) ;
74
76
}
@@ -83,7 +85,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
83
85
if ( this . mValues == null || this . mValues . length === 0 ) return ;
84
86
85
87
for ( let index = 0 , e : T ; index < this . mValues . length ; index ++ ) {
86
- e = this . mValues [ index ] ;
88
+ e = this . getEntryForIndex ( index ) ;
87
89
this . calcMinMaxForEntry ( e , index ) ;
88
90
}
89
91
}
@@ -99,7 +101,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
99
101
100
102
for ( let i = indexFrom ; i <= indexTo ; i ++ ) {
101
103
// only recalculate y
102
- this . calcMinMaxY ( this . mValues [ i ] ) ;
104
+ this . calcMinMaxY ( this . getEntryForIndex ( i ) ) ;
103
105
}
104
106
}
105
107
@@ -125,7 +127,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
125
127
this . mXMin = Infinity ;
126
128
127
129
for ( let index = 0 , e : T ; index < this . mValues . length ; index ++ ) {
128
- e = this . mValues [ index ] ;
130
+ e = this . getEntryForIndex ( index ) ;
129
131
this . calcMinMaxForEntry ( e , index ) ;
130
132
}
131
133
} else {
@@ -194,7 +196,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
194
196
}
195
197
196
198
let addedIndex = this . mValues . length ;
197
- if ( this . mValues . length > 0 && this . xProperty && this . mValues [ this . mValues . length - 1 ] > e [ this . xProperty ] ) {
199
+ if ( this . mValues . length > 0 && this . xProperty && this . getEntryForIndex ( this . mValues . length - 1 ) > e [ this . xProperty ] ) {
198
200
addedIndex = this . getEntryIndexForXValue ( e [ this . xProperty ] , e [ this . yProperty ] , Rounding . UP ) ;
199
201
this . mValues . splice ( addedIndex , 0 , e ) ;
200
202
} else {
@@ -245,17 +247,17 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
245
247
246
248
public getEntryForXValue ( xValue , closestToY , rounding = Rounding . CLOSEST ) : T {
247
249
const index = this . getEntryIndexForXValue ( xValue , closestToY , rounding ) ;
248
- if ( index > - 1 ) return this . getInternalValues ( ) [ index ] ;
250
+ if ( index > - 1 ) return this . getEntryForIndex ( index ) ;
249
251
return null ;
250
252
}
251
253
public getEntryAndIndexForXValue ( xValue , closestToY , rounding = Rounding . CLOSEST ) : { entry : T ; index : number } {
252
254
const index = this . getEntryIndexForXValue ( xValue , closestToY , rounding ) ;
253
- if ( index > - 1 ) return { entry : this . getInternalValues ( ) [ index ] , index } ;
255
+ if ( index > - 1 ) return { entry : this . getEntryForIndex ( index ) , index } ;
254
256
return null ;
255
257
}
256
258
257
259
public getEntryForIndex ( index ) {
258
- return this . getInternalValues ( ) [ index ] ;
260
+ return this . getInternalValues ( ) instanceof ObservableArray ? ( this . getInternalValues ( ) as ObservableArray < T > ) . getItem ( index ) : this . getInternalValues ( ) [ index ] ;
259
261
}
260
262
261
263
public getEntryIndexForXValue ( xValue , closestToY , rounding ) {
@@ -270,8 +272,8 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
270
272
let m : number , e : T , e1 : T ;
271
273
while ( low < high ) {
272
274
m = Math . floor ( ( low + high ) / 2 ) ;
273
- e = values [ m ] ;
274
- e1 = values [ m + 1 ] ;
275
+ e = Utils . getArrayItem ( values , m ) ;
276
+ e1 = Utils . getArrayItem ( values , m + 1 ) ;
275
277
const d1 = getEntryXValue ( e , xKey , m ) - xValue ,
276
278
d2 = getEntryXValue ( e1 , xKey , m + 1 ) - xValue ,
277
279
ad1 = Math . abs ( d1 ) ,
@@ -301,7 +303,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
301
303
}
302
304
303
305
if ( closest !== - 1 ) {
304
- let e = values [ closest ] ;
306
+ let e = Utils . getArrayItem ( values , closest ) ;
305
307
const closestXValue = getEntryXValue ( e , xKey , closest ) ;
306
308
if ( rounding === Rounding . UP ) {
307
309
// If rounding up, and found x-value is lower than specified x, and we can go upper...
@@ -317,19 +319,19 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
317
319
318
320
// Search by closest to y-value
319
321
if ( ! isNaN ( closestToY ) ) {
320
- e = values [ closest - 1 ] ;
322
+ e = Utils . getArrayItem ( values , closest - 1 ) ;
321
323
xValue = getEntryXValue ( e , xKey , closest - 1 ) ;
322
324
while ( closest > 0 && xValue === closestXValue ) closest -= 1 ;
323
325
324
- let closestYValue = values [ closest ] [ yKey ] ;
326
+ let closestYValue = Utils . getArrayItem ( values , closest ) [ yKey ] ;
325
327
let closestYIndex = closest ;
326
328
327
329
// eslint-disable-next-line no-constant-condition
328
330
while ( true ) {
329
331
closest += 1 ;
330
332
if ( closest >= values . length ) break ;
331
333
332
- e = values [ closest ] ;
334
+ e = Utils . getArrayItem ( values , closest ) ;
333
335
xValue = getEntryXValue ( e , xKey , closest ) ;
334
336
335
337
if ( xValue !== closestXValue ) break ;
@@ -358,17 +360,17 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
358
360
let m : number , e : T , e1 : T , mXValue ;
359
361
while ( low <= high ) {
360
362
m = Math . floor ( ( high + low ) / 2 ) ;
361
- e = values [ m ] ;
363
+ e = Utils . getArrayItem ( values , m ) ;
362
364
mXValue = getEntryXValue ( e , xKey , m ) ;
363
365
// if we have a match
364
366
if ( xValue === mXValue ) {
365
- while ( m > 0 && getEntryXValue ( values [ m - 1 ] , xKey , m - 1 ) === xValue ) m -- ;
367
+ while ( m > 0 && getEntryXValue ( Utils . getArrayItem ( values , m - 1 ) , xKey , m - 1 ) === xValue ) m -- ;
366
368
367
369
high = values . length ;
368
370
369
371
// loop over all "equal" entries
370
372
for ( ; m < high ; m ++ ) {
371
- e = values [ m ] ;
373
+ e = Utils . getArrayItem ( values , m ) ;
372
374
mXValue = getEntryXValue ( e , xKey , m ) ;
373
375
if ( mXValue === xValue ) {
374
376
entries . push ( e ) ;
@@ -397,17 +399,17 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
397
399
let entry : T , mXValue ;
398
400
while ( low <= high ) {
399
401
let m = Math . floor ( ( high + low ) / 2 ) ;
400
- entry = values [ m ] ;
402
+ entry = Utils . getArrayItem ( values , m ) ;
401
403
mXValue = getEntryXValue ( entry , xKey , m ) ;
402
404
// if we have a match
403
405
if ( xValue === mXValue ) {
404
- while ( m > 0 && getEntryXValue ( values [ m - 1 ] , xKey , m - 1 ) === xValue ) m -- ;
406
+ while ( m > 0 && getEntryXValue ( Utils . getArrayItem ( values , m - 1 ) , xKey , m - 1 ) === xValue ) m -- ;
405
407
406
408
high = values . length ;
407
409
408
410
// loop over all "equal" entries
409
411
for ( ; m < high ; m ++ ) {
410
- entry = values [ m ] ;
412
+ entry = Utils . getArrayItem ( values , m ) ;
411
413
mXValue = getEntryXValue ( entry , xKey , m ) ;
412
414
if ( mXValue === xValue ) {
413
415
entries . push ( { entry, index : m } ) ;
0 commit comments