forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYAxis.java
More file actions
570 lines (484 loc) · 17.7 KB
/
YAxis.java
File metadata and controls
570 lines (484 loc) · 17.7 KB
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
/****************************************************************************************
* Copyright (c) 2014 Michael Goldbach <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
package com.wildplot.android.rendering;
import com.wildplot.android.rendering.graphics.wrapper.FontMetricsWrap;
import com.wildplot.android.rendering.graphics.wrapper.GraphicsWrap;
import com.wildplot.android.rendering.graphics.wrapper.RectangleWrap;
import com.wildplot.android.rendering.interfaces.Drawable;
import java.text.DecimalFormat;
/**
* This Class represents a Drawable x-axis
*
*
*/
public class YAxis implements Drawable {
private boolean mHasNumbersRotated = false;
private float maxTextWidth = 0;
private boolean isIntegerNumbering = false;
private boolean isOnRightSide = false;
private boolean isLog = false;
public boolean hasVariableLimits = true;
private boolean isAutoTic = false;
/**
* the offset of the x-axis to prevent drawing numbering on x-axis
*/
private double yOffset = 0;
/**
* offset to move axis left or right
*/
private double xOffset = 0;
private String name = "Y";
private boolean mHasName = false;
/**
* Format that is used to print numbers under markers
*/
private DecimalFormat df = new DecimalFormat( "##0.0#" );
private DecimalFormat dfScience = new DecimalFormat( "0.0###E0" );
private DecimalFormat dfInteger = new DecimalFormat( "#.#" );
private boolean isScientific = false;
/**
* the PlotSheet object the x-axis is drawn onto
*/
private PlotSheet plotSheet;
/**
* the start of x-axis marker, used for relative alignment of further marks
*/
private double ticStart = 0;
/**
* the space between two marks
*/
private double tic = 1;
/**
* the space between two minor marks
*/
private double minorTic= 0.5;
/**
* the estimated size between two mayor tics in auto tic mode
*/
private float pixelDistance = 25;
/**
* the estimated size between two minor tics in auto tic mode
*/
private float minorPixelDistance = 25;
/**
* start of drawn x-axis
*/
private double start = 0;
/**
* end of drawn x-axis
*/
private double end = 100;
/**
* true if the marker should be drawn into the direction above the axis
*/
private boolean markOnLeft = true;
/**
* true if the marker should be drawn into the direction under the axis
*/
private boolean markOnRight = true;
/**
* length of a marker in pixel, length is only for one side
*/
private float markerLength = 5;
private boolean isOnFrame = false;
/**
* Constructor for an Y-axis object
* @param plotSheet the sheet the axis will be drawn onto
* @param ticStart the start of the axis markers used for relative alignment of other markers
* @param tic the space between two markers
*/
public YAxis(PlotSheet plotSheet, double ticStart, double tic, double minorTic ) {
this.plotSheet = plotSheet;
this.ticStart = ticStart;
this.tic = tic;
this.minorTic=minorTic;
this.pixelDistance = (int)Math.round((plotSheet.getyRange()[1] - plotSheet.getyRange()[0])/tic);
}
/**
* Constructor for an Y-axis object this instance uses autocalculation of tics with a given pixeldistance
* @param plotSheet the sheet the axis will be drawn onto
* @param ticStart the start of the axis markers used for relative alignment of other markers
*/
public YAxis(PlotSheet plotSheet, double ticStart, float pixelDistance , float minorPixelDistance) {
this.plotSheet = plotSheet;
this.ticStart = ticStart;
this.pixelDistance = pixelDistance;
this.minorPixelDistance = minorPixelDistance;
this.isAutoTic = true;
}
/*
* (non-Javadoc)
* @see rendering.Drawable#paint(java.awt.Graphics)
*/
public void paint(GraphicsWrap g) {
RectangleWrap field = g.getClipBounds();
if(this.hasVariableLimits){
start = plotSheet.getyRange()[0];
end = plotSheet.getyRange()[1];
}
if(this.isOnFrame)
//xOffset = plotSheet.getxRange()[0];
if(this.isAutoTic) {
this.tic = plotSheet.ticsCalcY(pixelDistance, field);
double quotient = (pixelDistance*1.0)/(minorPixelDistance*1.0);
this.minorTic = (this.tic / Math.round(quotient));
}
else {
this.pixelDistance = Math.abs(plotSheet.yToGraphic(0, field) - plotSheet.yToGraphic(tic, field));
this.minorPixelDistance = Math.abs(plotSheet.yToGraphic(0, field) - plotSheet.yToGraphic(minorTic, field));
}
if(this.tic < 1e-2 || this.tic > 1e2)
this.isScientific = true;
//vertikale Linie
float[] coordStart = plotSheet.toGraphicPoint(xOffset, start, field);
float[] coordEnd = plotSheet.toGraphicPoint(xOffset, end, field);
if(!this.isOnFrame)
g.drawLine(coordStart[0], coordStart[1], coordEnd[0], coordEnd[1]);
drawMarkers(g);
drawMinorMarkers(g);
}
/**
* draw markers on the axis
* @param g graphic object used for drawing
*/
private void drawMarkers(GraphicsWrap g) {
RectangleWrap field = g.getClipBounds();
float cleanSpace = 17; // space in pixel that will be unmarked on the end of the axis for arrow and description
float tics = (int)((this.ticStart - this.start)/tic);
double leftStart = this.ticStart - this.tic*tics;
double logStart = 0;
if(this.isLog) {
logStart = Math.ceil(Math.log10(this.start));
leftStart = Math.pow(10, logStart++);
}
double currentY = leftStart;
while(currentY <= this.end) {
if((!this.isOnFrame &&plotSheet.yToGraphic(currentY, field) >= plotSheet.yToGraphic(this.end, field) +cleanSpace
&& plotSheet.yToGraphic(currentY, field) <= plotSheet.yToGraphic(this.start, field) - cleanSpace
&& plotSheet.yToGraphic(currentY, field) <= field.y + field.height - cleanSpace
&& plotSheet.yToGraphic(currentY, field) >= field.y + cleanSpace) ||
(this.isOnFrame && currentY <= this.plotSheet.getyRange()[1] &&
currentY >= this.plotSheet.getyRange()[0])){
if(this.markOnRight ) {
drawRightMarker(g, field, currentY);
}
if(this.markOnLeft ) {
drawLeftMarker(g, field, currentY);
}
if(!(Math.abs(currentY) < yOffset+0.001 && !this.isOnFrame)) {
if(isOnRightSide)
drawNumberingOnRightSide(g,field,currentY);
else
drawNumbering(g, field, currentY);
}
}
if(this.isLog) {
currentY = Math.pow(10, logStart++);
}else {
currentY += this.tic;
}
}
FontMetricsWrap fm = g.getFontMetrics( g.getFont() );
float width = fm.stringWidth(this.name);
//arrow
float[] arowheadPos = {plotSheet.xToGraphic(xOffset, field), (plotSheet.getyRange()[1] >= this.end)? plotSheet.yToGraphic( this.end, field): plotSheet.yToGraphic(plotSheet.getyRange()[1], field) };
if(!this.isOnFrame) {
g.drawLine(arowheadPos[0]-1, arowheadPos[1]+1, arowheadPos[0]-3, arowheadPos[1]+6);
g.drawLine(arowheadPos[0]+1, arowheadPos[1]+1, arowheadPos[0]+3, arowheadPos[1]+6);
if(mHasName)
g.drawString(this.name, arowheadPos[0]-13-width, arowheadPos[1] + 10);
} else {
// AffineTransform fontAT = new AffineTransform();
// Font oldFont = g.getFont();
// fontAT.rotate(-Math.PI/2.0);
// Font newFont = oldFont.deriveFont(fontAT);
// g.setFont(newFont);
float spacerValue = maxTextWidth;
if(mHasNumbersRotated)
spacerValue = g.getFontMetrics().getHeight();
g.save();
if(isOnRightSide) {
float[] middlePosition = {plotSheet.xToGraphic(xOffset, field), plotSheet.yToGraphic(0, field)};
g.rotate(90, middlePosition[0]+spacerValue*1.4f, field.height / 2 - width / 2);
if(mHasName)
g.drawString(this.name, middlePosition[0]+spacerValue*1.4f, field.height / 2 - width / 2);
}else {
float[] middlePosition = {plotSheet.xToGraphic(xOffset, field), plotSheet.yToGraphic(0, field)};
g.rotate(-90, middlePosition[0]-spacerValue*1.4f, field.height / 2 + width / 2);
if(mHasName)
g.drawString(this.name, middlePosition[0]-spacerValue*1.4f, field.height / 2 + width / 2);
}
g.restore();
// g.setFont(oldFont);
}
//axis name
}
/**
* draw number left to a marker
* @param g graphic object used for drawing
* @param field bounds of plot
* @param y position of number
*/
private void drawNumbering(GraphicsWrap g, RectangleWrap field, double y) {
if(this.tic < 1 && Math.abs(ticStart-y) < this.tic*this.tic)
y = ticStart;
float[] coordStart = plotSheet.toGraphicPoint(xOffset, y, field);
FontMetricsWrap fm = g.getFontMetrics( g.getFont() );
float fontHeight = fm.getHeight(true);
String font = df.format(y);
float width = fm.stringWidth(font);
if(this.isScientific && !isIntegerNumbering){
font = dfScience.format(y);
width = fm.stringWidth(font);
}else if(isIntegerNumbering){
font = dfInteger.format(y);
width = fm.stringWidth(font);
}
g.save();
if(mHasNumbersRotated) {
float[] middlePosition = {plotSheet.xToGraphic(xOffset, field), plotSheet.yToGraphic(y, field)};
g.rotate(-90, middlePosition[0]-width*0.1f, middlePosition[1]+ width / 2.0f);
g.drawString(font, middlePosition[0]-width*0.1f, middlePosition[1]+ width / 2.0f );
}else
g.drawString(font, Math.round(coordStart[0]-width*1.1f), Math.round(coordStart[1] + fontHeight*0.4f) );
g.restore();
if(width > maxTextWidth)
maxTextWidth = width;
//g.drawString(df.format(y), coordStart[0]-33, coordStart[1]+4);
}
/**
* draw number left to a marker
* @param g graphic object used for drawing
* @param field bounds of plot
* @param y position of number
*/
private void drawNumberingOnRightSide(GraphicsWrap g, RectangleWrap field, double y) {
if(this.tic < 1 && Math.abs(ticStart-y) < this.tic*this.tic)
y = ticStart;
float[] coordStart = plotSheet.toGraphicPoint(xOffset, y, field);
FontMetricsWrap fm = g.getFontMetrics( g.getFont() );
float fontHeight = fm.getHeight(true);
String font = df.format(y);
float width = fm.stringWidth(font);
g.save();
if(this.isScientific && !isIntegerNumbering){
font = dfScience.format(y);
width = fm.stringWidth(font);
}else if(isIntegerNumbering){
font = dfInteger.format(y);
width = fm.stringWidth(font);
}
if(mHasNumbersRotated) {
float[] middlePosition = {plotSheet.xToGraphic(xOffset, field), plotSheet.yToGraphic(y, field)};
g.rotate(90, middlePosition[0]+width*0.1f, middlePosition[1]- width / 2.0f);
g.drawString(font, middlePosition[0]+width*0.1f, middlePosition[1]- width / 2.0f );
}else
g.drawString(font, Math.round(coordStart[0] + width * 0.1f), Math.round(coordStart[1] + fontHeight * 0.4f));
g.restore();
if(width > maxTextWidth)
maxTextWidth = width;
//g.drawString(df.format(y), coordStart[0]-33, coordStart[1]+4);
}
/**
* draws an left marker
* @param g graphic object used for drawing
* @param field bounds of plot
* @param y position of marker
*/
private void drawLeftMarker(GraphicsWrap g, RectangleWrap field, double y){
float[] coordStart = plotSheet.toGraphicPoint(xOffset, y, field);
float[] coordEnd = {coordStart[0] - this.markerLength, coordStart[1]};
g.drawLine(coordStart[0], coordStart[1], coordEnd[0], coordEnd[1]);
}
/**
* draws an right marker
* @param g graphic object used for drawing
* @param field bounds of plot
* @param y position of marker
*/
private void drawRightMarker(GraphicsWrap g, RectangleWrap field, double y){
float[] coordStart = plotSheet.toGraphicPoint(xOffset, y, field);
float[] coordEnd = {coordStart[0] + this.markerLength+1, coordStart[1]};
g.drawLine(coordStart[0], coordStart[1], coordEnd[0], coordEnd[1]);
}
/**
* get the offset of this axis
* @return the offset of this axis
*/
public double getxOffset() {
return xOffset;
}
/**
* set the offset of this axis
* @param yOffset new offset
*/
public void setxOffset(double yOffset) {
this.xOffset = yOffset;
}
/**
* set offset back to zero for normal axis behavior
*/
public void unsetxOffset() {
this.xOffset = 0;
}
/**
* set the y coordinate where the x axis goes through this axis
* @param yOffset offset of the x-axis
*/
public void setIntersectionOffset(double yOffset) {
this.yOffset = yOffset;
}
/**
* set the axis to draw on the border between outer frame and plot
*/
public void setOnFrame() {
this.isOnFrame = true;
xOffset = plotSheet.getxRange()[0];
markOnLeft = false;
}
/**
* unset the axis to draw on the border between outer frame and plot
*/
public void unsetOnFrame() {
this.isOnFrame = false;
xOffset = 0;
markOnLeft = true;
markOnRight = true;
isOnRightSide = false;
}
public void setOnRightSideFrame() {
this.isOnFrame = true;
xOffset = plotSheet.getxRange()[1];
markOnRight = false;
isOnRightSide = true;
}
/*
* (non-Javadoc)
* @see rendering.Drawable#isOnFrame()
*/
public boolean isOnFrame() {
return isOnFrame;
}
/**
* set name of axis
* @param name name of axis
*/
public void setName(String name) {
this.name = name;
if(name.equals(""))
mHasName = false;
else
mHasName = true;
}
private void drawMinorMarkers(GraphicsWrap g) {
RectangleWrap field = g.getClipBounds();
int cleanSpace = 17; // space in pixel that will be unmarked on the end of the axis for arrow and description
int tics = (int)((this.ticStart - this.start)/tic);
double leftStart = this.ticStart - this.tic*tics;
int factor = 1;
double logStart = 0;
if(this.isLog) {
logStart = Math.floor(Math.log10(this.start));
leftStart = Math.pow(10, logStart) * factor++;
}
double currentY = leftStart;
while(currentY <= this.end) {
if((!this.isOnFrame &&plotSheet.yToGraphic(currentY, field) >= plotSheet.yToGraphic(this.end, field) +cleanSpace
&& plotSheet.yToGraphic(currentY, field) <= plotSheet.yToGraphic(this.start, field) - cleanSpace
&& plotSheet.yToGraphic(currentY, field) <= field.y + field.height - cleanSpace
&& plotSheet.yToGraphic(currentY, field) >= field.y + cleanSpace) ||
(this.isOnFrame && currentY <= this.plotSheet.getyRange()[1] &&
currentY >= this.plotSheet.getyRange()[0])){
if(this.markOnRight ) {
drawRightMinorMarker(g, field, currentY);
}
if(this.markOnLeft ) {
drawLeftMinorMarker(g, field, currentY);
}
}
if(this.isLog) {
if(factor == 10)
{
factor = 1;
logStart++;
}
currentY = Math.pow(10, logStart)*factor++;
}else {
currentY += minorTic;
}
}
}
// /**
// * caculates minor tics
// * @param g graphic object used for drawing
// * @return returns the new minor tics in percent
// */
//
// private double calcMinorTics(Graphics g)
// {
// double percentMinorTic=this.minorTic/this.tic;
// return percentMinorTic;
// }
/**
* draws an left minor marker
* @param g graphic object used for drawing
* @param field bounds of plot
* @param y position of marker
*/
private void drawLeftMinorMarker(GraphicsWrap g, RectangleWrap field, double y){
float[] coordStart = plotSheet.toGraphicPoint(xOffset, y, field);
float[] coordEnd = {(float) (coordStart[0] - 0.5*this.markerLength), coordStart[1]};
g.drawLine(coordStart[0], coordStart[1], coordEnd[0], coordEnd[1]);
}
/**
* draws an right minor marker
* @param g graphic object used for drawing
* @param field bounds of plot
* @param y position of marker
*/
private void drawRightMinorMarker(GraphicsWrap g, RectangleWrap field, double y){
float[] coordStart = plotSheet.toGraphicPoint(xOffset, y, field);
float[] coordEnd = {(float) (coordStart[0] + 0.5*this.markerLength+1), coordStart[1]};
g.drawLine(coordStart[0], coordStart[1], coordEnd[0], coordEnd[1]);
}
public void setIntegerNumbering(boolean isIntegerNumbering){
this.isIntegerNumbering = isIntegerNumbering;
}
public void setLog(){
this.isLog = true;
}
public void unsetLog(){
this.isLog = false;
}
@Override
public void abortAndReset() {
// TODO Auto-generated method stub
}
@Override
public boolean isClusterable() {
return true;
}
@Override
public boolean isCritical() {
return true;
}
public void setHasNumbersRotated(){
mHasNumbersRotated = true;
}
public void unsetHasNumbersRotated(){
mHasNumbersRotated = false;
}
}