Skip to content

Commit 5bbea78

Browse files
committed
fix: a lot of fixes and improvements
1 parent 886592f commit 5bbea78

20 files changed

+186
-80
lines changed

src/charting/charts/BarLineChartBase.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
262262

263263
this.drawMarkers(canvas);
264264

265-
this.notify({ eventName: 'drawn', object: this });
265+
this.notify({ eventName: 'postDraw', canvas, object: this });
266266
if (Trace.isEnabled()) {
267267
const drawtime = Date.now() - startTime;
268268
this.totalTime += drawtime;
@@ -610,7 +610,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
610610
this.mViewPortHandler.zoomAtPosition(scaleX, scaleY, x, y, this.mZoomMatrixBuffer);
611611
this.mViewPortHandler.refresh(this.mZoomMatrixBuffer, this, false);
612612

613-
// Range might have changed, which means that Y-axis labels
613+
// Range might have changed, which means that Y-axis labels
614614
// could have changed in size, affecting Y-axis size.
615615
// So we need to recalculate offsets.
616616
this.calculateOffsets();
@@ -640,7 +640,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
640640
*/
641641
public zoomToCenter(scaleX, scaleY) {
642642
const center = this.getCenterOffsets();
643-
644643
const save = this.mZoomMatrixBuffer;
645644
this.mViewPortHandler.zoomAtPosition(scaleX, scaleY, center.x, -center.y, save);
646645
this.mViewPortHandler.refresh(save, this, false);
@@ -678,6 +677,38 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
678677
this.addViewportJob(job);
679678
}
680679

680+
/**
681+
* Zooms by the specified scale factor to the specified values on the specified axis.
682+
*
683+
* @param scaleX
684+
* @param scaleY
685+
* @param xValue
686+
* @param yValue
687+
* @param axis
688+
*/
689+
public zoomAndCenter(scaleX, scaleY, xValue, yValue, axis) {
690+
const origin = this.getValuesByTouchPoint(this.mViewPortHandler.contentLeft(), this.mViewPortHandler.contentTop(), axis);
691+
692+
const job = AnimatedZoomJob.getInstance(
693+
this.mViewPortHandler,
694+
this,
695+
this.getTransformer(axis),
696+
this.getAxis(axis),
697+
this.mXAxis.mAxisRange,
698+
scaleX,
699+
scaleY,
700+
this.mViewPortHandler.getScaleX(),
701+
this.mViewPortHandler.getScaleY(),
702+
xValue,
703+
yValue,
704+
origin.x,
705+
origin.y,
706+
0
707+
);
708+
job.setPhase(1);
709+
job.onAnimationUpdate(0);
710+
}
711+
681712
protected mFitScreenMatrixBuffer = new Matrix();
682713

683714
/**

src/charting/charts/Chart.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
324324
}
325325

326326
public onDraw(canvas: Canvas) {
327-
// super.onDraw(canvas);
327+
super.onDraw(canvas);
328328

329329
if (this.mData === null) {
330330
const hasText = this.mNoDataText && this.mNoDataText.length > 0;
@@ -336,6 +336,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
336336

337337
return;
338338
}
339+
this.notify({ eventName: 'postDraw', object: this, canvas });
339340

340341
// if (!this.mOffsetsCalculated) {
341342
this.calculateOffsets(false);
@@ -567,6 +568,22 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
567568
return null;
568569
} else return this.getHighlighter().getHighlight(x, y);
569570
}
571+
/**
572+
* Returns the Highlight object (contains x-index and DataSet index) of the
573+
* selected value at the given touch polet inside the Line-, Scatter-, or
574+
* CandleStick-Chart.
575+
*
576+
* @param x
577+
* @param y
578+
* @return
579+
*/
580+
public getHighlightByXValue(xValue) {
581+
if (this.mData == null) {
582+
return null;
583+
} else {
584+
return this.getHighlighter().getHighlightsAtXValue(xValue);
585+
}
586+
}
570587

571588
// /**
572589
// * Set a new (e.g. custom) ChartTouchListener NOTE: make sure to
@@ -1436,6 +1453,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
14361453
}
14371454

14381455
this.mJobs = [];
1456+
this.notify({ eventName: 'sizeChanged', object: this });
14391457
}
14401458
public onLayout(left: number, top: number, right: number, bottom: number) {
14411459
super.onLayout(left, top, right, bottom);

src/charting/charts/LineChart.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { Highlight } from '../highlight/Highlight';
88
import { Canvas, Paint, Path } from '@nativescript-community/ui-canvas';
99

1010
export interface CustomRenderer {
11-
drawLine: (c: Canvas, line: Path, paint: Paint) => void;
12-
drawHighlight: (c: Canvas, e: Highlight, set: LineDataSet, paint: Paint) => void;
11+
drawLine?: (c: Canvas, line: Path, paint: Paint) => void;
12+
drawHighlight?: (c: Canvas, e: Highlight, set: LineDataSet, paint: Paint) => void;
1313
}
1414

1515
export class LineChart extends BarLineChartBase<Entry, LineDataSet, LineData> implements LineDataProvider {

src/charting/charts/PieChart.ts

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class PieChart extends PieRadarChartBase<Entry, PieDataSet, PieData> {
140140
const average = this.totalTime / this.drawCycles;
141141
CLog(CLogTypes.log, this.constructor.name, 'Drawtime: ' + drawtime + ' ms, average: ' + average + ' ms, cycles: ' + this.drawCycles);
142142
}
143+
this.notify({ eventName: 'postDraw', object: this, canvas });
143144
}
144145

145146
public calculateOffsets() {

src/charting/components/AxisBase.ts

+42
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LimitLine } from './LimitLine';
44
import { DefaultAxisValueFormatter } from '../formatter/DefaultAxisValueFormatter';
55
import { IAxisValueFormatter } from '../formatter/IAxisValueFormatter';
66
import { ValueFormatter } from '../formatter/ValueFormatter';
7+
import { CustomRenderer } from '../renderer/AxisRenderer';
78

89
/**
910
* Base-class of all axes (previously called labels).
@@ -68,6 +69,9 @@ export abstract class AxisBase extends ComponentBase {
6869
*/
6970
protected mGranularityEnabled = false;
7071

72+
protected mForcedIntervalEnabled = false;
73+
protected mForcedInterval = -1;
74+
7175
/**
7276
* if true, the set number of y-labels will be forced
7377
*/
@@ -181,6 +185,8 @@ export abstract class AxisBase extends ComponentBase {
181185
*/
182186
public mIgnoreOffsets = false;
183187

188+
protected mCustomRenderer: CustomRenderer;
189+
184190
/**
185191
* default constructor
186192
*/
@@ -380,6 +386,29 @@ export abstract class AxisBase extends ComponentBase {
380386
return this.mLabelCount;
381387
}
382388

389+
/**
390+
* @return true if granularity is enabled
391+
*/
392+
public isForceIntervalEnabled() {
393+
return this.mForcedIntervalEnabled;
394+
}
395+
/**
396+
* Set a forced interval.
397+
*
398+
* @param interval
399+
*/
400+
public setForcedInterval(interval) {
401+
this.mForcedInterval = interval;
402+
// set this to true if it was disabled, as it makes no sense to call this method with forcedInterval disabled
403+
this.mForcedIntervalEnabled = true;
404+
}
405+
/**
406+
* @return the force interval
407+
*/
408+
public getForcedInterval() {
409+
return this.mForcedInterval;
410+
}
411+
383412
/**
384413
* @return true if granularity is enabled
385414
*/
@@ -869,4 +898,17 @@ export abstract class AxisBase extends ComponentBase {
869898
public setLabelTextAlign(value: Align) {
870899
this.mLabelTextAlign = value;
871900
}
901+
902+
/**
903+
* set a custom line renderer
904+
*/
905+
public setCustomRenderer(renderer: CustomRenderer) {
906+
this.mCustomRenderer = renderer;
907+
}
908+
/**
909+
* get the custom line renderer
910+
*/
911+
public getCustomRenderer() {
912+
return this.mCustomRenderer;
913+
}
872914
}

src/charting/components/LimitLine.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ export class LimitLine extends ComponentBase {
6767
* @param width
6868
*/
6969
public setLineWidth(width) {
70-
// if (width < 0.2) width = 0.2;
71-
// if (width > 12.0) width = 12.0;
72-
this.mLineWidth = (width);
70+
this.mLineWidth = width;
7371
}
7472

7573
/**

src/charting/data/LineRadarDataSet.ts

-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ export abstract class LineRadarDataSet<T extends Entry> extends LineScatterCandl
102102
* @param width
103103
*/
104104
public setLineWidth(width) {
105-
if (width < 0.0) width = 0.0;
106-
if (width > 10.0) width = 10.0;
107-
// this.mLineWidth = width;
108105
this.mLineWidth = width;
109106
}
110107

src/charting/highlight/ChartHighlighter.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
2626
public getHighlight(x, y) {
2727
const pos = this.getValsForTouch(x, y);
2828
const xVal = pos.x;
29-
// MPPointD.recycleInstance(pos);
3029

3130
const high = this.getHighlightForX(xVal, x, y);
3231
return high;
@@ -107,7 +106,7 @@ export class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
107106
* @param xVal the transformed x-value of the x-touch position
108107
* @return
109108
*/
110-
protected getHighlightsAtXValue(xVal, x, y) {
109+
public getHighlightsAtXValue(xVal, x?, y?) {
111110
this.mHighlightBuffer = [];
112111

113112
const data = this.getData();
@@ -170,7 +169,7 @@ export class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
170169
xPx: pixels.x,
171170
yPx: pixels.y,
172171
dataSetIndex,
173-
axis: set.getAxisDependency(),
172+
axis: set.getAxisDependency()
174173
});
175174
}
176175

src/charting/highlight/CombinedHighlighter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class CombinedHighlighter extends ChartHighlighter<CombinedDataProvider>
2121
this.barHighlighter = barChart.getBarData() == null ? null : new BarHighlighter(barChart);
2222
}
2323

24-
protected getHighlightsAtXValue(xVal, x, y) {
24+
public getHighlightsAtXValue(xVal, x?, y?) {
2525
this.mHighlightBuffer = [];
2626

2727
const dataObjects = this.mChart.getCombinedData().getAllData();
@@ -30,7 +30,7 @@ export class CombinedHighlighter extends ChartHighlighter<CombinedDataProvider>
3030
const dataObject = dataObjects[i];
3131

3232
// in case of BarData, let the BarHighlighter take over
33-
if (this.barHighlighter != null && dataObject instanceof BarData) {
33+
if (this.barHighlighter != null && dataObject instanceof BarData && x !== undefined) {
3434
const high = this.barHighlighter.getHighlight(x, y);
3535

3636
if (high != null) {

src/charting/highlight/IHighlighter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export interface IHighlighter {
1212
* @return
1313
*/
1414
getHighlight(x, y): Highlight;
15+
getHighlightsAtXValue(xVal, x?, y?): Highlight[];
1516
}

src/charting/highlight/PieRadarHighlighter.ts

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export abstract class PieRadarHighlighter<E extends Entry, D extends DataSet<E>,
1717
constructor(chart: T) {
1818
this.mChart = chart;
1919
}
20+
getHighlightsAtXValue(xVal, x?, y?): Highlight[] {
21+
// not implemented
22+
return null;
23+
}
2024

2125
public getHighlight(x: number, y: number): Highlight {
2226
const touchDistanceToCenter = this.mChart.distanceToCenter(x, y);

src/charting/jobs/AnimatedViewPortJob.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,10 @@ export abstract class AnimatedViewPortJob extends ViewPortJob implements Animato
2323
protected xOrigin;
2424
protected yOrigin;
2525

26-
constructor(viewPortHandler: ViewPortHandler, xValue, yValue, trans: Transformer, v: BarLineChartBase<any, any, any>, xOrigin, yOrigin, duration) {
26+
constructor(viewPortHandler: ViewPortHandler, xValue, yValue, trans: Transformer, v: BarLineChartBase<any, any, any>, xOrigin, yOrigin, private duration) {
2727
super(viewPortHandler, xValue, yValue, trans, v);
2828
this.xOrigin = xOrigin;
2929
this.yOrigin = yOrigin;
30-
this.createAnimator(duration);
31-
// this.animator = ObjectAnimator.ofFloat(this, "phase", 0, 1);
32-
// animator.setDuration(duration);
33-
// animator.addUpdateListener(this);
34-
// animator.addListener(this);
3530
}
3631

3732
createAnimator(duration) {
@@ -47,6 +42,9 @@ export abstract class AnimatedViewPortJob extends ViewPortJob implements Animato
4742
}
4843

4944
public run() {
45+
if (!this.animator) {
46+
this.createAnimator(this.duration);
47+
}
5048
this.animator.start();
5149
}
5250

src/charting/listener/BarLineChartTouchListener.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ export class BarLineChartTouchListener extends ChartTouchListener<BarLineChartBa
508508
// }
509509
}
510510
this.mMatrix.postTranslate(distanceX, distanceY);
511-
511+
this.mChart.notify({ eventName: 'translate', object: this.mChart, distanceX, distanceY });
512512
// if (l != null)
513513
// l.onChartTranslate(event, distanceX, distanceY);
514514
}

src/charting/renderer/AxisRenderer.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Renderer } from './Renderer';
22
import { AxisBase } from '../components/AxisBase';
3-
import { Align, Canvas, Paint, Style } from '@nativescript-community/ui-canvas';
3+
import { Align, Canvas, Paint, Path, RectF, Style } from '@nativescript-community/ui-canvas';
44
import { ViewPortHandler } from '../utils/ViewPortHandler';
55
import { Transformer } from '../utils/Transformer';
66
import { Utils } from '../utils/Utils';
7-
import { profile } from '@nativescript/core/profiling';
7+
8+
export type CustomRendererGridLineFunction = (c: Canvas, renderer: AxisRenderer, rect: RectF, x, y, axisValue, paint: Paint) => void;
9+
export interface CustomRenderer {
10+
drawGridLine?: CustomRendererGridLineFunction;
11+
}
12+
813
/**
914
* Baseclass of all axis renderers.
1015
*
@@ -153,7 +158,7 @@ export abstract class AxisRenderer extends Renderer {
153158

154159
// Find out how much spacing (in y value space) between axis values
155160
const rawInterval = range / labelCount;
156-
let interval = Utils.roundToNextSignificant(rawInterval);
161+
let interval = axis.isForceIntervalEnabled() ? axis.getForcedInterval() : Utils.roundToNextSignificant(rawInterval);
157162

158163
// If granularity is enabled, then do not allow the interval to go below specified granularity.
159164
// This is used to avoid repeated values when rounding values for display.
@@ -198,7 +203,7 @@ export abstract class AxisRenderer extends Renderer {
198203
// if we use Math.ceil(yMin / interval) * interval and the min value is 20
199204
// then we will see 0 as axis first when it should be 20
200205
// let first = interval === 0 ? 0 : Math.ceil(yMin / interval) * interval;
201-
let first = interval === 0 ? 0 : yMin;
206+
let first = interval === 0 ? 0 : Math.ceil(yMin / interval) * interval;
202207
if (axis.isCenterAxisLabelsEnabled()) {
203208
first -= interval;
204209
}

src/charting/renderer/LineChartRenderer.ts

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export class LineChartRenderer extends LineRadarRenderer {
185185

186186
protected drawDataSet(c: Canvas, dataSet: LineDataSet): boolean {
187187
if (dataSet.getEntryCount() < 1) return false;
188+
console.log('drawDataSet', dataSet.getLineWidth());
188189
this.mRenderPaint.setStrokeWidth(dataSet.getLineWidth());
189190
this.mRenderPaint.setPathEffect(dataSet.getDashPathEffect());
190191
if (dataSet.getNbColors() === 1) {

src/charting/renderer/LineRadarRenderer.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ export abstract class LineRadarRenderer extends LineScatterCandleRadarRenderer {
7373

7474
this.mRenderPaint.setStyle(Style.FILL);
7575

76+
this.mRenderPaint.setColor(color);
7677
if (shader) {
77-
this.mRenderPaint.setColor('black');
7878
this.mRenderPaint.setShader(shader);
79-
} else {
80-
this.mRenderPaint.setColor(color);
8179
}
8280

8381
if (this.clipPathSupported()) {

0 commit comments

Comments
 (0)