1
1
import { ObservableArray } from '@nativescript/core' ;
2
2
import { Entry } from './Entry' ;
3
3
import { BaseDataSet } from './BaseDataSet' ;
4
- import { getEntryXValue } from './BaseEntry' ;
5
4
import { Utils } from '../utils/Utils' ;
6
5
7
6
/**
@@ -59,6 +58,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
59
58
constructor ( values , label , xProperty ?, yProperty ?) {
60
59
super ( label , xProperty , yProperty ) ;
61
60
this . mValues = values ;
61
+ this . updateGetEntryForIndex ( ) ;
62
62
}
63
63
64
64
toString ( ) {
@@ -67,7 +67,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
67
67
68
68
init ( ) {
69
69
if ( this . mValues == null ) this . mValues = [ ] ;
70
-
70
+ this . updateGetEntryForIndex ( ) ;
71
71
if ( this . mValues . length > 0 ) {
72
72
for ( let index = 0 , e : T ; index < this . mValues . length ; index ++ ) {
73
73
e = this . getEntryForIndex ( index ) ;
@@ -131,7 +131,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
131
131
this . calcMinMaxForEntry ( e , index ) ;
132
132
}
133
133
} else {
134
- const x = getEntryXValue ( e , this . xProperty , index ) ;
134
+ const x = this . getEntryXValue ( e , index ) ;
135
135
if ( x < this . mXMin ) this . mXMin = x ;
136
136
137
137
if ( x > this . mXMax ) this . mXMax = x ;
@@ -169,6 +169,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
169
169
*/
170
170
public setValues ( values ) {
171
171
this . mValues = values ;
172
+ this . updateGetEntryForIndex ( ) ;
172
173
this . notifyDataSetChanged ( ) ;
173
174
}
174
175
@@ -256,6 +257,19 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
256
257
return null ;
257
258
}
258
259
260
+ protected updateGetEntryForIndex ( ) {
261
+ const internalValues = this . getInternalValues ( ) ;
262
+ if ( internalValues instanceof ObservableArray ) {
263
+ this . getEntryForIndex = function ( index ) {
264
+ return internalValues . getItem ( index ) ;
265
+ } ;
266
+ } else {
267
+ this . getEntryForIndex = function ( index ) {
268
+ return internalValues [ index ] ;
269
+ } ;
270
+ }
271
+ }
272
+
259
273
public getEntryForIndex ( index ) {
260
274
return this . getInternalValues ( ) instanceof ObservableArray ? ( this . getInternalValues ( ) as ObservableArray < T > ) . getItem ( index ) : this . getInternalValues ( ) [ index ] ;
261
275
}
@@ -267,15 +281,14 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
267
281
let low = 0 ;
268
282
let high = values . length - 1 ;
269
283
let closest = high ;
270
- const xKey = this . xProperty ;
271
284
const yKey = this . yProperty ;
272
285
let m : number , e : T , e1 : T ;
273
286
while ( low < high ) {
274
287
m = Math . floor ( ( low + high ) / 2 ) ;
275
288
e = Utils . getArrayItem ( values , m ) ;
276
289
e1 = Utils . getArrayItem ( values , m + 1 ) ;
277
- const d1 = getEntryXValue ( e , xKey , m ) - xValue ,
278
- d2 = getEntryXValue ( e1 , xKey , m + 1 ) - xValue ,
290
+ const d1 = this . getEntryXValue ( e , m ) - xValue ,
291
+ d2 = this . getEntryXValue ( e1 , m + 1 ) - xValue ,
279
292
ad1 = Math . abs ( d1 ) ,
280
293
ad2 = Math . abs ( d2 ) ;
281
294
@@ -304,7 +317,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
304
317
305
318
if ( closest !== - 1 ) {
306
319
let e = Utils . getArrayItem ( values , closest ) ;
307
- const closestXValue = getEntryXValue ( e , xKey , closest ) ;
320
+ const closestXValue = this . getEntryXValue ( e , closest ) ;
308
321
if ( rounding === Rounding . UP ) {
309
322
// If rounding up, and found x-value is lower than specified x, and we can go upper...
310
323
if ( closestXValue < xValue && closest < values . length - 1 ) {
@@ -321,7 +334,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
321
334
if ( closest >= 1 && ! isNaN ( closestToY ) ) {
322
335
e = Utils . getArrayItem ( values , closest - 1 ) ;
323
336
if ( e ) {
324
- xValue = getEntryXValue ( e , xKey , closest - 1 ) ;
337
+ xValue = this . getEntryXValue ( e , closest - 1 ) ;
325
338
while ( closest > 0 && xValue === closestXValue ) closest -= 1 ;
326
339
327
340
let closestYValue = Utils . getArrayItem ( values , closest ) [ yKey ] ;
@@ -334,7 +347,7 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
334
347
335
348
e = Utils . getArrayItem ( values , closest ) ;
336
349
if ( ! e ) break ;
337
- xValue = getEntryXValue ( e , xKey , closest ) ;
350
+ xValue = this . getEntryXValue ( e , closest ) ;
338
351
339
352
if ( xValue !== closestXValue ) break ;
340
353
@@ -364,17 +377,17 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
364
377
while ( low <= high ) {
365
378
m = Math . floor ( ( high + low ) / 2 ) ;
366
379
e = Utils . getArrayItem ( values , m ) ;
367
- mXValue = getEntryXValue ( e , xKey , m ) ;
380
+ mXValue = this . getEntryXValue ( e , m ) ;
368
381
// if we have a match
369
382
if ( xValue === mXValue ) {
370
- while ( m > 0 && getEntryXValue ( Utils . getArrayItem ( values , m - 1 ) , xKey , m - 1 ) === xValue ) m -- ;
383
+ while ( m > 0 && this . getEntryXValue ( Utils . getArrayItem ( values , m - 1 ) , m - 1 ) === xValue ) m -- ;
371
384
372
385
high = values . length ;
373
386
374
387
// loop over all "equal" entries
375
388
for ( ; m < high ; m ++ ) {
376
389
e = Utils . getArrayItem ( values , m ) ;
377
- mXValue = getEntryXValue ( e , xKey , m ) ;
390
+ mXValue = this . getEntryXValue ( e , m ) ;
378
391
if ( mXValue === xValue ) {
379
392
entries . push ( e ) ;
380
393
} else {
@@ -398,22 +411,21 @@ export abstract class DataSet<T extends Entry> extends BaseDataSet<T> {
398
411
let low = 0 ;
399
412
let high = values . length - 1 ;
400
413
401
- const xKey = this . xProperty ;
402
414
let entry : T , mXValue ;
403
415
while ( low <= high ) {
404
416
let m = Math . floor ( ( high + low ) / 2 ) ;
405
417
entry = Utils . getArrayItem ( values , m ) ;
406
- mXValue = getEntryXValue ( entry , xKey , m ) ;
418
+ mXValue = this . getEntryXValue ( entry , m ) ;
407
419
// if we have a match
408
420
if ( xValue === mXValue ) {
409
- while ( m > 0 && getEntryXValue ( Utils . getArrayItem ( values , m - 1 ) , xKey , m - 1 ) === xValue ) m -- ;
421
+ while ( m > 0 && this . getEntryXValue ( Utils . getArrayItem ( values , m - 1 ) , m - 1 ) === xValue ) m -- ;
410
422
411
423
high = values . length ;
412
424
413
425
// loop over all "equal" entries
414
426
for ( ; m < high ; m ++ ) {
415
427
entry = Utils . getArrayItem ( values , m ) ;
416
- mXValue = getEntryXValue ( entry , xKey , m ) ;
428
+ mXValue = this . getEntryXValue ( entry , m ) ;
417
429
if ( mXValue === xValue ) {
418
430
entries . push ( { entry, index : m } ) ;
419
431
} else {
0 commit comments