comparison src/org/dancres/blitz/tools/dash/PieChart.java @ 6:48228766ed4c

when expecting floating point math, do floating point math
author dbrosius@mebigfatguy.com
date Thu, 14 May 2009 12:15:48 -0400
parents 3dc0c5604566
children
comparison
equal deleted inserted replaced
5:27ca8522be85 6:48228766ed4c
3 import java.awt.Color; 3 import java.awt.Color;
4 import java.awt.Dimension; 4 import java.awt.Dimension;
5 import java.awt.Font; 5 import java.awt.Font;
6 import java.awt.FontMetrics; 6 import java.awt.FontMetrics;
7 import java.awt.Graphics; 7 import java.awt.Graphics;
8
8 import javax.swing.JPanel; 9 import javax.swing.JPanel;
9 10
10 public class PieChart extends JPanel { 11 public class PieChart extends JPanel {
11 String title; 12 String title;
12 Font font; 13 Font font;
48 totalValue += values[i]; 49 totalValue += values[i];
49 if (values[i] > max) { 50 if (values[i] > max) {
50 max = values[i]; 51 max = values[i];
51 } 52 }
52 maxLabelWidth = Math.max(fontMetrics 53 maxLabelWidth = Math.max(fontMetrics
53 .stringWidth((String) (labels[i])), 54 .stringWidth((labels[i])),
54 maxLabelWidth); 55 maxLabelWidth);
55 } 56 }
56 float multiFactor = 100 / totalValue; 57 float multiFactor = 100 / totalValue;
57 for (int i = 0; i < columns; i++) { 58 for (int i = 0; i < columns; i++) {
58 percent[i] = values[i] * multiFactor; 59 percent[i] = values[i] * multiFactor;
91 int initAngle = 90; 92 int initAngle = 90;
92 int sweepAngle = 0; 93 int sweepAngle = 0;
93 int incSweepAngle = 0; 94 int incSweepAngle = 0;
94 int incLabelAngle = (int) (angle[0] / 2); 95 int incLabelAngle = (int) (angle[0] / 2);
95 for (int i = 0; i < columns; i++) { 96 for (int i = 0; i < columns; i++) {
96 sweepAngle = (int) Math.round(angle[i]); 97 sweepAngle = Math.round(angle[i]);
97 g.setColor((Color) colors[i]); 98 g.setColor(colors[i]);
98 if (i == (columns - 1)) { 99 if (i == (columns - 1)) {
99 sweepAngle = 360 - incSweepAngle; 100 sweepAngle = 360 - incSweepAngle;
100 g.fillArc(x, y, width, height, initAngle, (-sweepAngle)); 101 g.fillArc(x, y, width, height, initAngle, (-sweepAngle));
101 g.setColor(Color.black); 102 g.setColor(Color.black);
102 g.drawArc(x, y, width, height, initAngle, (-sweepAngle)); 103 g.drawArc(x, y, width, height, initAngle, (-sweepAngle));
104 lx = (int) (cx + (radius * Math 105 lx = (int) (cx + (radius * Math
105 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 106 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
106 ly = (int) (cy + (radius * Math 107 ly = (int) (cy + (radius * Math
107 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 108 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
108 adjustLabel(i); 109 adjustLabel(i);
109 g.drawString((String) labels[i], lx, ly); 110 g.drawString(labels[i], lx, ly);
110 } 111 }
111 if (showPercent) { 112 if (showPercent) {
112 px = (int) (cx + ((radius * 2 / 3) * Math 113 px = (int) (cx + ((radius * 2.0f / 3) * Math
113 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 114 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
114 py = (int) (cy + ((radius * 2 / 3) * Math 115 py = (int) (cy + ((radius * 2 / 3) * Math
115 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 116 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
116 g.drawString(String.valueOf(Math.round(percent[i])) + "%", 117 g.drawString(String.valueOf(Math.round(percent[i])) + "%",
117 px, py); 118 px, py);
131 lx = (int) (cx + (radius * Math 132 lx = (int) (cx + (radius * Math
132 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 133 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
133 ly = (int) (cy + (radius * Math 134 ly = (int) (cy + (radius * Math
134 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 135 .sin((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
135 adjustLabel(i); 136 adjustLabel(i);
136 g.drawString((String) labels[i], lx, ly); 137 g.drawString(labels[i], lx, ly);
137 } 138 }
138 if (showPercent) { 139 if (showPercent) {
139 px = (int) (cx + ((radius * 2 / 3) * Math 140 px = (int) (cx + ((radius * 2 / 3) * Math
140 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2))); 141 .cos((incLabelAngle * 3.14f / 180) - 3.14f / 2)));
141 py = (int) (cy + ((radius * 2 / 3) * Math 142 py = (int) (cy + ((radius * 2 / 3) * Math