forked from yannrichet/jmathplot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLabel.java
More file actions
151 lines (117 loc) · 2.96 KB
/
Label.java
File metadata and controls
151 lines (117 loc) · 2.96 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
package org.math.plot.plotObjects;
import java.awt.*;
import org.math.plot.render.*;
/**
* BSD License
*
* @author Yann RICHET
*/
public class Label implements Plotable {
protected double[] coord;
protected double[] base_offset;
protected String label;
protected Color color;
protected double cornerN = 0.5;
protected double cornerE = 0.5;
boolean visible = true;
public double angle = 0;
public Font font = AbstractDrawer.DEFAULT_FONT;
// private static DecimalFormat dec = new DecimalFormat("##0.###E0");
public Label(String l, Color col, double... c) {
label = l;
coord = c;
color = col;
}
public Label(String l, double... c) {
this(l, AbstractDrawer.DEFAULT_COLOR, c);
}
/**
* show coord itself
*/
public Label(double... c) {
this(coordToString(c), AbstractDrawer.DEFAULT_COLOR, c);
}
public void setText(String _t) {
label = _t;
}
public String getText() {
return label;
}
public void setCoord(double... _c) {
coord = _c;
}
public void setColor(Color c) {
color = c;
}
public Color getColor() {
return color;
}
/**
* reference point center: 0.5, 0.5 lowerleft: 0,0 upperleft 1, 0 ...
*/
public void setCorner(double north_south, double east_west) {
cornerN = north_south;
cornerE = east_west;
}
public void setVisible(boolean v) {
visible = v;
}
public boolean getVisible() {
return visible;
}
/**
* shift by given screen coordinates offset
*/
/*
* public void setOffset(double[] offset) { double[] newCoord =
* coord.getPlotCoordCopy(); for (int i = 0; i < newCoord.length; i++) {
* newCoord[i] += offset[i]; } coord.setPlotCoord(newCoord); }
*/
/**
* see Text for formatted text output
*/
public void plot(AbstractDrawer draw) {
if (!visible) return;
draw.setColor(color);
draw.setFont(font);
draw.setBaseOffset(base_offset);
draw.setTextOffset(cornerE, cornerN);
draw.setTextAngle(angle);
draw.drawText(label, coord);
draw.setBaseOffset(null);
}
public void rotate(double _angle) {
angle = _angle;
}
public void setFont(Font _font) {
font = _font;
}
public static double approx(double val, int decimal) {
// double timesEn = val*Math.pow(10,decimal);
// if (Math.rint(timesEn) == timesEn) {
// return val;
// } else {
// to limit precision loss, you need to separate cases where decimal<0
// and >0
// if you don't you'll have this : approx(10000.0,-4) => 10000.00000001
if (decimal < 0) {
return Math.rint(val / Math.pow(10, -decimal)) * Math.pow(10, -decimal);
} else {
return Math.rint(val * Math.pow(10, decimal)) / Math.pow(10, decimal);
}
// }
}
public static String coordToString(double... c) {
StringBuffer sb = new StringBuffer("(");
for (int i = 0; i < c.length; i++)
sb.append(approx(c[i], 2)).append(",");
// sb.append(dec.format(c.getPlotCoordCopy()[i])).append(",");
sb.setLength(sb.length() - 1);
if (sb.length() > 0)
sb.append(")");
return sb.toString();
}
public Font getFont() {
return font;
}
}