Skip to content

Commit 2b866b7

Browse files
committed
chore: refactoring
1 parent f5c0d6d commit 2b866b7

File tree

1 file changed

+84
-79
lines changed

1 file changed

+84
-79
lines changed

Diff for: src/charting/renderer/LineChartRenderer.ts

+84-79
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LineRadarRenderer } from './LineRadarRenderer';
22
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';
44
import { ImageSource } from '@nativescript/core/image-source/image-source';
55
import { ChartAnimator } from '../animation/ChartAnimator';
66
import { ViewPortHandler } from '../utils/ViewPortHandler';
@@ -487,56 +487,57 @@ export class LineChartRenderer extends LineRadarRenderer {
487487
private mLineBuffer: number[];
488488

489489
@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);
493494

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());
499496

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;
502499

503-
const trans = this.mChart.getTransformer(dataSet.getAxisDependency());
500+
if (!dataSet.isDrawCirclesEnabled()) valOffset = valOffset / 2;
504501

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);
507503

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();
509506

510-
this.mXBounds.set(this.mChart, dataSet, this.mAnimator);
507+
const iconsOffset = Object.assign({}, dataSet.getIconsOffset());
511508

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];
514512

515-
const iconsOffset = Object.assign({}, dataSet.getIconsOffset());
513+
if (!this.mViewPortHandler.isInBoundsRight(x)) break;
516514

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;
520516

521-
if (!this.mViewPortHandler.isInBoundsRight(x)) break;
517+
let entry = dataSet.getEntryForIndex(j / 2 + this.mXBounds.min);
518+
if (!entry) continue;
522519

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+
}
524523

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;
527526

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+
}
531531

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();
534535

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];
538538

539-
// MPPointF.recycleInstance(iconsOffset);
539+
if (!this.shouldDrawValues(dataSet) || dataSet.getEntryCount() < 1) continue;
540+
this.drawValuesForDataset(c, dataSet);
540541
}
541542
}
542543
}
@@ -560,72 +561,76 @@ export class LineChartRenderer extends LineRadarRenderer {
560561
*/
561562
private mCirclesBuffer = Utils.createNativeArray(2);
562563

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());
566567
let phaseY = this.mAnimator.getPhaseY();
567568

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());
570572

571-
const dataSets = this.mChart.getLineData().getDataSets();
573+
this.mXBounds.set(this.mChart, dataSet, this.mAnimator);
572574

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;
577579

578-
if (!dataSet.isVisible() || !dataSet.isDrawCirclesEnabled() || dataSet.getEntryCount() == 0) continue;
580+
let imageCache: DataSetImageCache;
579581

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+
}
581588

582-
const trans = this.mChart.getTransformer(dataSet.getAxisDependency());
589+
let changeRequired = imageCache.init(dataSet);
583590

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+
}
585595

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;
590597

591-
let imageCache: DataSetImageCache;
598+
for (let j = this.mXBounds.min; j <= boundsRangeCount; j++) {
599+
let e = dataSet.getEntryForIndex(j);
592600

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;
599602

600-
let changeRequired = imageCache.init(dataSet);
603+
this.mCirclesBuffer[0] = e[xKey];
604+
this.mCirclesBuffer[1] = e[yKey] * phaseY;
601605

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);
606607

607-
let boundsRangeCount = this.mXBounds.range + this.mXBounds.min;
608+
if (!this.mViewPortHandler.isInBoundsRight(this.mCirclesBuffer[0])) break;
608609

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;
611611

612-
if (e == null) break;
612+
let circleBitmap = imageCache.getBitmap(j);
613613

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);
616622

617-
trans.pointValuesToPixel(this.mCirclesBuffer);
618623

619-
if (!this.mViewPortHandler.isInBoundsRight(this.mCirclesBuffer[0])) break;
624+
this.mCirclesBuffer[0] = 0;
625+
this.mCirclesBuffer[1] = 0;
620626

621-
if (!this.mViewPortHandler.isInBoundsLeft(this.mCirclesBuffer[0]) || !this.mViewPortHandler.isInBoundsY(this.mCirclesBuffer[1])) continue;
627+
const dataSets = this.mChart.getLineData().getVisibleDataSets();
622628

623-
let circleBitmap = imageCache.getBitmap(j);
629+
for (let i = 0; i < dataSets.length; i++) {
630+
const dataSet = dataSets[i];
624631

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);
629634
}
630635
}
631636

0 commit comments

Comments
 (0)