comparison src/org/dancres/blitz/tools/dash/PieChart.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children 48228766ed4c
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.tools.dash;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Font;
6 import java.awt.FontMetrics;
7 import java.awt.Graphics;
8 import javax.swing.JPanel;
9
10 public class PieChart extends JPanel {
11 String title;
12 Font font;
13 FontMetrics fontMetrics;
14 int titleHeight = 15;
15 int columns;
16 int values[];
17 Color colors[];
18 String labels[];
19 float percent[];
20 float angle[];
21 int maxLabelWidth = 0;
22 int maxValueWidth = 0;
23 int max = 0;
24 int strWidth = 0;
25 boolean showLabel = true;
26 boolean showPercent = true;
27 int lx = 0, ly = 0;
28 int cx = 0, cy = 0;
29 public PieChart() {
30 font = new java.awt.Font("Sanserif", Font.BOLD, 12);
31 fontMetrics = getFontMetrics(font);
32 setBackground(Color.white);
33 title = "JavaSpace ops";
34 columns = 3;
35 showLabel = true;
36 showPercent = true;
37 values = new int[]{0, 0, 0};
38 colors = new Color[]{ColorScheme.TAKE,ColorScheme.WRITE,
39 ColorScheme.READ};
40 labels = new String[]{"Take", "Write", "Read"};
41 percent = new float[columns];
42 angle = new float[columns];
43 calcValues();
44 }
45 private void calcValues() {
46 float totalValue = 0;
47 for (int i = 0; i < columns; i++) {
48 totalValue += values[i];
49 if (values[i] > max) {
50 max = values[i];
51 }
52 maxLabelWidth = Math.max(fontMetrics
53 .stringWidth((String) (labels[i])),
54 maxLabelWidth);
55 }
56 float multiFactor = 100 / totalValue;
57 for (int i = 0; i < columns; i++) {
58 percent[i] = values[i] * multiFactor;
59 angle[i] = (float) (percent[i] * 3.6);
60 }
61 }
62 void update(int take, int write, int read) {
63 values = new int[]{take, write, read};
64 calcValues();
65 repaint();
66 }
67 public synchronized void paint(Graphics g) {
68 Dimension dim = getSize();
69 g.setColor(Color.white);
70 g.fillRect(0, 0, dim.width, dim.height);
71 int x = 0;
72 int y = 0;
73 int width = 0, height = 0;
74 int ax = 0, ay = 0;
75 int px = 0, py = 0;
76 int radius = 0;
77 width = height = Math.min((getSize().width - 100),
78 (getSize().height - 100));
79 x = y = 50;
80 if (getSize().width > width) {
81 x = (getSize().width - width) / 2;
82 }
83 cx = x + width / 2;
84 cy = y + height / 2;
85 radius = width / 2;
86 strWidth = fontMetrics.stringWidth(title);
87 Font fnt = new java.awt.Font("Sanserif", Font.BOLD, 16);
88 g.setFont(fnt);
89 g.setColor(Color.red);
90 g.setFont(font);
91 int initAngle = 90;
92 int sweepAngle = 0;
93 int incSweepAngle = 0;
94 int incLabelAngle = (int) (angle[0] / 2);
95 for (int i = 0; i < columns; i++) {
96 sweepAngle = (int) Math.round(angle[i]);
97 g.setColor((Color) colors[i]);
98 if (i == (columns - 1)) {
99 sweepAngle = 360 - incSweepAngle;
100 g.fillArc(x, y, width, height, initAngle, (-sweepAngle));
101 g.setColor(Color.black);
102 g.drawArc(x, y, width, height, initAngle, (-sweepAngle));
103 if (showLabel) {
104 lx = (int) (cx + (radius * Math
105 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
106 ly = (int) (cy + (radius * Math
107 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
108 adjustLabel(i);
109 g.drawString((String) labels[i], lx, ly);
110 }
111 if (showPercent) {
112 px = (int) (cx + ((radius * 2 / 3) * Math
113 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
114 py = (int) (cy + ((radius * 2 / 3) * Math
115 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
116 g.drawString(String.valueOf(Math.round(percent[i])) + "%",
117 px, py);
118 }
119 break;
120 }
121 g.fillArc(x, y, width, height, initAngle, (-sweepAngle));
122 g.setColor(Color.black);
123 g.drawArc(x, y, width, height, initAngle, (-sweepAngle));
124 incSweepAngle += sweepAngle;
125 ax = (int) (cx + (radius * Math
126 .cos((incSweepAngle * 3.14f / 180) - 3.14f / 2)));
127 ay = (int) (cy + (radius * Math
128 .sin((incSweepAngle * 3.14f / 180) - 3.14f / 2)));
129 g.drawLine(cx, cy, ax, ay);
130 if (showLabel) {
131 lx = (int) (cx + (radius * Math
132 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
133 ly = (int) (cy + (radius * Math
134 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
135 adjustLabel(i);
136 g.drawString((String) labels[i], lx, ly);
137 }
138 if (showPercent) {
139 px = (int) (cx + ((radius * 2 / 3) * Math
140 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
141 py = (int) (cy + ((radius * 2 / 3) * Math
142 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
143 strWidth = fontMetrics
144 .stringWidth(Math.round(percent[i]) + "%");
145 g.drawString(String.valueOf(Math.round(percent[i])) + "%",
146 (px - strWidth / 2), py);
147 }
148 incLabelAngle = incLabelAngle
149 + (int) (angle[i] / 2 + angle[i + 1] / 2);
150 initAngle += (-sweepAngle);
151 }
152 g.setColor(Color.black);
153 g.drawLine(cx, cy, cx, cy - radius);
154 }
155 private void adjustLabel(int i) {
156 if ((lx > cx) && (ly < cy)) {
157 lx += 5;
158 ly -= 5;
159 }
160 if ((lx > cx) && (ly > cy)) {
161 lx += 5;
162 ly += 10;
163 }
164 if ((lx < cx) && (ly > cy)) {
165 strWidth = fontMetrics.stringWidth(labels[i]);
166 lx -= strWidth + 5;
167 if (lx < 0)
168 lx = 0;
169 }
170 if ((lx < cx) && (ly < cy)) {
171 strWidth = fontMetrics.stringWidth(labels[i]);
172 lx -= strWidth + 5;
173 if (lx < 0)
174 lx = 0;
175 }
176 }
177 }