1 /*
2 * The contents of this file are subject to the BT "ZEUS" Open Source
3 * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
4 * except in compliance with the Licence. You may obtain a copy of the Licence
5 * from $ZEUS_INSTALL/licence.html or alternatively from
6 * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7 *
8 * Except as stated in Clause 7 of the Licence, software distributed under the
9 * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the Licence for the specific language governing rights and
11 * limitations under the Licence.
12 *
13 * The Original Code is within the package zeus.*.
14 * The Initial Developer of the Original Code is British Telecommunications
15 * public limited company, whose registered office is at 81 Newgate Street,
16 * London, EC1A 7AJ, England. Portions created by British Telecommunications
17 * public limited company are Copyright 1996-9. All Rights Reserved.
18 *
19 * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20 */
21
22
23
24 package zeus.visualiser.statistics.charts;
25
26 /*
27 Author : Haydn R. Haynes
28 Name : PieChart.java
29 Inception Date : 18/07/1997
30 Last Revision Date : 27/08/1997
31 Revision Reason : move slice percentage calcs into this class
32 As Part of : Statistics Tool for ZEUS Visualiser
33
34 This is a Pie Chart drawing class. It extends the DrawObject class which
35 has been created to allow an interface which all drawing objects can use to
36 allow their display within the Statistics Tool drawing area.
37 */
38
39 import java.util.*;
40 import java.awt.*;
41 import zeus.gui.ColorManager;
42
43
44 public class PieChart implements DrawObject
45 {
46 protected String title;
47 protected Vector pieces = new Vector();
48 protected int x,y = 0;
49 protected FontMetrics fm;
50 protected Font font;
51 protected static double TINY = 1E-6;
52
53 /*
54 This simple object is designed to hold slice information for the pie
55 chart such as percentage (of all messages), the legend string associated
56 with it and the colour that the slice will be draw on screen. This will
57 keep the colours consistent when the object is refreshed on screen.
58 */
59 class Slice { double value; String label; Color color; }
60
61 /*
62 This is the default constructor for the Pie Chart object. It takes as
63 its parameters the title for the object as well as the percentage/legend
64 information. It dispenses of any 0.0 % values and constructs a vector of
65 Slices to store the information for future refresh.
66 */
67 public PieChart() {
68 }
69 public PieChart(double[] values, String[] labels, String title) {
70 setData(values,labels,title);
71 }
72
73 public void setData(double[] values, String[] labels, String title) {
74 this.title = title;
75
76 double sum = 0.0;
77 int pos = 0;
78 pieces.removeAllElements();
79 for(int i=0;i<values.length;i++) sum += values[i];
80 for(int j=0;j<values.length;j++) {
81 if (Math.abs(values[j]-0.0) > TINY) {
82 Slice slice = new Slice();
83 slice.value = (100.0*values[j]/sum);
84 slice.label = labels[j];
85 slice.color = ColorManager.getColor(pos++);
86 pieces.addElement(slice);
87 }
88 }
89 }
90
91 /*
92 This is the method responsible for drawing the object onto whatever
93 Graphics object is passed to it. It attempts to draw the object in as
94 central a position as possible.
95 */
96 public void drawYourSelf(Graphics g) {
97 int si = 0; int ly = y/5; int lx = x-(x/5); int rad = x/3;
98
99 font = new Font("Arial", Font.BOLD, 14);
100 fm = g.getFontMetrics(font);
101 g.setFont(font);
102 si = fm.stringWidth(title);
103 g.drawString(title,(x-si)/2,y/13);
104
105 if ( pieces.isEmpty() ) return;
106
107 font = new Font("Arial", Font.PLAIN, 12);
108 fm = g.getFontMetrics(font);
109 g.setFont(font);
110 g.drawString("Key",lx,ly);
111
112 int sa = 90; ly += 25;
113 Slice slice;
114 for (int i=0;i<pieces.size();i++) {
115 slice = (Slice)pieces.elementAt(i);
116 int a = (int)(Math.round((360/100.0)*slice.value));
117 g.setColor(slice.color);
118 g.fillArc(x/3,y/3,rad,rad,sa,a);
119 doLegend(g,lx,ly,slice.label,Math.round(slice.value));
120 sa += a; ly += 20;
121 }
122 }
123
124 /*
125 Draw the legend information that corresponds to this pie chart. It
126 attempts to draw the legend on the right hand side of the screen.
127 */
128 protected void doLegend(Graphics g, int xc, int yc, String legend, long i) {
129 g.fillRect(xc,yc,10,10);
130 g.setColor(Color.black);
131 if (i<10.0) { g.drawString(" " + i + " % :: " + legend,xc+20,yc+10); }
132 else { g.drawString(i + " % :: " + legend,xc+20,yc+10); }
133 }
134
135 /*
136 This method sets the default height and width of the component that the
137 Pie chart is to be drawn on. It is necessary to allow the positioning
138 of the pie chart, the header and the legend information.
139 */
140 public void setXY(int xc, int yc) {
141 x = xc; y = yc;
142 }
143
144 /*
145 This is a method that allows the caller program to place miscellaneous
146 text on the drawing pane. This information is extra to the scale and
147 legend information. Note, there is no checking in the PieChart class
148 to determine whether the text will overlap any on screen component.
149 */
150 public void userDraw(Graphics g, String text, int xc, int yc) {
151 font = new Font("Arial", Font.PLAIN, 12);
152 fm = g.getFontMetrics(font);
153 g.setFont(font);
154 g.drawString(text,xc,yc);
155 }
156 }
157
158
159