1 |
|
package com.wikihouse.wildcats0201.jdbcchart; |
2 |
|
|
3 |
|
import java.sql.Connection; |
4 |
|
import java.sql.SQLException; |
5 |
|
import java.util.LinkedList; |
6 |
|
import java.util.List; |
7 |
|
|
8 |
|
import org.jfree.chart.JFreeChart; |
9 |
|
import org.jfree.chart.plot.PlotOrientation; |
10 |
|
import org.jfree.chart.plot.XYPlot; |
11 |
|
import org.jfree.chart.renderer.xy.XYItemRenderer; |
12 |
|
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; |
13 |
|
import org.jfree.data.general.Dataset; |
14 |
|
|
15 |
|
import com.wikihouse.wildcats0201.jdbcchart.dom4j.XMLParser; |
16 |
|
import com.wikihouse.wildcats0201.jdbcchart.dom4j.XMLParserFactory; |
17 |
|
import com.wikihouse.wildcats0201.jdbcchart.dto.ChartDTO; |
18 |
|
import com.wikihouse.wildcats0201.jdbcchart.dto.ChartFile; |
19 |
|
import com.wikihouse.wildcats0201.jdbcchart.dto.ChartXMLDTO; |
20 |
|
import com.wikihouse.wildcats0201.jdbcchart.dto.Item; |
21 |
|
import com.wikihouse.wildcats0201.jdbcchart.dto.Label; |
22 |
|
import com.wikihouse.wildcats0201.jdbcchart.jdbcutil.DriverManagerWrapper; |
23 |
|
import com.wikihouse.wildcats0201.jdbcchart.jdbcutil.JDBCConnectConfigure; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
0 |
public class XYChartByJDBC { |
32 |
|
|
33 |
0 |
private ChartXMLDTO chartXML = null; |
34 |
|
|
35 |
0 |
private LineChart lineChart = null; |
36 |
|
|
37 |
|
private Dataset getChart(Connection connection) { |
38 |
0 |
lineChart = LineChartFactory.create(); |
39 |
0 |
JDBCXYDatasetEnhance enhance = (JDBCXYDatasetEnhance) lineChart |
40 |
|
.getDataSet(connection, chartXML.getSQL().getText()); |
41 |
0 |
List list = new LinkedList(); |
42 |
0 |
Item[] items = chartXML.getSeries(); |
43 |
0 |
for (int i = 0, size = items.length; i < size; i++) { |
44 |
0 |
list.add(items[i].getName()); |
45 |
|
} |
46 |
0 |
enhance.setLegendItemLabels((String[]) list.toArray(new String[0])); |
47 |
0 |
return enhance; |
48 |
|
} |
49 |
|
|
50 |
|
private JFreeChart getJFreeChart(Dataset dataset) { |
51 |
0 |
ChartDTO data = new ChartDTO(); |
52 |
0 |
Label label = chartXML.getLabel(); |
53 |
0 |
data.setTitle(label.getTitle()); |
54 |
0 |
data.setXAxisLabel(label.getXLabel()); |
55 |
0 |
data.setYAxisLabel(label.getYLabel()); |
56 |
0 |
data.setOrientation(PlotOrientation.VERTICAL); |
57 |
0 |
data.setDataset(dataset); |
58 |
0 |
data.setLegend(true); |
59 |
0 |
data.setTooltips(true); |
60 |
0 |
data.setUrls(true); |
61 |
0 |
JFreeChart myChart = lineChart.getJFreeChart(data); |
62 |
0 |
XYPlot plot = (XYPlot) myChart.getPlot(); |
63 |
|
|
64 |
0 |
XYItemRenderer r = plot.getRenderer(); |
65 |
0 |
if (r instanceof XYLineAndShapeRenderer) { |
66 |
0 |
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; |
67 |
0 |
renderer.setDefaultShapesVisible(true); |
68 |
0 |
renderer.setDefaultShapesFilled(true); |
69 |
|
} |
70 |
0 |
return myChart; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
public void execute(String configureFileName, String driverClassName, |
84 |
|
String url) { |
85 |
0 |
this.execute(configureFileName, driverClassName, url, null, class="keyword">null); |
86 |
0 |
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
public void execute(String configureFileName, String driverClassName, |
103 |
|
String url, String username, String password) { |
104 |
0 |
JDBCConnectConfigure configure = new JDBCConnectConfigure(); |
105 |
0 |
configure.setDriverClass(driverClassName); |
106 |
0 |
configure.setUrl(url); |
107 |
0 |
configure.setUser(username); |
108 |
0 |
configure.setPassword(password); |
109 |
0 |
this.execute(configureFileName, configure); |
110 |
0 |
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
public void execute(String configureFileName, JDBCConnectConfigure configure) { |
121 |
0 |
XMLParser impl = XMLParserFactory.create(); |
122 |
0 |
chartXML = impl.read(configureFileName); |
123 |
|
try { |
124 |
0 |
Class.forName(configure.getDriverClass()); |
125 |
0 |
} catch (ClassNotFoundException e2) { |
126 |
0 |
throw new RuntimeException(e2); |
127 |
|
} |
128 |
|
|
129 |
0 |
System.out.println("Connect to " + configure.getUrl()); |
130 |
0 |
System.out.println("User : " + configure.getUser() + " Pwd : " |
131 |
|
+ configure.getPassword()); |
132 |
|
|
133 |
0 |
Connection connection = null; |
134 |
|
|
135 |
|
try { |
136 |
0 |
connection = DriverManagerWrapper.getConnection(configure); |
137 |
0 |
XYChartByJDBC xyChartByJDBC = new XYChartByJDBC(); |
138 |
0 |
Dataset dataset = xyChartByJDBC.getChart(connection); |
139 |
0 |
JFreeChart myChart = xyChartByJDBC.getJFreeChart(dataset); |
140 |
0 |
ChartFile fileType = chartXML.getFile(); |
141 |
0 |
fileType.saveChart(myChart); |
142 |
0 |
} finally { |
143 |
0 |
if (connection != null) { |
144 |
|
try { |
145 |
0 |
connection.close(); |
146 |
0 |
} catch (SQLException ignore) { |
147 |
0 |
} |
148 |
|
} |
149 |
|
} |
150 |
0 |
} |
151 |
|
|
152 |
|
public static void main(String args[]) { |
153 |
0 |
if (args.length < 3) { |
154 |
0 |
System.err |
155 |
|
.println("Usage : java XYChartByJDBC xmlfilename driverClassName databaseURL [databaseUser] [databasePassWord]"); |
156 |
0 |
System.exit(-1); |
157 |
|
} |
158 |
|
|
159 |
0 |
XYChartByJDBC xyChartByJDBC = new XYChartByJDBC(); |
160 |
|
|
161 |
0 |
if (args.length == 5) { |
162 |
0 |
xyChartByJDBC.execute(args[0], args[1], args[2], args[3], args[4]); |
163 |
0 |
} else if (args.length == 3) { |
164 |
0 |
xyChartByJDBC.execute(args[0], args[1], args[2]); |
165 |
|
} |
166 |
|
|
167 |
0 |
} |
168 |
|
|
169 |
|
} |