-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplePlotPanel.java
868 lines (708 loc) · 24.5 KB
/
SimplePlotPanel.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
// SimplePlotPanel.java
//
// Rahul Simha
// Spring 2008
import java.util.*;
import java.text.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class SimplePlotPanel extends JPanel {
public static int glueSpace = 3; // Generic spacing between lines, text.
public static int axisLabelFontSize = 10; // Font size for axis labels.
public static int tickFontSize = 8; // Same for tickmark labels.
public static int legendFontSize = 16; // Legend.
public static int tickSize = 6; // Length of each tick mark.
public static int titleFontSize = 12; // Length of each tick mark.
public static int pointBlobRadius = 4; // Size of circle to mark a point.
// Fonts and associated FontMetrics references:
Font legendFont, tickFont, axisLabelFont, titleFont;
FontMetrics tickFontMetrics, axisLabelFontMetrics, legendFontMetrics, titleFontMetrics;
// Various box boundaries:
// The panel:
int panelHeight, panelWidth;
// If an external bounding box is given:
int bboxLeft, bboxRight, bboxTop, bboxBottom, bboxHeight, bboxWidth;
// The inner box in which the curves are plotted:
int plotBoxTopInset, plotBoxBottomInset, plotBoxLeftInset, plotBoxRightInset;
int plotBoxHeight, plotBoxWidth, plotBoxLeft, plotBoxRight, plotBoxTop, plotBoxBottom;
// The outerbox that contains the plotbox and the labels:
int graphBoxHeight, graphBoxWidth, graphBoxLeft, graphBoxRight, graphBoxTop, graphBoxBottom;
// The box below the outerbox that contains the legend:
int legendBoxHeight, legendBoxWidth;
int maxNumCurves; // Max allowable curves: for storage.
int numCurves; // Actual number of curves.
int maxNumValues;
SimplePlotCurve[] curves; // The curves themselves.
double Xmax, Xmin, Xrange; // Lowest, highest, and range of X values.
double Ymax, Ymin, Yrange; // Same for Y.
int numXTicks, numYTicks; // Number of tick marks.
double[] XTicks, YTicks; // The actual points for the ticks.
String[] XTickLabels, YTickLabels; // Labels.
int XTickWidth, XTickHeight; // Font heights and widths.
int YTickWidth, YTickHeight;
String XaxisLabelString = "", YaxisLabelString = ""; // Axis labels.
DecimalFormat Xformat, Yformat; // Format labels/ticks.
String titleString = ""; // Title to appear center-top.
// A set of colors to use.
static Color colors[] = {Color.red, Color.blue, Color.green, Color.magenta, Color.cyan, Color.orange, Color.yellow,
Color.pink, Color.lightGray};
// Parameters to constructor: total number of curves. Over all curves,
// the largest number of X values, the largest number of Y values.
public SimplePlotPanel (int maxNumCurves, int maxNumValues)
{
this.setBackground (Color.white);
// Curve data:
this.maxNumCurves = maxNumCurves;
this.maxNumValues = maxNumValues;
numCurves = 0;
curves = new SimplePlotCurve [maxNumCurves];
for (int i=0; i<maxNumCurves; i++) {
curves[i] = new SimplePlotCurve (maxNumValues);
}
// Range stuff:
Ymax = Xmax = Double.NEGATIVE_INFINITY;
Ymin = Xmin = Double.POSITIVE_INFINITY;
Xrange = Yrange = 0;
// Tick marks:
numXTicks = numYTicks = -1;
XTicks = new double [maxNumValues+1];
YTicks = new double [maxNumValues+1];
XTickLabels = new String [maxNumValues+1];
YTickLabels = new String [maxNumValues+1];
// Font stuff:
legendFont = new Font ("Serif", Font.BOLD | Font.ITALIC, legendFontSize);
axisLabelFont = new Font ("SansSerif", Font.BOLD, axisLabelFontSize);
tickFont = new Font ("SansSerif", Font.PLAIN, tickFontSize);
titleFont = new Font ("Serif", Font.BOLD, titleFontSize);
}
// Plot one curve only
static JFrame frame;
public static void makePlotFrame (Vector points, String name, int frameWidth, int frameHeight)
{
SimplePlotPanel panel = new SimplePlotPanel (1, points.size());
panel.setNumXTicks (10);
panel.setNumYTicks (10);
int ID = panel.createNewCurve (name, Color.blue);
panel.setXYPoints (ID, points);
frame = new JFrame ();
frame.setSize (frameWidth, frameHeight);
frame.getContentPane().add (panel);
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e)
{
frame.dispose ();
}
});
frame.setVisible (true);
}
public static void makePlotFrame (Vector points)
{
makePlotFrame (points, "", 600, 480);
}
public static void makePlotFrame (ArrayList<Vector> pointSets, ArrayList<String> names, String xLabel, int frameWidth, int frameHeight)
{
Vector[] pointSetVector = new Vector [pointSets.size()];
String[] nameVector = new String [names.size()];
for (int i=0; i<pointSets.size(); i++) {
pointSetVector[i] = pointSets.get(i);
nameVector[i] = names.get(i);
}
makePlotFrame (pointSetVector, nameVector, xLabel, frameWidth, frameHeight);
}
public static void makePlotFrame (String title, ArrayList<Vector> pointSets, ArrayList<String> names, String xLabel, int frameWidth, int frameHeight)
{
// titleString = title;
Vector[] pointSetVector = new Vector [pointSets.size()];
String[] nameVector = new String [names.size()];
for (int i=0; i<pointSets.size(); i++) {
pointSetVector[i] = pointSets.get(i);
nameVector[i] = names.get(i);
}
makePlotFrame (pointSetVector, nameVector, xLabel, frameWidth, frameHeight);
}
public static void makePlotFrame (Vector[] pointSets, String[] names, String xLabel, int frameWidth, int frameHeight)
{
// Find largest set.
int N = pointSets[0].size();
for (int i=1; i<pointSets.length; i++) {
if (N < pointSets[i].size()) {
N = pointSets[i].size();
}
}
SimplePlotPanel panel = new SimplePlotPanel (pointSets.length, N);
panel.setXaxisLabel (xLabel);
panel.setNumXTicks (10);
panel.setNumYTicks (10);
for (int i=0; i<pointSets.length; i++) {
//System.out.println ("Simple.makeNew(): i=" + i + " names[i]=" + names[i]);
int ID = panel.createNewCurve (names[i], colors[i % colors.length]);
panel.setXYPoints (ID, pointSets[i]);
}
frame = new JFrame ();
frame.setSize (frameWidth, frameHeight);
frame.getContentPane().add (panel);
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e)
{
frame.dispose ();
}
});
frame.setVisible (true);
}
/**
* <code>paintComponent</code> overrides the <code>JPanel</code>'s
* method of the same name.
*
* @param g a <code>Graphics</code> value
*/
public void paintComponent (Graphics g)
{
super.paintComponent (g);
setBounds ();
// Now plot:
makePlot (g);
}
private void setBounds ()
{
// Set the bounding box to be the panel:
Dimension D = this.getSize();
panelHeight = D.height;
panelWidth = D.width;
bboxLeft = bboxTop = 0;
bboxRight = panelWidth;
bboxBottom = panelHeight;
}
////////////////////////////////////////////////////////////////////
// For public use:
/**
* Call <code>setTitle</code> with the plot title to be displayed.
*
* @param titleString a <code>String</code> value
*/
public void setTitle (String titleString)
{
this.titleString = titleString;
}
/**
* Call <code>createNewCurve</code> each time a new curve is to be started,
* before adding data for the curve.
*
* @param legendString a <code>String</code> value
* @return an <code>int</code> value: a curveID that is to be used later.
*/
public int createNewCurve (String legendString, Color color)
{
numCurves ++;
curves[numCurves-1].legendString = legendString;
curves[numCurves-1].color = color;
return numCurves-1;
}
/**
* Set the X-axis label.
*
* @param XaxisLabelString a <code>String</code> value
*/
public void setXaxisLabel (String XaxisLabelString)
{
this.XaxisLabelString = XaxisLabelString;
}
/**
* Set the Y-axis label.
*
* @param YaxisLabelString a <code>String</code> value
*/
public void setYaxisLabel (String YaxisLabelString)
{
this.YaxisLabelString = YaxisLabelString;
}
/**
* Set the number of X-axis tick marks.
*
* @param numXTicks an <code>int</code> value
*/
public void setNumXTicks (int numXTicks)
{
if (numXTicks <= maxNumValues) {
this.numXTicks = numXTicks;
}
}
/**
* Set the number of Y-axis tick marks.
*
* @param numYTicks an <code>int</code> value
*/
public void setNumYTicks (int numYTicks)
{
if (numYTicks <= maxNumValues) {
this.numYTicks = numYTicks;
}
}
public void setPointRadius (int numPixels)
{
if (numPixels > 0) {
pointBlobRadius = numPixels;
}
}
/**
* Call <code>setXVector</code> with a set of X-values for a particular curve,
* AFTER the curve has been created.
* This MUST be called before the corresponding Y-values are given.
* The Y-values can be given as a vector, or one by one to allowing
* for incremental plotting.
*
* @param curveID an <code>int</code> value
* @param X a <code>double[]</code> value
* @exception SimPlotException if an error occurs
*/
public void setXVector (int curveID, double[] X)
{
if (curveID >= maxNumCurves) {
return;
}
if (X.length > maxNumValues) {
return;
}
curves[curveID].setXValues (X);
// Update range:
double min = X[0], max = X[0];
for (int i=0; i<X.length; i++) {
if (X[i] < min)
min = X[i];
if (X[i] > max)
max = X[i];
}
if (min < Xmin)
Xmin = min;
if (max > Xmax)
Xmax = max;
Xrange = Xmax - Xmin;
}
/**
* Call <code>setYVector</code> to pass in a set of Y-values for a
* a particular curve. Call this only after setting the X-values for
* the same curve.
*
* @param curveID an <code>int</code> value
* @param Y a <code>double[]</code> value
* @exception SimPlotException if an error occurs
*/
public void setYVector (int curveID, double[] Y)
{
if (curveID >= maxNumCurves) {
return;
}
if (Y.length != curves[curveID].getNumXValues()) {
return;
}
curves[curveID].setYValues (Y);
// Update range:
double min = Y[0], max = Y[0];
for (int i=0; i<Y.length; i++) {
if (Y[i] < min)
min = Y[i];
if (Y[i] > max)
max = Y[i];
}
if (min < Ymin)
Ymin = min;
if (max > Ymax)
Ymax = max;
Yrange = Ymax - Ymin;
}
/**
* You can call <code>addNewYValue</code> for a curve, once for each
* Y-value for incremental plotting. In this case, the caller needs
* to <code>repaint</code> the panel.
*
* @param curveID an <code>int</code> value
* @param y a <code>double</code> value
* @exception SimPlotException if an error occurs
*/
public void addNewYValue (int curveID, double y)
{
if (curveID >= maxNumCurves) {
return;
}
if (curves[curveID].getNumYValues() >= curves[curveID].getNumXValues()) {
return;
}
curves[curveID].addNewYValue (y);
// Adjust range:
if (y < Ymin)
Ymin = y;
if (y > Ymax)
Ymax = y;
Yrange = Ymax - Ymin;
}
public void setXYPoints (int curveID, Vector points)
{
if (points == null)
return;
if (curveID >= maxNumCurves) {
return;
}
int numPoints = points.size();
if (points.size() > maxNumValues) {
numPoints = maxNumValues;
}
double[] X = new double [numPoints];
double[] Y = new double [numPoints];
for (int i=0; i<numPoints; i++) {
Object obj = points.get (i);
if (! (obj instanceof Point2D.Double)) {
return;
}
Point2D.Double p = (Point2D.Double) obj;
X[i] = p.x;
Y[i] = p.y;
}
setXVector (curveID, X);
setYVector (curveID, Y);
}
/**
* Call <code>makePlot</code> to make the plotter plot on a <code>Graphics</code>
* instance of your choice, with a specified bounding box within which to draw.
*
* @param g a <code>Graphics</code> value
* @param boundingBoxTopLeftX an <code>int</code> value - the top left corner of the box (X)
* @param boundingBoxTopLeftY an <code>int</code> value - top left Y value.
* @param boundingBoxWidth an <code>int</code> value - width of the box.
* @param boundingBoxHeight an <code>int</code> value - height of the box.
*/
public void makePlot (Graphics g, int boundingBoxTopLeftX, int boundingBoxTopLeftY,
int boundingBoxWidth, int boundingBoxHeight)
{
bboxLeft = boundingBoxTopLeftX;
bboxRight = bboxLeft + boundingBoxWidth;
bboxTop = boundingBoxTopLeftY;
bboxBottom = bboxTop + boundingBoxHeight;
makePlot (g);
}
////////////////////////////////////////////////////////////////////
// Drawing
void makePlot (Graphics g)
{
// Compute sizes:
computeDrawingConstants (g);
// Draw the bounding box:
g.setColor (Color.black);
g.drawRect (bboxLeft, bboxTop, bboxWidth, bboxHeight);
// Draw legend line:
g.drawLine (bboxLeft, graphBoxBottom, bboxRight, graphBoxBottom);
writeLegends (g);
// Draw plot box:
g.setColor (Color.black);
g.drawRect (plotBoxLeft, plotBoxTop, plotBoxWidth, plotBoxHeight);
// Write labels:
g.setFont (axisLabelFont);
int x = bboxLeft + glueSpace;
int y = bboxTop + 2*glueSpace + axisLabelFontMetrics.getHeight()
+ titleFontMetrics.getHeight();
g.drawString (YaxisLabelString, x, y);
x = graphBoxRight - glueSpace - axisLabelFontMetrics.stringWidth (XaxisLabelString);
y = graphBoxBottom - glueSpace;
g.drawString (XaxisLabelString, x, y);
// Draw and write ticks:
g.setFont (tickFont);
drawTicks (g);
// Write title.
x = (bboxLeft + bboxRight) / 2;
y = bboxTop + 2*glueSpace + titleFontMetrics.getHeight();
g.setColor (Color.black);
g.setFont (titleFont);
g.drawString (titleString, x, y);
// Plot curves:
for (int i=0; i<numCurves; i++) {
plotCurve (g, curves[i]);
}
}
int dataXToPixelValue (double x)
{
// Take a graph coordinate and get the pixel value for drawing.
// First, the pixel value with regular coordinates:
int regularX = (int) Math.round (((x - Xmin) / Xrange) * plotBoxWidth);
// Now shift:
return regularX + bboxLeft + plotBoxLeftInset;
}
int dataYToPixelValue (double y)
{
// Take a graph coordinate and get the pixel value for drawing.
// First, the pixel value with regular coordinates:
int regularY = (int) Math.round (((y - Ymin) / Yrange) * plotBoxHeight);
// Now shift:
return bboxTop + plotBoxTopInset + (plotBoxHeight - regularY);
}
void computeDrawingConstants (Graphics g)
{
// Boundingbox limits:
bboxHeight = bboxBottom - bboxTop;
bboxWidth = bboxRight - bboxLeft;
// Legend stuff:
legendBoxWidth = bboxWidth;
g.setFont (legendFont);
legendFontMetrics = g.getFontMetrics();
legendBoxHeight = legendFontMetrics.getHeight() * numCurves + 4*glueSpace;
// GraphBox stuff:
graphBoxWidth = bboxWidth;
graphBoxHeight = bboxHeight - legendBoxHeight;
graphBoxLeft = bboxLeft;
graphBoxRight = graphBoxLeft + graphBoxWidth;
graphBoxTop = bboxTop;
graphBoxBottom = graphBoxTop + graphBoxHeight;
// Need to do ticks next, to get tick distances:
g.setFont (tickFont);
tickFontMetrics = g.getFontMetrics();
makeTicks (g);
// plotBox stuff:
g.setFont (titleFont);
titleFontMetrics = g.getFontMetrics();
int titleFontHeight = titleFontMetrics.getHeight();
g.setFont (axisLabelFont);
axisLabelFontMetrics = g.getFontMetrics();
int axisLabelHeight = axisLabelFontMetrics.getHeight();
plotBoxTopInset = 6*glueSpace + axisLabelHeight + titleFontHeight;
plotBoxBottomInset = 4*glueSpace + tickFontMetrics.getHeight() + axisLabelHeight;
plotBoxRightInset = 4*glueSpace + XTickWidth + tickSize/2;
plotBoxLeftInset = 4*glueSpace + YTickWidth + tickSize/2;
plotBoxWidth = graphBoxWidth - (plotBoxLeftInset + plotBoxRightInset);
plotBoxHeight = graphBoxHeight - (plotBoxTopInset + plotBoxBottomInset);
plotBoxTop = graphBoxTop + plotBoxTopInset;
plotBoxBottom = plotBoxTop + plotBoxHeight;
plotBoxLeft = graphBoxLeft + plotBoxLeftInset;
plotBoxRight = plotBoxLeft + plotBoxWidth;
}
void makeTicks (Graphics g)
{
if ( (numXTicks < 0) || (numYTicks < 0) )
numXTicks = numYTicks = estimateNumXTicks (g);
createFormats();
makeXTicks ();
makeYTicks ();
}
void drawTicks (Graphics g)
{
for (int i=0; i<numXTicks; i++)
drawXTick (g, XTicks[i], XTickLabels[i]);
for (int i=0; i<numYTicks; i++)
drawYTick (g, YTicks[i], YTickLabels[i]);
}
int estimateNumXTicks (Graphics g)
{
// Do this by seeing how many ticks for Xmax will fit.
int width = tickFontMetrics.stringWidth ("" + Xmax);
int n = graphBoxWidth / (width + 2*glueSpace);
// System.out.println ("estimateNumXTicks(): Xmax=" + Xmax + " w=" + width + " n=" + n);
// Reduce by 1 to be safe.
if (n > 4)
n --;
if (n > maxNumValues)
n = maxNumValues;
// System.out.println ("estimateNumXTicks=" + n);
return n;
}
void makeXTicks ()
{
// Force an odd number of ticks:
if (numXTicks % 2 == 0)
numXTicks++;
double intervalSize = Xrange / (numXTicks-1);
XTicks[0] = Xmin;
for (int i=1; i<=numXTicks-2; i++)
XTicks[i] = XTicks[i-1] + intervalSize;
XTicks[numXTicks-1] = Xmax;
// Now for labels:
int maxWidth = Integer.MIN_VALUE;
for (int i=0; i<numXTicks; i++) {
XTickLabels[i] = makeDoubleFormatX (XTicks[i]);
int width = tickFontMetrics.stringWidth (XTickLabels[i]);
if (width > maxWidth)
maxWidth = width;
// System.out.println ("makeXTicks(): i=" + i + " XTick[i]=" + XTicks[i]);
}
// We'll need this for writing:
XTickWidth = maxWidth;
}
void makeYTicks ()
{
if (numYTicks % 2 == 0)
numYTicks++;
double intervalSize = Yrange / (numYTicks-1);
YTicks[0] = Ymin;
for (int i=1; i<=numYTicks-2; i++)
YTicks[i] = YTicks[i-1] + intervalSize;
YTicks[numYTicks-1] = Ymax;
// Now for labels:
int maxWidth = Integer.MIN_VALUE;
for (int i=0; i<numYTicks; i++) {
YTickLabels[i] = makeDoubleFormatY (YTicks[i]);
int width = tickFontMetrics.stringWidth (YTickLabels[i]);
if (width > maxWidth)
maxWidth = width;
// System.out.println ("makeYTicks(): i=" + i + " YTickLabels[i]=" + YTickLabels[i] + " w=" + width + " max=" + maxWidth);
}
YTickWidth = maxWidth;
}
void createFormats ()
{
// Based on Xmin, Xmax:
Xformat = makeFormat (Xmin, Xmax);
// Based on Ymin, Ymax:
Yformat = makeFormat (Ymin, Ymax);
}
DecimalFormat makeFormat (double low, double high)
{
// Find out precision required:
String formatStr = "#.##";
double value = low;
if (low == 0.0) {
// Use high, possibly.
if ( (high == 0) || (high > 1) )
return new DecimalFormat (formatStr);
value = high;
}
// Make it positive:
if (value < 0) value = value * (-1);
while (value < 1) {
value = value * 10;
formatStr = formatStr + "#";
}
// System.out.println ("low=" + low + " high=" + high + " value=" + value + " formatStr=" + formatStr);
return new DecimalFormat (formatStr);
}
String makeDoubleFormatX (double x)
{
String str= Xformat.format (x);
// System.out.println ("makeX: x=" + x + " str=" + str);
return str;
}
String makeDoubleFormatY (double y)
{
String str = Yformat.format (y);
// System.out.println ("makeY: y=" + y + " str=" + str);
return str;
}
void drawXTick (Graphics g, double x, String tickLabel)
{
// System.out.println ("DrawXTick: numXTicks=" + numXTicks + " x=" + x + " label=" + tickLabel);
// Draw a tick at data position x.
int xTick = dataXToPixelValue (x);
int y = plotBoxBottom;
g.setColor (Color.darkGray);
g.drawLine (xTick, y+tickSize/2, xTick, y-tickSize/2);
if (tickLabel != null) {
// Put a label centered at xTick.
int xLabelPos = xTick - (tickFontMetrics.stringWidth(tickLabel) / 2);
int yLabelPos = plotBoxBottom + glueSpace
+ tickSize/2 + tickFontMetrics.getHeight();
g.drawString (tickLabel, xLabelPos, yLabelPos);
}
}
void drawYTick (Graphics g, double y, String tickLabel)
{
// System.out.println ("DrawYTick: numYTicks=" + numYTicks + " y=" + y + " label=" + tickLabel);
// Draw a tick at data position y.
int yTick = dataYToPixelValue (y);
int x = plotBoxLeft;
g.setColor (Color.darkGray);
g.drawLine (x-tickSize/2, yTick, x+tickSize/2, yTick);
if (tickLabel != null) {
// Put a label.
int xLabelPos = plotBoxLeft - tickSize/2 - 2*glueSpace
- tickFontMetrics.stringWidth(tickLabel);
int yLabelPos = yTick + tickFontMetrics.getHeight()/2;
g.drawString (tickLabel, xLabelPos, yLabelPos);
}
}
void writeLegends (Graphics g)
{
for (int i=0; i<numCurves; i++) {
// Write the legend string for curve i. First, the font.
g.setFont (legendFont);
// Now, the location
int x = graphBoxLeft + 4*glueSpace;
int legendFontHeight = g.getFontMetrics().getHeight();
int y = graphBoxBottom + 2*glueSpace + ((i+1) * legendFontHeight);
g.setColor (curves[i].color);
g.drawString (curves[i].legendString, x, y);
}
}
void plotCurve (Graphics g, SimplePlotCurve curve)
{
g.setColor (curve.color);
int x=0, y=0, prev_x=0, prev_y=0;
for (int i=0; i<curve.getNumValues(); i++) {
// Plot the point X[i],Y[i].
if (i > 0) {
prev_x = x; prev_y = y;
}
x = dataXToPixelValue (curve.getX(i));
y = dataYToPixelValue (curve.getY(i));
g.fillOval (x-pointBlobRadius, y-pointBlobRadius, 2*pointBlobRadius, 2*pointBlobRadius);
//** Joint the dots? Assumes data is sorted.
if (i > 0)
g.drawLine (prev_x, prev_y, x, y);
}
}
}
class SimplePlotCurve {
private int maxNumValues; // Maximum number of points.
private int numXValues; // Actual number of points.
private int numCurrentYValues; // Number of Y values added so far.
String legendString; // String to print in legend.
Color color; // Preferred color.
private double[] X, Y; // The points.
public SimplePlotCurve (int maxNumValues)
{
X = new double [maxNumValues];
Y = new double [maxNumValues];
numXValues = 0;
numCurrentYValues = 0;
}
public void setXValues (double[] X)
{
numXValues = 0;
for (int i=0; i<X.length; i++) {
numXValues++;
this.X[numXValues-1] = X[i];
}
}
public void setYValues (double[] Y)
{
numCurrentYValues = 0;
for (int i=0; i<Y.length; i++) {
numCurrentYValues ++;
this.Y[numCurrentYValues-1] = Y[i];
}
}
public void addNewYValue (double y)
{
numCurrentYValues ++;
Y[numCurrentYValues-1] = y;
}
public int getNumXValues ()
{
return numXValues;
}
public int getNumValues ()
{
if (numXValues > numCurrentYValues)
return numCurrentYValues;
else
return numXValues;
}
public int getNumYValues ()
{
return numCurrentYValues;
}
public double getX (int i)
{
return X[i];
}
public double getY (int i)
{
return Y[i];
}
}