1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.visualiser.statistics.charts;
25
26 import java.util.*;
27 import java.awt.*;
28 import zeus.util.*;
29
30 public class TabularGraph implements DrawObject {
31 protected static final double TINY = 1E-6;
32 protected static final int LEFT = 30;
33
34 protected String title;
35 protected Vector bars = new Vector();
36 protected int x, y = 0;
37 protected FontMetrics fm;
38 protected Font font;
39 protected int gap = 10;
40
41 class Bar { String label; double[] values; }
42
43 public TabularGraph() {
44 }
45 public TabularGraph(double[][] values, String[] labels, String title) {
46 setData(values,labels,title);
47 }
48 public void setData(double[][] values, String[] labels, String title) {
49 this.title = title;
50
51 bars.removeAllElements();
52 for( int i = 0; i < values.length; i++ ) {
53 Bar bar = new Bar();
54 bar.values = new double[values[i].length];
55 for( int j = 0; j < bar.values.length; j++ )
56 bar.values[j] = values[i][j];
57 bar.label = labels[i];
58 bars.addElement(bar);
59 }
60 }
61
62
63 public void drawYourSelf(Graphics g) {
64 font = new Font("Arial", Font.BOLD, 14);
65 fm = g.getFontMetrics(font);
66 g.setFont(font);
67 int W = fm.stringWidth(title);
68 int H = fm.getHeight();
69 g.drawString(title,(x-W)/2,H);
70
71 if ( bars.isEmpty() ) return;
72
73 String str;
74 Bar bar;
75 int w = 0;
76 font = new Font("Arial", Font.PLAIN, 12);
77 fm = g.getFontMetrics(font);
78 int h = fm.getHeight() + gap;
79 g.setFont(font);
80 for( int i = 0; i < bars.size(); i++ ) {
81 bar = (Bar) bars.elementAt(i);
82 w = Math.max(w,fm.stringWidth(bar.label));
83 for( int j = 0; j < bar.values.length; j++ ) {
84 str = Misc.decimalPlaces(bar.values[j],2);
85 w = Math.max(w,fm.stringWidth(str));
86 }
87 }
88 w += gap;
89
90 int x0, y0, x1, y1, x2, y2;
91 g.drawLine(LEFT+w,3*H,LEFT+w,3*H+(bars.size()+1)*h);
92 g.drawLine(LEFT,3*H+h,LEFT+(bars.size()+1)*w,3*H+h);
93
94 x0 = LEFT+w+gap/2; y0 = 3*H + h - gap/2;
95 y1 = y0;
96 for( int i = 0; i < bars.size(); i++ ) {
97 bar = (Bar) bars.elementAt(i);
98 x1 = x0 + i*w;
99 g.drawString(bar.label,x1,y1);
100 }
101 x0 = LEFT+gap/2; y0 = 3*H + h + h - gap/2;
102 x1 = x0;
103 for( int i = 0; i < bars.size(); i++ ) {
104 bar = (Bar) bars.elementAt(i);
105 y1 = y0 + i*h;
106 g.drawString(bar.label,x1,y1);
107 y2 = y1;
108 for( int j = 0; j < bar.values.length; j++ ) {
109 str = Misc.decimalPlaces(bar.values[j],2);
110 x2 = (x1 + w) + j*w;
111 g.drawString(str,x2,y2);
112 }
113 }
114 }
115
116 public void setXY(int xc, int yc) {
117 x = xc; y = yc;
118 }
119 }