/
XYGraphWidget.java
1656 lines (1433 loc) · 57.3 KB
/
XYGraphWidget.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012 AndroidPlot.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.androidplot.xy;
import android.graphics.*;
import com.androidplot.exception.PlotRenderException;
import com.androidplot.ui.LayoutManager;
import com.androidplot.ui.RenderStack;
import com.androidplot.ui.SizeMetrics;
import com.androidplot.ui.widget.Widget;
import com.androidplot.util.*;
import java.text.DecimalFormat;
import java.text.Format;
/**
* Displays graphical data annotated with domain and range tick markers.
*/
public class XYGraphWidget extends Widget {
public float getRangeLabelOrientation() {
return rangeLabelOrientation;
}
public void setRangeLabelOrientation(float rangeLabelOrientation) {
this.rangeLabelOrientation = rangeLabelOrientation;
}
public float getDomainLabelOrientation() {
return domainLabelOrientation;
}
public void setDomainLabelOrientation(float domainLabelOrientation) {
this.domainLabelOrientation = domainLabelOrientation;
}
/**
*
* @return
* @deprecated Since 0.6.2; Use {@link #getDomainTickLabelPaintMap()}.
*/
@Deprecated
public Mapping<Paint, Number> getDomainLabelPaintMap() {
return getDomainTickLabelPaintMap();
}
public Mapping<Paint, Number> getDomainTickLabelPaintMap() {
return domainTickLabelPaintMap;
}
/**
* Set a mapping to override the Paint used to draw domain labels. The mapping should
* return null for values that should not be overridden.
* @param domainLabelPaintMap
* @deprecated Since 0.6.2; Use {@link #setDomainTickLabelPaintMap(com.androidplot.util.Mapping)}.
*/
@Deprecated
public void setDomainLabelPaintMap(Mapping<Paint, Number> domainLabelPaintMap) {
setDomainTickLabelPaintMap(domainLabelPaintMap);
}
/**
* Set a mapping to override the Paint used to draw domain labels. The mapping should
* return null for values that should not be overridden.
* @param domainTickLabelPaintMap
*/
public void setDomainTickLabelPaintMap(Mapping<Paint, Number> domainTickLabelPaintMap) {
this.domainTickLabelPaintMap = domainTickLabelPaintMap;
}
public Mapping<Paint, Number> getRangeTickLabelPaintMap() {
return rangeTickLabelPaintMap;
}
/**
*
* @return
* @deprecated Since 0.6.2; Use {@link #getRangeTickLabelPaintMap()}.
*/
@Deprecated
public Mapping<Paint, Number> getRangeLabelPaintMap() {
return rangeTickLabelPaintMap;
}
/**
* Set a mapping to override the Paint used to draw range labels. The mapping should
* return null for values that should not be overridden.
* @param rangeLabelTickPaintMap
*/
public void setRangeLabelTickPaintMap(Mapping<Paint, Number> rangeLabelTickPaintMap) {
this.rangeTickLabelPaintMap = rangeLabelTickPaintMap;
}
/**
*
* @param rangeLabelPaintMap
* @deprecated Since 0.6.2; Use {@link #setRangeLabelTickPaintMap(com.androidplot.util.Mapping)}.
*/
@Deprecated
public void setRangeLabelPaintMap(Mapping<Paint, Number> rangeLabelPaintMap) {
setRangeLabelTickPaintMap(rangeLabelPaintMap);
}
/**
* Will be used in a future version.
*/
public enum XYPlotOrientation {
HORIZONTAL, VERTICAL
}
private static final int MARKER_LABEL_SPACING = 2;
// space between cursor lines and label in pixels
private static final int CURSOR_LABEL_SPACING = 2;
// max size of domain tick labels in pixels
private float domainTickLabelWidth = 15;
// ...
private float rangeTickLabelWidth = 41;
private float domainTickLabelVerticalOffset = -5;
private float domainTickLabelHorizontalOffset = 0.0f;
// allows tweaking of text position
private float rangeTickLabelHorizontalOffset = 1.0f;
// allows tweaking of text position
private float rangeTickLabelVerticalOffset = 0.0f;
private int ticksPerRangeLabel = 1;
private int ticksPerDomainLabel = 1;
private float gridPaddingTop = 0;
private float gridPaddingBottom = 0;
private float gridPaddingLeft = 0;
private float gridPaddingRight = 0;
private int domainTickExtension = 5;
private int rangeTickExtension = 5;
private int domainLabelSubTickExtension = 0;
private int rangeLabelSubTickExtension = 0;
private Paint gridBackgroundPaint;
private Paint rangeGridLinePaint;
private Paint rangeSubGridLinePaint;
private Paint domainGridLinePaint;
private Paint domainSubGridLinePaint;
private Paint domainTickLabelPaint;
private Paint rangeTickLabelPaint;
private Paint domainCursorPaint;
private Paint rangeCursorPaint;
private Paint cursorLabelPaint;
private Paint cursorLabelBackgroundPaint;
private XYPlot plot;
private Format rangeValueFormat;
private Format domainValueFormat;
private Paint domainOriginLinePaint;
private Paint rangeOriginLinePaint;
private Paint domainOriginTickLabelPaint;
private Paint rangeOriginTickLabelPaint;
private RectF gridRect;
private RectF paddedGridRect;
private float domainCursorPosition;
private float rangeCursorPosition;
@SuppressWarnings("FieldCanBeLocal")
private boolean drawCursorLabelEnabled = true;
private boolean drawMarkersEnabled = true;
private boolean rangeAxisLeft = true;
private boolean domainAxisBottom = true;
private boolean rangeTick = true;
private boolean rangeSubTick = true;
private boolean domainTick = true;
private boolean domainSubTick = true;
private float rangeLabelOrientation;
private float domainLabelOrientation;
private Mapping<Paint, Number> domainTickLabelPaintMap;
private Mapping<Paint, Number> rangeTickLabelPaintMap;
private ZHash<RectRegion, AxisValueLabelFormatter> axisValueLabelRegions;
private RenderStack<? extends XYSeries, ? extends XYSeriesFormatter> renderStack;
{
gridBackgroundPaint = new Paint();
gridBackgroundPaint.setColor(Color.rgb(140, 140, 140));
gridBackgroundPaint.setStyle(Paint.Style.FILL);
rangeGridLinePaint = new Paint();
rangeGridLinePaint.setColor(Color.rgb(180, 180, 180));
rangeGridLinePaint.setAntiAlias(true);
rangeGridLinePaint.setStyle(Paint.Style.STROKE);
domainGridLinePaint = new Paint(rangeGridLinePaint);
domainSubGridLinePaint = new Paint(domainGridLinePaint);
rangeSubGridLinePaint = new Paint(rangeGridLinePaint);
domainOriginLinePaint = new Paint();
domainOriginLinePaint.setColor(Color.WHITE);
domainOriginLinePaint.setAntiAlias(true);
rangeOriginLinePaint = new Paint();
rangeOriginLinePaint.setColor(Color.WHITE);
rangeOriginLinePaint.setAntiAlias(true);
domainOriginTickLabelPaint = new Paint();
domainOriginTickLabelPaint.setColor(Color.WHITE);
domainOriginTickLabelPaint.setAntiAlias(true);
domainOriginTickLabelPaint.setTextAlign(Paint.Align.CENTER);
rangeOriginTickLabelPaint = new Paint();
rangeOriginTickLabelPaint.setColor(Color.WHITE);
rangeOriginTickLabelPaint.setAntiAlias(true);
rangeOriginTickLabelPaint.setTextAlign(Paint.Align.RIGHT);
domainTickLabelPaint = new Paint();
domainTickLabelPaint.setColor(Color.LTGRAY);
domainTickLabelPaint.setAntiAlias(true);
domainTickLabelPaint.setTextAlign(Paint.Align.CENTER);
rangeTickLabelPaint = new Paint();
rangeTickLabelPaint.setColor(Color.LTGRAY);
rangeTickLabelPaint.setAntiAlias(true);
rangeTickLabelPaint.setTextAlign(Paint.Align.RIGHT);
domainCursorPaint = new Paint();
domainCursorPaint.setColor(Color.YELLOW);
rangeCursorPaint = new Paint();
rangeCursorPaint.setColor(Color.YELLOW);
cursorLabelPaint = new Paint();
cursorLabelPaint.setColor(Color.YELLOW);
cursorLabelBackgroundPaint = new Paint();
cursorLabelBackgroundPaint.setColor(Color.argb(100, 50, 50, 50));
setMarginTop(7);
setMarginRight(4);
setMarginBottom(4);
rangeValueFormat = new DecimalFormat("0.0");
domainValueFormat = new DecimalFormat("0.0");
axisValueLabelRegions = new ZHash<RectRegion, AxisValueLabelFormatter>();
}
public XYGraphWidget(LayoutManager layoutManager, XYPlot plot, SizeMetrics sizeMetrics) {
super(layoutManager, sizeMetrics);
this.plot = plot;
renderStack = new RenderStack(plot);
}
public ZIndexable<RectRegion> getAxisValueLabelRegions() {
return axisValueLabelRegions;
}
/**
* Add a new Region used for rendering axis valuelabels. Note that it is
* possible to add multiple Region instances which overlap, in which cast
* the last region to be added will be used. It is up to the developer to
* guard against this often undesireable situation.
*
* @param region
* @param formatter
*/
public void addAxisValueLabelRegion(RectRegion region,
AxisValueLabelFormatter formatter) {
axisValueLabelRegions.addToTop(region, formatter);
}
/**
* Convenience method - wraps addAxisValueLabelRegion, using
* Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY to mask off range
* axis value labels.
*
* @param min
* @param max
* @param formatter
*
*/
public void addDomainAxisValueLabelRegion(double min, double max,
AxisValueLabelFormatter formatter) {
addAxisValueLabelRegion(new RectRegion(min, max,
Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, null),
formatter);
}
/**
* Convenience method - wraps addAxisValueLabelRegion, using
* Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY to mask off domain
* axis value labels.
*
* @param min
* @param max
* @param formatter
*/
public void addRangeAxisValueLabelRegion(double min, double max,
AxisValueLabelFormatter formatter) {
addAxisValueLabelRegion(new RectRegion(Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, min, max, null), formatter);
}
/**
* Returns the formatter associated with the first (bottom) Region
* containing x and y.
*
* @param x
* @param y
* @return the formatter associated with the first (bottom) region
* containing x and y. null otherwise.
*/
public AxisValueLabelFormatter getAxisValueLabelFormatterForVal(double x,
double y) {
for (RectRegion r : axisValueLabelRegions.elements()) {
if (r.containsValue(x, y)) {
return axisValueLabelRegions.get(r);
}
}
return null;
}
public AxisValueLabelFormatter getAxisValueLabelFormatterForDomainVal(
double val) {
for (RectRegion r : axisValueLabelRegions.elements()) {
if (r.containsDomainValue(val)) {
return axisValueLabelRegions.get(r);
}
}
return null;
}
public AxisValueLabelFormatter getAxisValueLabelFormatterForRangeVal(
double val) {
for (RectRegion r : axisValueLabelRegions.elements()) {
if (r.containsRangeValue(val)) {
return axisValueLabelRegions.get(r);
}
}
return null;
}
/**
* Returns the formatter associated with the first (bottom-most) Region
* containing value.
*
* @param value
* @return
*/
/*
* public AxisValueLabelFormatter getXYAxisFormatterForRangeVal(double
* value) { return getRegionContainingVal(rangeLabelRegions, value); }
*//**
* Returns the formatter associated with the first (bottom-most) Region
* containing value.
*
* @param value
* @return
*/
/*
* public AxisValueLabelFormatter getXYAxisFormatterForDomainVal(double
* value) { return getRegionContainingVal(domainLabelRegions, value); }
*/
/*
* private AxisValueLabelFormatter getRegionContainingVal(ZHash<LineRegion,
* AxisValueLabelFormatter> zhash, double val) { for (LineRegion r :
* zhash.elements()) { if (r.contains(val)) { return
* rangeLabelRegions.get(r); } } // nothing found return null; }
*/
/**
* Returns a RectF representing the grid area last drawn by this plot.
*
* @return
*/
public RectF getGridRect() {
return paddedGridRect;
}
private String getFormattedRangeValue(Number value) {
return rangeValueFormat.format(value);
}
private String getFormattedDomainValue(Number value) {
return domainValueFormat.format(value);
}
/**
* Convenience method. Wraps getYVal(float)
*
* @param point
* @return
*/
public Double getYVal(PointF point) {
return getYVal(point.y);
}
/**
* Converts a y pixel to a y value.
*
* @param yPix
* @return
*/
public Double getYVal(float yPix) {
if (plot.getCalculatedMinY() == null
|| plot.getCalculatedMaxY() == null) {
return null;
}
return ValPixConverter.pixToVal(yPix - paddedGridRect.top, plot
.getCalculatedMinY().doubleValue(), plot.getCalculatedMaxY()
.doubleValue(), paddedGridRect.height(), true);
}
/**
* Convenience method. Wraps getXVal(float)
*
* @param point
* @return
*/
public Double getXVal(PointF point) {
return getXVal(point.x);
}
/**
* Converts an x pixel into an x value.
*
* @param xPix
* @return
*/
public Double getXVal(float xPix) {
if (plot.getCalculatedMinX() == null
|| plot.getCalculatedMaxX() == null) {
return null;
}
return ValPixConverter.pixToVal(xPix - paddedGridRect.left, plot
.getCalculatedMinX().doubleValue(), plot.getCalculatedMaxX()
.doubleValue(), paddedGridRect.width(), false);
}
@Override
protected void doOnDraw(Canvas canvas, RectF widgetRect)
throws PlotRenderException {
gridRect = getGridRect(widgetRect); // used for drawing the background
// of the grid
paddedGridRect = getPaddedGridRect(gridRect); // used for drawing lines
// etc.
//Log.v(TAG, "gridRect :" + gridRect);
//Log.v(TAG, "paddedGridRect :" + paddedGridRect);
// if (!plot.isEmpty()) {
// don't draw if we have no space to draw into
if ((paddedGridRect.height() > 0.0f) && (paddedGridRect.width() > 0.0f)) {
if (plot.getCalculatedMinX() != null
&& plot.getCalculatedMaxX() != null
&& plot.getCalculatedMinY() != null
&& plot.getCalculatedMaxY() != null) {
drawGrid(canvas);
drawData(canvas);
drawCursors(canvas);
if (isDrawMarkersEnabled()) {
drawMarkers(canvas);
}
}
}
// }
}
private RectF getGridRect(RectF widgetRect) {
return new RectF(widgetRect.left + ((rangeAxisLeft)?rangeTickLabelWidth:1),
widgetRect.top + ((domainAxisBottom)?1:domainTickLabelWidth),
widgetRect.right - ((rangeAxisLeft)?1:rangeTickLabelWidth),
widgetRect.bottom - ((domainAxisBottom)?domainTickLabelWidth:1));
}
private RectF getPaddedGridRect(RectF gridRect) {
return new RectF(gridRect.left + gridPaddingLeft, gridRect.top
+ gridPaddingTop, gridRect.right - gridPaddingRight,
gridRect.bottom - gridPaddingBottom);
}
private void drawTickText(Canvas canvas, XYAxisType axis, Number value,
float xPix, float yPix, Paint labelPaint) {
AxisValueLabelFormatter rf = null;
String txt = null;
double v = value.doubleValue();
int canvasState = canvas.save();
try {
switch (axis) {
case DOMAIN:
rf = getAxisValueLabelFormatterForDomainVal(v);
txt = getFormattedDomainValue(value);
canvas.rotate(getDomainLabelOrientation(), xPix, yPix);
break;
case RANGE:
rf = getAxisValueLabelFormatterForRangeVal(v);
txt = getFormattedRangeValue(value);
canvas.rotate(getRangeLabelOrientation(), xPix, yPix);
break;
}
// if a matching region formatter was found, create a clone
// of labelPaint and use the formatter's color. Otherwise
// just use labelPaint:
Paint p;
if (rf != null) {
p = new Paint(labelPaint);
p.setColor(rf.getColor());
} else {
p = labelPaint;
}
canvas.drawText(txt, xPix, yPix, p);
} finally {
canvas.restoreToCount(canvasState);
}
}
private void drawDomainTick(Canvas canvas, float xPix, Number xVal,
Paint labelPaint, Paint linePaint, boolean drawLineOnly) {
if (!drawLineOnly) {
if (linePaint != null && (domainTick || domainTickExtension > 0)) {
if (domainAxisBottom){
canvas.drawLine(xPix, domainTick ? gridRect.top : gridRect.bottom,
xPix, gridRect.bottom + domainTickExtension, linePaint);
} else {
canvas.drawLine(xPix, gridRect.top - domainTickExtension,
xPix, domainTick ? gridRect.bottom : gridRect.top, linePaint);
}
}
if (labelPaint != null) {
float fontHeight = FontUtils.getFontHeight(labelPaint);
float yPix;
if (domainAxisBottom){
yPix = gridRect.bottom + domainTickExtension
+ domainTickLabelVerticalOffset + fontHeight;
} else {
yPix = gridRect.top - domainTickExtension
- domainTickLabelVerticalOffset;
}
drawTickText(canvas, XYAxisType.DOMAIN, xVal,
xPix + domainTickLabelHorizontalOffset, yPix,
labelPaint);
}
} else if (linePaint != null && (domainSubTick || domainLabelSubTickExtension > 0)) {
if (domainAxisBottom){
canvas.drawLine(xPix, domainSubTick ? gridRect.top : gridRect.bottom,
xPix, gridRect.bottom + domainLabelSubTickExtension, linePaint);
} else {
canvas.drawLine(xPix, gridRect.top - domainLabelSubTickExtension,
xPix, domainSubTick ? gridRect.bottom : gridRect.top, linePaint);
}
}
}
public void drawRangeTick(Canvas canvas, float yPix, Number yVal,
Paint labelPaint, Paint linePaint, boolean drawLineOnly) {
if (!drawLineOnly) {
if (linePaint != null && (rangeTick || rangeTickExtension > 0)) {
if (rangeAxisLeft){
canvas.drawLine(gridRect.left - rangeTickExtension, yPix,
rangeTick ? gridRect.right : gridRect.left, yPix, linePaint);
} else {
canvas.drawLine(rangeTick ? gridRect.left : gridRect.right, yPix,
gridRect.right + rangeTickExtension, yPix, linePaint);
}
}
if (labelPaint != null) {
float xPix;
if (rangeAxisLeft){
xPix = gridRect.left
- (rangeTickExtension + rangeTickLabelHorizontalOffset);
} else {
xPix = gridRect.right
+ (rangeTickExtension + rangeTickLabelHorizontalOffset);
}
drawTickText(canvas, XYAxisType.RANGE, yVal, xPix, yPix - rangeTickLabelVerticalOffset,
labelPaint);
}
} else if (linePaint != null && (rangeSubTick || rangeLabelSubTickExtension > 0)) {
if (rangeAxisLeft){
canvas.drawLine(gridRect.left - rangeLabelSubTickExtension, yPix,
rangeSubTick ? gridRect.right : gridRect.left, yPix, linePaint);
} else {
canvas.drawLine(rangeTick ? gridRect.left : gridRect.right, yPix,
gridRect.right + rangeLabelSubTickExtension, yPix, linePaint);
}
}
}
/**
* Draws the drid and domain/range labels for the plot.
*
* @param canvas
*/
protected void drawGrid(Canvas canvas) {
if (gridBackgroundPaint != null) {
canvas.drawRect(gridRect, gridBackgroundPaint);
}
float domainOriginF;
if (plot.getDomainOrigin() != null) {
double domainOriginVal = plot.getDomainOrigin().doubleValue();
domainOriginF = ValPixConverter.valToPix(domainOriginVal, plot
.getCalculatedMinX().doubleValue(), plot
.getCalculatedMaxX().doubleValue(), paddedGridRect.width(),
false);
domainOriginF += paddedGridRect.left;
// if no origin is set, use the leftmost value visible on the grid:
} else {
domainOriginF = paddedGridRect.left;
}
XYStep domainStep = XYStepCalculator.getStep(plot, XYAxisType.DOMAIN,
paddedGridRect, plot.getCalculatedMinX().doubleValue(), plot
.getCalculatedMaxX().doubleValue());
// draw domain origin:
if (domainOriginF >= paddedGridRect.left
&& domainOriginF <= paddedGridRect.right) {
if (domainOriginLinePaint != null){
domainOriginLinePaint.setTextAlign(Paint.Align.CENTER);
}
Paint olp = domainTickLabelPaintMap != null ?
domainTickLabelPaintMap.get(plot.getDomainOrigin()) : domainOriginTickLabelPaint;
if(olp == null) {
olp = domainTickLabelPaint;
}
drawDomainTick(canvas, domainOriginF, plot.getDomainOrigin()
.doubleValue(), olp, domainOriginLinePaint, false);
}
// draw ticks LEFT of origin:
{
int i = 1;
double xVal;
float xPix = domainOriginF - domainStep.getStepPix();
for (; xPix >= paddedGridRect.left; xPix = domainOriginF
- (i * domainStep.getStepPix())) {
xVal = plot.getDomainOrigin().doubleValue() - i
* domainStep.getStepVal();
Paint dlp = domainTickLabelPaintMap != null ?
domainTickLabelPaintMap.get(xVal) : domainTickLabelPaint;
if(dlp == null) {
dlp = domainTickLabelPaint;
}
if (xPix >= paddedGridRect.left && xPix <= paddedGridRect.right) {
if (i % getTicksPerDomainLabel() == 0) {
drawDomainTick(canvas, xPix, xVal, dlp, domainGridLinePaint, false);
} else {
drawDomainTick(canvas, xPix, xVal, dlp, domainSubGridLinePaint, true);
}
}
i++;
}
}
// draw ticks RIGHT of origin:
{
int i = 1;
double xVal;
float xPix = domainOriginF + domainStep.getStepPix();
for (; xPix <= paddedGridRect.right; xPix = domainOriginF
+ (i * domainStep.getStepPix())) {
xVal = plot.getDomainOrigin().doubleValue() + i
* domainStep.getStepVal();
Paint dlp = domainTickLabelPaintMap != null ?
domainTickLabelPaintMap.get(xVal) : domainTickLabelPaint;
if(dlp == null) {
dlp = domainTickLabelPaint;
}
if (xPix >= paddedGridRect.left && xPix <= paddedGridRect.right) {
if (i % getTicksPerDomainLabel() == 0) {
drawDomainTick(canvas, xPix, xVal, dlp, domainGridLinePaint, false);
} else {
drawDomainTick(canvas, xPix, xVal, dlp, domainSubGridLinePaint, true);
}
}
i++;
}
}
// draw range origin:
float rangeOriginF;
if (plot.getRangeOrigin() != null) {
// --------- NEW WAY ------
double rangeOriginD = plot.getRangeOrigin().doubleValue();
rangeOriginF = ValPixConverter.valToPix(rangeOriginD, plot
.getCalculatedMinY().doubleValue(), plot
.getCalculatedMaxY().doubleValue(),
paddedGridRect.height(), true);
rangeOriginF += paddedGridRect.top;
// if no origin is set, use the leftmost value visible on the grid
} else {
rangeOriginF = paddedGridRect.bottom;
}
XYStep rangeStep = XYStepCalculator.getStep(plot, XYAxisType.RANGE,
paddedGridRect, plot.getCalculatedMinY().doubleValue(), plot
.getCalculatedMaxY().doubleValue());
// draw range origin:
if (rangeOriginF >= paddedGridRect.top
&& rangeOriginF <= paddedGridRect.bottom) {
if (rangeOriginLinePaint != null){
rangeOriginLinePaint.setTextAlign(Paint.Align.RIGHT);
}
Paint olp = rangeTickLabelPaintMap != null ?
rangeTickLabelPaintMap.get(plot.getRangeOrigin()) : rangeOriginTickLabelPaint;
if(olp == null) {
olp = rangeTickLabelPaint;
}
drawRangeTick(canvas, rangeOriginF, plot.getRangeOrigin()
.doubleValue(), olp,
rangeOriginLinePaint, false);
}
// draw ticks ABOVE origin:
{
int i = 1;
double yVal;
float yPix = rangeOriginF - rangeStep.getStepPix();
for (; yPix >= paddedGridRect.top; yPix = rangeOriginF
- (i * rangeStep.getStepPix())) {
yVal = plot.getRangeOrigin().doubleValue() + i
* rangeStep.getStepVal();
Paint rlp = rangeTickLabelPaintMap != null ?
rangeTickLabelPaintMap.get(yVal) : rangeTickLabelPaint;
if (rlp == null) {
rlp = rangeTickLabelPaint;
}
if (yPix >= paddedGridRect.top && yPix <= paddedGridRect.bottom) {
if (i % getTicksPerRangeLabel() == 0) {
drawRangeTick(canvas, yPix, yVal, rlp,
rangeGridLinePaint, false);
} else {
drawRangeTick(canvas, yPix, yVal, rlp,
rangeSubGridLinePaint, true);
}
}
i++;
}
}
// draw ticks BENEATH origin:
{
int i = 1;
double yVal;
float yPix = rangeOriginF + rangeStep.getStepPix();
for (; yPix <= paddedGridRect.bottom; yPix = rangeOriginF
+ (i * rangeStep.getStepPix())) {
yVal = plot.getRangeOrigin().doubleValue() - i
* rangeStep.getStepVal();
Paint rlp = rangeTickLabelPaintMap != null ?
rangeTickLabelPaintMap.get(yVal) : rangeTickLabelPaint;
if (rlp == null) {
rlp = rangeTickLabelPaint;
}
if (yPix >= paddedGridRect.top && yPix <= paddedGridRect.bottom) {
if (i % getTicksPerRangeLabel() == 0) {
drawRangeTick(canvas, yPix, yVal, rlp,
rangeGridLinePaint, false);
} else {
drawRangeTick(canvas, yPix, yVal, rlp,
rangeSubGridLinePaint, true);
}
}
i++;
}
}
}
/**
* Renders the text associated with user defined markers
*
* @param canvas
* @param text
* @param marker
* @param x
* @param y
*/
private void drawMarkerText(Canvas canvas, String text, ValueMarker marker,
float x, float y) {
x += MARKER_LABEL_SPACING;
y -= MARKER_LABEL_SPACING;
RectF textRect = new RectF(FontUtils.getStringDimensions(text,
marker.getTextPaint()));
textRect.offsetTo(x, y - textRect.height());
if (textRect.right > paddedGridRect.right) {
textRect.offset(-(textRect.right - paddedGridRect.right), 0);
}
if (textRect.top < paddedGridRect.top) {
textRect.offset(0, paddedGridRect.top - textRect.top);
}
canvas.drawText(text, textRect.left, textRect.bottom,
marker.getTextPaint());
}
protected void drawMarkers(Canvas canvas) {
for (YValueMarker marker : plot.getYValueMarkers()) {
if (marker.getValue() != null) {
double yVal = marker.getValue().doubleValue();
float yPix = ValPixConverter.valToPix(yVal, plot
.getCalculatedMinY().doubleValue(), plot
.getCalculatedMaxY().doubleValue(), paddedGridRect
.height(), true);
yPix += paddedGridRect.top;
canvas.drawLine(paddedGridRect.left, yPix,
paddedGridRect.right, yPix, marker.getLinePaint());
// String text = getFormattedRangeValue(yVal);
float xPix = marker.getTextPosition().getPixelValue(
paddedGridRect.width());
xPix += paddedGridRect.left;
if (marker.getText() != null) {
drawMarkerText(canvas, marker.getText(), marker, xPix, yPix);
} else {
drawMarkerText(canvas,
getFormattedRangeValue(marker.getValue()), marker,
xPix, yPix);
}
}
}
for (XValueMarker marker : plot.getXValueMarkers()) {
if (marker.getValue() != null) {
double xVal = marker.getValue().doubleValue();
float xPix = ValPixConverter.valToPix(xVal, plot
.getCalculatedMinX().doubleValue(), plot
.getCalculatedMaxX().doubleValue(), paddedGridRect
.width(), false);
xPix += paddedGridRect.left;
canvas.drawLine(xPix, paddedGridRect.top, xPix,
paddedGridRect.bottom, marker.getLinePaint());
// String text = getFormattedDomainValue(xVal);
float yPix = marker.getTextPosition().getPixelValue(
paddedGridRect.height());
yPix += paddedGridRect.top;
if (marker.getText() != null) {
drawMarkerText(canvas, marker.getText(), marker, xPix, yPix);
} else {
drawMarkerText(canvas,
getFormattedDomainValue(marker.getValue()), marker,
xPix, yPix);
}
}
}
}
protected void drawCursors(Canvas canvas) {
boolean hasDomainCursor = false;
// draw the domain cursor:
if (domainCursorPaint != null
&& domainCursorPosition <= paddedGridRect.right
&& domainCursorPosition >= paddedGridRect.left) {
hasDomainCursor = true;
canvas.drawLine(domainCursorPosition, paddedGridRect.top,
domainCursorPosition, paddedGridRect.bottom,
domainCursorPaint);
}
boolean hasRangeCursor = false;
// draw the range cursor:
if (rangeCursorPaint != null
&& rangeCursorPosition >= paddedGridRect.top
&& rangeCursorPosition <= paddedGridRect.bottom) {
hasRangeCursor = true;
canvas.drawLine(paddedGridRect.left, rangeCursorPosition,
paddedGridRect.right, rangeCursorPosition, rangeCursorPaint);
}
if (drawCursorLabelEnabled && cursorLabelPaint != null
&& hasRangeCursor && hasDomainCursor) {
String label = "X="
+ getDomainValueFormat().format(getDomainCursorVal());
label += " Y=" + getRangeValueFormat().format(getRangeCursorVal());
// convert the label dimensions rect into floating-point:
RectF cursorRect = new RectF(FontUtils.getPackedStringDimensions(
label, cursorLabelPaint));
cursorRect.offsetTo(domainCursorPosition, rangeCursorPosition
- cursorRect.height());
// if we are too close to the right edge of the plot, we will move
// the
// label to the left side of our cursor:
if (cursorRect.right >= paddedGridRect.right) {
cursorRect.offsetTo(domainCursorPosition - cursorRect.width(),
cursorRect.top);
}
// same thing for the top edge of the plot:
// dunno why but these rects can have negative values for top and
// bottom.
if (cursorRect.top <= paddedGridRect.top) {
cursorRect.offsetTo(cursorRect.left, rangeCursorPosition);
}
if (cursorLabelBackgroundPaint != null) {
canvas.drawRect(cursorRect, cursorLabelBackgroundPaint);
}
canvas.drawText(label, cursorRect.left, cursorRect.bottom,
cursorLabelPaint);
}
}
/**
* Draws lines and points for each element in the series.
*
* @param canvas
* @throws PlotRenderException
*/
protected void drawData(Canvas canvas) throws PlotRenderException {
try {
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.clipRect(gridRect, android.graphics.Region.Op.INTERSECT);
renderStack.sync();
for(RenderStack.StackElement thisElement : renderStack.getElements()) {
if(thisElement.isEnabled()) {
plot.getRenderer(thisElement.getPair().getFormatter().getRendererClass()).
render(canvas, paddedGridRect, thisElement.getPair(), renderStack);
}
}
} finally {
canvas.restore();
}
}
protected void drawPoint(Canvas canvas, PointF point, Paint paint) {
canvas.drawPoint(point.x, point.y, paint);
}
/**
*
* @return
* @deprecated Since 0.6.2; Use {@link #getDomainTickLabelWidth()}.
*/
@Deprecated
public float getDomainLabelWidth() {
return getDomainTickLabelWidth();
}
public float getDomainTickLabelWidth() {
return domainTickLabelWidth;
}
/**
*
* @param domainLabelWidth
* @deprecated Since 0.6.2; Use {@link #setDomainTickLabelWidth(float)}.
*/
@Deprecated
public void setDomainLabelWidth(float domainLabelWidth) {
setDomainTickLabelWidth(domainLabelWidth);
}
public void setDomainTickLabelWidth(float domainTickLabelWidth) {
this.domainTickLabelWidth = domainTickLabelWidth;
}
/**
*
* @return
* @deprecated Since 0.6.2; Use {@link #getRangeTickLabelWidth()}.
*/
@Deprecated
public float getRangeLabelWidth() {
return getRangeTickLabelWidth();
}
public float getRangeTickLabelWidth() {
return rangeTickLabelWidth;