forked from yannrichet/jmathplot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScatterPlot.java
More file actions
213 lines (179 loc) · 6.31 KB
/
ScatterPlot.java
File metadata and controls
213 lines (179 loc) · 6.31 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
package org.math.plot.plots;
import java.awt.*;
import javax.swing.*;
import org.math.plot.*;
import org.math.plot.canvas.PlotCanvas;
import org.math.plot.render.*;
import org.math.plot.utils.Array;
import org.math.plot.utils.FastMath;
public class ScatterPlot extends Plot {
private int type;
private int radius;
private boolean[][] pattern;
private boolean use_pattern;
double[][] XY;
private String[] tags;
public ScatterPlot(String n, Color c, boolean[][] _pattern, double[][] _XY) {
this(n, new Color[] { c }, _pattern, _XY);
}
public ScatterPlot(String n, Color[] c, boolean[][] _pattern, double[][] _XY) {
super(n, c);
XY = _XY;
use_pattern = true;
pattern = _pattern;
}
public ScatterPlot(String n, Color c, int _type, int _radius, double[][] _XY) {
this(n, new Color[] { c }, _type, _radius, _XY);
}
public ScatterPlot(String n, Color[] c, int _type, int _radius, double[][] _XY) {
super(n, c);
XY = _XY;
use_pattern = false;
type = _type;
radius = _radius;
}
public ScatterPlot(String n, Color c, double[][] _XY) {
this(n, c, AbstractDrawer.ROUND_DOT, AbstractDrawer.DEFAULT_DOT_RADIUS, _XY);
}
public ScatterPlot(String n, Color[] c, double[][] _XY) {
this(n, c, AbstractDrawer.ROUND_DOT, AbstractDrawer.DEFAULT_DOT_RADIUS, _XY);
}
public void plot(AbstractDrawer draw, Color[] c) {
if (!visible) {
return;
}
boolean monoColor = false;
if (c.length == 1) {
monoColor = true;
}
else if (c.length != XY.length) {
throw new IllegalArgumentException("Color array length must match length of data array. ");
}
if (use_pattern) {
draw.setDotType(AbstractDrawer.PATTERN_DOT);
draw.setDotPattern(pattern);
} else {
draw.setDotRadius(radius);
if (type == AbstractDrawer.CROSS_DOT) {
draw.setDotType(AbstractDrawer.CROSS_DOT);
} else {
draw.setDotType(AbstractDrawer.ROUND_DOT);
}
}
for (int i = 0; i < XY.length; i++) {
draw.setColor(monoColor ? c[0] : c[i]);
draw.drawDot(XY[i]);
}
}
public void setDotPattern(int t) {
type = t;
use_pattern = false;
}
public void setDotPattern(boolean[][] t) {
use_pattern = true;
pattern = t;
}
@Override
public void setData(double[][] d) {
datapanel = null;
XY = d;
}
@Override
public double[][] getData() {
return XY;
}
@Override
public double[][] getBounds() {
return Array.mergeRows(Array.min(XY), Array.max(XY));
}
public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
for (int i = 0; i < XY.length; i++) {
int[] screenCoord = draw.project(XY[i]);
if (FastMath.abs(screenCoord[0] - screenCoordTest[0]) < note_precision && FastMath.abs(screenCoord[1] - screenCoordTest[1]) < note_precision) {
return XY[i];
}
}
return null;
}
public static void main(String[] args) {
Plot2DPanel p2 = new Plot2DPanel();
for (int i = 0; i < 3; i++) {
double[][] XYZ = new double[10][2];
for (int j = 0; j < XYZ.length; j++) {
XYZ[j][0] = /*1 + */ Math.random();
XYZ[j][1] = /*100 * */ Math.random();
}
p2.addScatterPlot("toto" + i, XYZ);
}
p2.setLegendOrientation(PlotPanel.SOUTH);
new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Plot3DPanel p = new Plot3DPanel();
String[] tags = null;
for (int i = 0; i < 3; i++) {
double[][] XYZ = new double[10][3];
tags = new String[10];
for (int j = 0; j < XYZ.length; j++) {
XYZ[j][0] = /*1 +*/ 2.5 * Math.random();
XYZ[j][1] = /*100 **/ Math.random();
XYZ[j][2] = /*0.0001 **/ Math.random();
tags[j] = "tags " + j;
}
p.addScatterPlot("toto" + i, XYZ);
}
((ScatterPlot) p.getPlot(0)).setTags(tags);
p.setLegendOrientation(PlotPanel.SOUTH);
new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color[] c = new Color[10];
p2 = new Plot2DPanel();
double[][] XYZ = new double[10][2];
for (int j = 0; j < XYZ.length; j++) {
XYZ[j][0] = /*1 + */ Math.random();
XYZ[j][1] = /*100 * */ Math.random();
c[j] = new Color((int)(Math.random() * 0x1000000));
}
p2.addScatterPlot("toto", c, XYZ);
p2.setLegendOrientation(PlotPanel.SOUTH);
new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new Plot3DPanel();
XYZ = new double[10][3];
tags = new String[10];
for (int j = 0; j < XYZ.length; j++) {
XYZ[j][0] = /*1 +*/ 2.5 * Math.random();
XYZ[j][1] = /*100 **/ Math.random();
XYZ[j][2] = /*0.0001 **/ Math.random();
tags[j] = "tags " + j;
c[j] = new Color((int)(Math.random() * 0x1000000));
}
p.addScatterPlot("toto", c, XYZ);
((ScatterPlot) p.getPlot(0)).setTags(tags);
p.setLegendOrientation(PlotPanel.SOUTH);
new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param tags the tags to set
*/
public void setTags(String[] tags) {
datapanel = null;
this.tags = tags;
}
@Override
public void noteCoord(AbstractDrawer draw, double[] coordNoted) {
if (coordNoted == null) {
return;
}
if (tags == null) {
super.noteCoord(draw, coordNoted);
} else {
draw.setColor(PlotCanvas.NOTE_COLOR);
for (int i = 0; i < XY.length; i++) {
if (tags.length > i) {
if (Array.equals(XY[i], coordNoted)) {
draw.drawShadowedText(tags[i], .5f, coordNoted);
}
}
}
}
//draw.drawCoordinate(coordNoted);
//draw.drawText(Array.cat(draw.canvas.reverseMapedData(coordNoted)), coordNoted);
}
}