Skip to content

Commit 07bb4d8

Browse files
committed
stash
1 parent 22492e1 commit 07bb4d8

File tree

8 files changed

+773
-0
lines changed

8 files changed

+773
-0
lines changed

ch_32/Exercise32_02.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ch_32;
2+
3+
import java.sql.*;
4+
5+
import ch_32.common.ChartModel;
6+
import ch_32.common.BarChart;
7+
import ch_32.common.Constants;
8+
import ch_32.common.PieChart;
9+
import javafx.scene.Scene;
10+
import javafx.scene.layout.HBox;
11+
import javafx.stage.Stage;
12+
import javafx.application.Application;
13+
14+
/**
15+
* **32.2 (Visualize data) Write a program that displays the number of students in each
16+
* department in a pie chart and a bar chart, as shown in Figure 32.27b. The number
17+
* of students for each department can be obtained from the Student table (see
18+
* Figure 32.4) using the following SQL statement:
19+
* select deptId, count(*)
20+
* from Student
21+
* where deptId is not null
22+
* group by deptId;
23+
* <p>
24+
* <p>
25+
* Chapter 32 Exercises require DB setup see the ReadMe.md for detailed instructions.
26+
*/
27+
public class Exercise32_02 extends Application {
28+
private String[] dataName;
29+
private double[] data;
30+
31+
@Override
32+
public void start(Stage primaryStage) {
33+
initializeDB();
34+
35+
ChartModel chartModel = new ChartModel();
36+
chartModel.setChartData(dataName, data);
37+
38+
PieChart pieChart = new PieChart();
39+
BarChart barChart = new BarChart();
40+
41+
HBox hBox = new HBox(5);
42+
hBox.getChildren().addAll(pieChart, barChart);
43+
44+
Scene scene = new Scene(hBox, 420, 80);
45+
primaryStage.setTitle(getClass().getName());
46+
primaryStage.setScene(scene);
47+
primaryStage.show();
48+
49+
pieChart.setModel(chartModel);
50+
barChart.setModel(chartModel);
51+
}
52+
53+
private void initializeDB() {
54+
try {
55+
Connection conn = DriverManager.getConnection
56+
(Constants.MY_SQL_DB_URL, Constants.MY_SQL_DB_USERNAME, Constants.MY_SQL_DB_PASSWORD);
57+
System.out.println("Database connected\n");
58+
59+
// Connect to the sample database
60+
Statement stmt = conn.createStatement();
61+
ResultSet rs = stmt.executeQuery(
62+
"select deptId, count(*) from Student where deptId is not null group by deptId;");
63+
64+
// Count rows
65+
int count = 0;
66+
while (rs.next()) {
67+
count++;
68+
}
69+
70+
dataName = new String[count];
71+
data = new double[count];
72+
73+
// We have to obtain the result set again
74+
rs = stmt.executeQuery(
75+
"select deptId, count(*) from Student where deptId is not null group by deptId;");
76+
int i = 0;
77+
while (rs.next()) {
78+
dataName[i] = rs.getString(1);
79+
data[i] = rs.getInt(2);
80+
i++;
81+
}
82+
} catch (Exception ex) {
83+
System.out.println("Error: " + ex.getMessage());
84+
}
85+
}
86+
}
87+

ch_32/ReadMe.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Chapter 32 - _Java Database Programming_
2+
### This chapter requires some additional setup because of the dependency on the MySQL database. The following steps are required to run the examples and exercises in this chapter.
3+
- Install Docker
4+
- Run the docker-compose.yml file in the ch_32 directory
5+
- Run the SQL script in the ch_32 directory to create the database and table
6+
7+
### Detailed instructions on how to install Docker and set up the MySQL database can be found here:
8+
### [Medium Article - Install Docker and compose a MySQL db container](https://medium.com/@chrischuck35/how-to-create-a-mysql-instance-with-docker-compose-1598f3cc1bee)

ch_32/common/BarChart.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ch_32.common;
2+
3+
import javafx.event.ActionEvent;
4+
import javafx.event.EventHandler;
5+
import javafx.scene.layout.Pane;
6+
import javafx.scene.paint.Color;
7+
import javafx.scene.shape.Line;
8+
import javafx.scene.shape.Rectangle;
9+
import javafx.scene.text.Text;
10+
11+
public class BarChart extends Pane implements EventHandler<ActionEvent> {
12+
Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE,
13+
Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.PINK,
14+
Color.GRAY};
15+
private ChartModel model;
16+
17+
public BarChart() {
18+
repaint();
19+
}
20+
21+
double width = 200;
22+
double height = 200;
23+
24+
public void repaint() {
25+
if (model == null) return;
26+
27+
String[] dataName = model.getDataName();
28+
double[] data = model.getData();
29+
30+
// Find the maximum value in the data
31+
double max = data[0];
32+
for (int i=1; i<data.length; i++)
33+
max = Math.max(max, data[i]);
34+
35+
double barWidth = (width - 10.0) / data.length - 10;
36+
double maxBarHeight = height - 30;
37+
38+
getChildren().add(new Line(5, height - 10, width - 5, height - 10));
39+
40+
int x = 15;
41+
for (int i = 0; i < data.length; i++) {
42+
double newHeight = maxBarHeight * data[i] / max;
43+
double y = height - 10 - newHeight;
44+
Rectangle rectangle = new Rectangle(x, y, barWidth, newHeight);
45+
rectangle.setFill(colors[i % colors.length]);
46+
47+
getChildren().add(rectangle);
48+
getChildren().add(new Text(x, y - 7, dataName[i]));
49+
x += barWidth + 10;
50+
}
51+
}
52+
53+
public void setModel(ChartModel newModel) {
54+
model = newModel;
55+
model.addActionListener(this);
56+
repaint();
57+
}
58+
59+
public ChartModel getModel() {
60+
return model;
61+
}
62+
63+
@Override
64+
public void handle(ActionEvent e) {
65+
repaint();
66+
}
67+
}

ch_32/common/ChartModel.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ch_32.common;
2+
3+
import java.util.*;
4+
5+
import javafx.event.EventHandler;
6+
import javafx.event.ActionEvent;
7+
8+
public class ChartModel {
9+
private double[] data = {200, 40, 50, 100, 40, 10};
10+
private String[] dataName = {"CS", "Math", "Chem", "Biol", "Phys", "Buss"};
11+
12+
private transient Vector<EventHandler<ActionEvent>> actionListeners;
13+
14+
public ChartModel() {
15+
}
16+
17+
public double[] getData() {
18+
return data;
19+
}
20+
21+
public synchronized void removeActionListener(EventHandler<ActionEvent> l) {
22+
if (actionListeners != null && actionListeners.contains(l)) {
23+
Vector<EventHandler<ActionEvent>> v = (Vector<EventHandler<ActionEvent>>) actionListeners.clone();
24+
v.removeElement(l);
25+
actionListeners = v;
26+
}
27+
}
28+
29+
public synchronized void addActionListener(EventHandler<ActionEvent> l) {
30+
Vector<EventHandler<ActionEvent>> v = actionListeners == null ? new Vector<>(2) : (Vector) actionListeners.clone();
31+
if (!v.contains(l)) {
32+
v.addElement(l);
33+
actionListeners = v;
34+
}
35+
}
36+
37+
protected void fireActionPerformed(ActionEvent e) {
38+
if (actionListeners != null) {
39+
Vector<EventHandler<ActionEvent>> listeners = actionListeners;
40+
int count = listeners.size();
41+
for (int i = 0; i < count; i++) {
42+
listeners.elementAt(i).handle(e);
43+
}
44+
}
45+
}
46+
47+
public void setChartData(String[] newDataName, double[] newData) {
48+
dataName = newDataName;
49+
data = newData;
50+
// System.arraycopy(newData, 0, data, 0, newData.length);
51+
fireActionPerformed(new ActionEvent());
52+
}
53+
54+
public String[] getDataName() {
55+
return dataName;
56+
}
57+
}
58+

ch_32/common/Constants.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ch_32.common;
2+
3+
public class Constants {
4+
5+
private Constants() {
6+
}
7+
8+
public static final String MY_SQL_DB_URL = "jdbc:mysql://localhost:3306/db";
9+
public static final String MY_SQL_DB_USERNAME = "user";
10+
public static final String MY_SQL_DB_PASSWORD = "password";
11+
12+
}

ch_32/common/PieChart.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ch_32.common;
2+
3+
import javafx.event.ActionEvent;
4+
import javafx.event.EventHandler;
5+
import javafx.scene.layout.Pane;
6+
import javafx.scene.paint.Color;
7+
import javafx.scene.shape.Arc;
8+
import javafx.scene.shape.ArcType;
9+
import javafx.scene.text.Text;
10+
11+
public class PieChart extends Pane implements EventHandler<ActionEvent> {
12+
public PieChart() {
13+
repaint();
14+
}
15+
16+
private Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE,
17+
Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.PINK,
18+
Color.GRAY};
19+
private ChartModel model;
20+
21+
double width = 200;
22+
double height = 200;
23+
24+
private void repaint() {
25+
if (model == null) return;
26+
27+
double radius = Math.min(width, height) * 0.5 * 0.9;
28+
double x = width / 2;
29+
double y = height / 2;
30+
31+
String[] dataName = model.getDataName();
32+
double[] data = model.getData();
33+
34+
double total = 0;
35+
for (int i = 0; i < data.length; i++) {
36+
total += data[i];
37+
}
38+
39+
double angle1 = 0;
40+
double angle2 = 0;
41+
for (int i = 0; i < data.length; i++) {
42+
angle1 = angle1 + angle2;
43+
angle2 = Math.ceil(360 * data[i] / total);
44+
Arc arc = new Arc(x, y, radius, radius, angle1, angle2);
45+
arc.setType(ArcType.ROUND);
46+
arc.setFill(colors[i % colors.length]);
47+
getChildren().add(arc);
48+
getChildren().add(new Text(
49+
width / 2 + radius * Math.cos((angle1 + angle2 / 2) * 2 * Math.PI / 360),
50+
height / 2 - radius * Math.sin((angle1 + angle2 / 2) * 2 * Math.PI / 360), dataName[i]));
51+
}
52+
}
53+
54+
public void setModel(ChartModel newModel) {
55+
model = newModel;
56+
model.addActionListener(this);
57+
repaint();
58+
}
59+
60+
public ChartModel getModel() {
61+
return model;
62+
}
63+
64+
@Override
65+
public void handle(ActionEvent e) {
66+
repaint();
67+
}
68+
}

0 commit comments

Comments
 (0)