-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieChart.java
More file actions
178 lines (176 loc) · 4.8 KB
/
PieChart.java
File metadata and controls
178 lines (176 loc) · 4.8 KB
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
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JComponent;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
public class PieChart extends JComponent
{
ArrayList<PieChartSlice> list;
PieChartSlice slice;
Rectangle boundry;
Rectangle legBox;
Rectangle legend;
Ellipse2D.Double pie;
double angle = 0;
String title;
/**
manages all the pie chart slices and the pie chart.
creates the outline for the PieChartViewer.
*/
public PieChart()
{
list = new ArrayList<PieChartSlice>();
boundry = new Rectangle(10,60,575,300);
legend = new Rectangle(400,90,140,260);
pie = new Ellipse2D.Double(50,85,250,250);
legBox = new Rectangle();
}
/**
updates the start angle for each slice
*/
public double updateAngle()
{
angle = 0;
//loops to updates start angle
for(int i = 0; i < list.size(); i++)
{
angle += list.get(i).getAngleExtent();
}
return angle;
}
/**
adds slice to list
@param name, color, size
then repaints
return true if slice cannot fit in chart
else return false
*/
public boolean addSlice(String name, Color color, double size)
{
size =((size*360)/100);
if((0 < size)&&(size + updateAngle() <= 360))
{
slice = new PieChartSlice(name, angle, size);
slice.setName(name);
slice.setColor(color);
slice.setAngleExtent(size);
slice.setAngleStart(updateAngle());
list.add(slice);
repaint();
return true;
}
System.out.println("Cannot add slice, PieChart is too full or slice has no size");
return false;
}
/**
Colapse the slice in arraylist.
*/
public void collapse()
{ double newAngle = 0;
for (int i = 0; i < list.size(); i++)
{
if(list.get(i) != null)
{
list.get(i).setAngleStart(newAngle);
newAngle += list.get(i).getAngleExtent();
}
}
}
/**
deletes the slice by click
then updates the arraylist
@param xSlice
*/
public void deleteSlice(PieChartSlice xSlice)
{
list.remove(xSlice);
collapse();
repaint();
}
/**
sets title for chart
@param String a
*/
public void setTitle(String a)
{
title = a;
}
/**
returns title
*/
public String getTitle()
{
return title;
}
/**
loops through all the slices in list to find it x and y are in the size
*/
PieChartSlice selectSlice(double x, double y)
{
PieChartSlice empty = null;
System.out.println(x +" "+ y);
for( int i = 0; i < list.size(); i++)
{
if(list.get(i).contains(x,y))
{
System.out.println("slice found");
empty = list.get(i);
return empty;
}
}
System.out.println(slice.getBounds2D());
return empty;
}
/**
paintComponent for the slice + frame
*/
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
//loops for each slice in list, and draws slice
for(int i = 0; i < list.size(); i++)
{
slice = list.get(i);
g2.setColor(slice.getColor());
slice.draw(g2);
g2.setColor(Color.BLACK);
Font font = new Font("Serif",Font.PLAIN, 20);
g2.setFont(font);
g2.drawString(getTitle(),250,50);
}
//loops for each slice in list, and draws String in legend
for(int i = 0; i < 1; i++)
{
int x = 0;
for(int j = 0; j < list.size(); j++)
{
slice = list.get(j);
g2.setColor(Color.BLACK);
Font font2 = new Font("Serif",Font.PLAIN, 12);
g2.setFont(font2);
g2.drawString(slice.getName(),425,110+x);
g2.setColor(slice.getColor());
legBox.setBounds((int)legend.getX()+10, (int)legend.getY()+10+x,10,10);
g2.fill(legBox);
x += 20;
}
}
//draws veiwer outline
g2.setColor(Color.BLACK);
g2.draw(boundry);
g2.draw(legend);
g2.draw(pie);
g2.drawString("Legend",450,80);
}
}