1
1
import { LineRadarRenderer } from './LineRadarRenderer' ;
2
2
import { LineDataProvider } from '../interfaces/dataprovider/LineDataProvider' ;
3
- import { Direction , Paint , Canvas , Path , Style , createImage , releaseImage , FillType } from 'nativescript-canvas' ;
3
+ import { Direction , Paint , Canvas , Path , Style , createImage , releaseImage , FillType , Matrix } from 'nativescript-canvas' ;
4
4
import { ImageSource } from '@nativescript/core/image-source/image-source' ;
5
5
import { ChartAnimator } from '../animation/ChartAnimator' ;
6
6
import { ViewPortHandler } from '../utils/ViewPortHandler' ;
@@ -487,56 +487,57 @@ export class LineChartRenderer extends LineRadarRenderer {
487
487
private mLineBuffer : number [ ] ;
488
488
489
489
@profile
490
- public drawValues ( c : Canvas ) {
491
- if ( this . isDrawingValuesAllowed ( this . mChart ) ) {
492
- const dataSets = this . mChart . getLineData ( ) . getDataSets ( ) ;
490
+ public drawValuesForDataset ( c : Canvas , dataSet : LineDataSet ) {
491
+ const yKey = dataSet . yProperty ;
492
+ // apply the text-styling defined by the DataSet
493
+ this . applyValueTextStyle ( dataSet ) ;
493
494
494
- for ( let i = 0 ; i < dataSets . length ; i ++ ) {
495
- const dataSet = dataSets [ i ] ;
496
- const yKey = dataSet . yProperty ;
497
-
498
- if ( ! this . shouldDrawValues ( dataSet ) || dataSet . getEntryCount ( ) < 1 ) continue ;
495
+ const trans = this . mChart . getTransformer ( dataSet . getAxisDependency ( ) ) ;
499
496
500
- // apply the text-styling defined by the DataSet
501
- this . applyValueTextStyle ( dataSet ) ;
497
+ // make sure the values do not interfear with the circles
498
+ let valOffset = dataSet . getCircleRadius ( ) * 1.75 ;
502
499
503
- const trans = this . mChart . getTransformer ( dataSet . getAxisDependency ( ) ) ;
500
+ if ( ! dataSet . isDrawCirclesEnabled ( ) ) valOffset = valOffset / 2 ;
504
501
505
- // make sure the values do not interfear with the circles
506
- let valOffset = dataSet . getCircleRadius ( ) * 1.75 ;
502
+ this . mXBounds . set ( this . mChart , dataSet , this . mAnimator ) ;
507
503
508
- if ( ! dataSet . isDrawCirclesEnabled ( ) ) valOffset = valOffset / 2 ;
504
+ const positions = trans . generateTransformedValues ( dataSet , this . mAnimator . getPhaseX ( ) , this . mAnimator . getPhaseY ( ) , this . mXBounds . min , this . mXBounds . max ) ;
505
+ const formatter = dataSet . getValueFormatter ( ) ;
509
506
510
- this . mXBounds . set ( this . mChart , dataSet , this . mAnimator ) ;
507
+ const iconsOffset = Object . assign ( { } , dataSet . getIconsOffset ( ) ) ;
511
508
512
- const positions = trans . generateTransformedValues ( dataSet , this . mAnimator . getPhaseX ( ) , this . mAnimator . getPhaseY ( ) , this . mXBounds . min , this . mXBounds . max ) ;
513
- const formatter = dataSet . getValueFormatter ( ) ;
509
+ for ( let j = 0 ; j < positions . length ; j += 2 ) {
510
+ let x = positions [ j ] ;
511
+ let y = positions [ j + 1 ] ;
514
512
515
- const iconsOffset = Object . assign ( { } , dataSet . getIconsOffset ( ) ) ;
513
+ if ( ! this . mViewPortHandler . isInBoundsRight ( x ) ) break ;
516
514
517
- for ( let j = 0 ; j < positions . length ; j += 2 ) {
518
- let x = positions [ j ] ;
519
- let y = positions [ j + 1 ] ;
515
+ if ( ! this . mViewPortHandler . isInBoundsLeft ( x ) || ! this . mViewPortHandler . isInBoundsY ( y ) ) continue ;
520
516
521
- if ( ! this . mViewPortHandler . isInBoundsRight ( x ) ) break ;
517
+ let entry = dataSet . getEntryForIndex ( j / 2 + this . mXBounds . min ) ;
518
+ if ( ! entry ) continue ;
522
519
523
- if ( ! this . mViewPortHandler . isInBoundsLeft ( x ) || ! this . mViewPortHandler . isInBoundsY ( y ) ) continue ;
520
+ if ( dataSet . isDrawValuesEnabled ( ) ) {
521
+ this . drawValue ( c , formatter . getFormattedValue ( entry [ yKey ] ) , x , y - valOffset , dataSet . getValueTextColor ( j / 2 ) ) ;
522
+ }
524
523
525
- let entry = dataSet . getEntryForIndex ( j / 2 + this . mXBounds . min ) ;
526
- if ( ! entry ) continue ;
524
+ if ( entry . icon != null && dataSet . isDrawIconsEnabled ( ) ) {
525
+ let icon = entry . icon ;
527
526
528
- if ( dataSet . isDrawValuesEnabled ( ) ) {
529
- this . drawValue ( c , formatter . getFormattedValue ( entry [ yKey ] ) , x , y - valOffset , dataSet . getValueTextColor ( j / 2 ) ) ;
530
- }
527
+ Utils . drawImage ( c , icon , x + iconsOffset . x , y + iconsOffset . y , icon . getIntrinsicWidth ( ) , icon . getIntrinsicHeight ( ) ) ;
528
+ }
529
+ }
530
+ }
531
531
532
- if ( entry . icon != null && dataSet . isDrawIconsEnabled ( ) ) {
533
- let icon = entry . icon ;
532
+ public drawValues ( c : Canvas ) {
533
+ if ( this . isDrawingValuesAllowed ( this . mChart ) ) {
534
+ const dataSets = this . mChart . getLineData ( ) . getDataSets ( ) ;
534
535
535
- Utils . drawImage ( c , icon , x + iconsOffset . x , y + iconsOffset . y , icon . getIntrinsicWidth ( ) , icon . getIntrinsicHeight ( ) ) ;
536
- }
537
- }
536
+ for ( let i = 0 ; i < dataSets . length ; i ++ ) {
537
+ const dataSet = dataSets [ i ] ;
538
538
539
- // MPPointF.recycleInstance(iconsOffset);
539
+ if ( ! this . shouldDrawValues ( dataSet ) || dataSet . getEntryCount ( ) < 1 ) continue ;
540
+ this . drawValuesForDataset ( c , dataSet ) ;
540
541
}
541
542
}
542
543
}
@@ -560,72 +561,76 @@ export class LineChartRenderer extends LineRadarRenderer {
560
561
*/
561
562
private mCirclesBuffer = Utils . createNativeArray ( 2 ) ;
562
563
563
- protected drawCircles ( c : Canvas ) {
564
- this . mRenderPaint . setStyle ( Style . FILL ) ;
565
-
564
+ @ profile
565
+ protected drawCirclesForDataset ( c : Canvas , dataSet : LineDataSet ) {
566
+ this . mCirclePaintInner . setColor ( dataSet . getCircleHoleColor ( ) ) ;
566
567
let phaseY = this . mAnimator . getPhaseY ( ) ;
567
568
568
- this . mCirclesBuffer [ 0 ] = 0 ;
569
- this . mCirclesBuffer [ 1 ] = 0 ;
569
+ const xKey = dataSet . xProperty ;
570
+ const yKey = dataSet . yProperty ;
571
+ const trans = this . mChart . getTransformer ( dataSet . getAxisDependency ( ) ) ;
570
572
571
- const dataSets = this . mChart . getLineData ( ) . getDataSets ( ) ;
573
+ this . mXBounds . set ( this . mChart , dataSet , this . mAnimator ) ;
572
574
573
- for ( let i = 0 ; i < dataSets . length ; i ++ ) {
574
- const dataSet = dataSets [ i ] ;
575
- const xKey = dataSet . xProperty ;
576
- const yKey = dataSet . yProperty ;
575
+ let circleRadius = dataSet . getCircleRadius ( ) ;
576
+ let circleHoleRadius = dataSet . getCircleHoleRadius ( ) ;
577
+ let drawCircleHole = dataSet . isDrawCircleHoleEnabled ( ) && circleHoleRadius < circleRadius && circleHoleRadius > 0 ;
578
+ let drawTransparentCircleHole = drawCircleHole && dataSet . getCircleHoleColor ( ) == ColorTemplate . COLOR_NONE ;
577
579
578
- if ( ! dataSet . isVisible ( ) || ! dataSet . isDrawCirclesEnabled ( ) || dataSet . getEntryCount ( ) == 0 ) continue ;
580
+ let imageCache : DataSetImageCache ;
579
581
580
- this . mCirclePaintInner . setColor ( dataSet . getCircleHoleColor ( ) ) ;
582
+ if ( this . mImageCaches . get ( dataSet ) ) {
583
+ imageCache = this . mImageCaches . get ( dataSet ) ;
584
+ } else {
585
+ imageCache = new DataSetImageCache ( ) ;
586
+ this . mImageCaches . set ( dataSet , imageCache ) ;
587
+ }
581
588
582
- const trans = this . mChart . getTransformer ( dataSet . getAxisDependency ( ) ) ;
589
+ let changeRequired = imageCache . init ( dataSet ) ;
583
590
584
- this . mXBounds . set ( this . mChart , dataSet , this . mAnimator ) ;
591
+ // only fill the cache with new bitmaps if a change is required
592
+ if ( changeRequired ) {
593
+ imageCache . fill ( dataSet , this . mRenderPaint , this . mCirclePaintInner , drawCircleHole , drawTransparentCircleHole ) ;
594
+ }
585
595
586
- let circleRadius = dataSet . getCircleRadius ( ) ;
587
- let circleHoleRadius = dataSet . getCircleHoleRadius ( ) ;
588
- let drawCircleHole = dataSet . isDrawCircleHoleEnabled ( ) && circleHoleRadius < circleRadius && circleHoleRadius > 0 ;
589
- let drawTransparentCircleHole = drawCircleHole && dataSet . getCircleHoleColor ( ) == ColorTemplate . COLOR_NONE ;
596
+ let boundsRangeCount = this . mXBounds . range + this . mXBounds . min ;
590
597
591
- let imageCache : DataSetImageCache ;
598
+ for ( let j = this . mXBounds . min ; j <= boundsRangeCount ; j ++ ) {
599
+ let e = dataSet . getEntryForIndex ( j ) ;
592
600
593
- if ( this . mImageCaches . get ( dataSet ) ) {
594
- imageCache = this . mImageCaches . get ( dataSet ) ;
595
- } else {
596
- imageCache = new DataSetImageCache ( ) ;
597
- this . mImageCaches . set ( dataSet , imageCache ) ;
598
- }
601
+ if ( e == null ) break ;
599
602
600
- let changeRequired = imageCache . init ( dataSet ) ;
603
+ this . mCirclesBuffer [ 0 ] = e [ xKey ] ;
604
+ this . mCirclesBuffer [ 1 ] = e [ yKey ] * phaseY ;
601
605
602
- // only fill the cache with new bitmaps if a change is required
603
- if ( changeRequired ) {
604
- imageCache . fill ( dataSet , this . mRenderPaint , this . mCirclePaintInner , drawCircleHole , drawTransparentCircleHole ) ;
605
- }
606
+ trans . pointValuesToPixel ( this . mCirclesBuffer ) ;
606
607
607
- let boundsRangeCount = this . mXBounds . range + this . mXBounds . min ;
608
+ if ( ! this . mViewPortHandler . isInBoundsRight ( this . mCirclesBuffer [ 0 ] ) ) break ;
608
609
609
- for ( let j = this . mXBounds . min ; j <= boundsRangeCount ; j ++ ) {
610
- let e = dataSet . getEntryForIndex ( j ) ;
610
+ if ( ! this . mViewPortHandler . isInBoundsLeft ( this . mCirclesBuffer [ 0 ] ) || ! this . mViewPortHandler . isInBoundsY ( this . mCirclesBuffer [ 1 ] ) ) continue ;
611
611
612
- if ( e == null ) break ;
612
+ let circleBitmap = imageCache . getBitmap ( j ) ;
613
613
614
- this . mCirclesBuffer [ 0 ] = e [ xKey ] ;
615
- this . mCirclesBuffer [ 1 ] = e [ yKey ] * phaseY ;
614
+ if ( circleBitmap != null ) {
615
+ c . drawBitmap ( circleBitmap , this . mCirclesBuffer [ 0 ] - circleRadius , this . mCirclesBuffer [ 1 ] - circleRadius , null ) ;
616
+ }
617
+ }
618
+ }
619
+
620
+ protected drawCircles ( c : Canvas ) {
621
+ this . mRenderPaint . setStyle ( Style . FILL ) ;
616
622
617
- trans . pointValuesToPixel ( this . mCirclesBuffer ) ;
618
623
619
- if ( ! this . mViewPortHandler . isInBoundsRight ( this . mCirclesBuffer [ 0 ] ) ) break ;
624
+ this . mCirclesBuffer [ 0 ] = 0 ;
625
+ this . mCirclesBuffer [ 1 ] = 0 ;
620
626
621
- if ( ! this . mViewPortHandler . isInBoundsLeft ( this . mCirclesBuffer [ 0 ] ) || ! this . mViewPortHandler . isInBoundsY ( this . mCirclesBuffer [ 1 ] ) ) continue ;
627
+ const dataSets = this . mChart . getLineData ( ) . getVisibleDataSets ( ) ;
622
628
623
- let circleBitmap = imageCache . getBitmap ( j ) ;
629
+ for ( let i = 0 ; i < dataSets . length ; i ++ ) {
630
+ const dataSet = dataSets [ i ] ;
624
631
625
- if ( circleBitmap != null ) {
626
- c . drawBitmap ( circleBitmap , this . mCirclesBuffer [ 0 ] - circleRadius , this . mCirclesBuffer [ 1 ] - circleRadius , null ) ;
627
- }
628
- }
632
+ if ( ! dataSet . isDrawCirclesEnabled ( ) || dataSet . getEntryCount ( ) == 0 ) continue ;
633
+ this . drawCirclesForDataset ( c , dataSet ) ;
629
634
}
630
635
}
631
636
0 commit comments