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 * <B>XyChartByJDBC </B>. 27 * 28 * @author $Author: wildcats $ 29 * @version $Revision: 5 $ 30 */ 31 public class XYChartByJDBC { 32 33 private ChartXMLDTO chartXML = null; 34 35 private LineChart lineChart = null; 36 37 private Dataset getChart(Connection connection) { 38 lineChart = LineChartFactory.create(); 39 JDBCXYDatasetEnhance enhance = (JDBCXYDatasetEnhance) lineChart 40 .getDataSet(connection, chartXML.getSQL().getText()); 41 List list = new LinkedList(); 42 Item[] items = chartXML.getSeries(); 43 for (int i = 0, size = items.length; i < size; i++) { 44 list.add(items[i].getName()); 45 } 46 enhance.setLegendItemLabels((String[]) list.toArray(new String[0])); 47 return enhance; 48 } 49 50 private JFreeChart getJFreeChart(Dataset dataset) { 51 ChartDTO data = new ChartDTO(); 52 Label label = chartXML.getLabel(); 53 data.setTitle(label.getTitle()); 54 data.setXAxisLabel(label.getXLabel()); 55 data.setYAxisLabel(label.getYLabel()); 56 data.setOrientation(PlotOrientation.VERTICAL); 57 data.setDataset(dataset); 58 data.setLegend(true); 59 data.setTooltips(true); 60 data.setUrls(true); 61 JFreeChart myChart = lineChart.getJFreeChart(data); 62 XYPlot plot = (XYPlot) myChart.getPlot(); 63 64 XYItemRenderer r = plot.getRenderer(); 65 if (r instanceof XYLineAndShapeRenderer) { 66 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; 67 renderer.setDefaultShapesVisible(true); 68 renderer.setDefaultShapesFilled(true); 69 } 70 return myChart; 71 } 72 73 /*** 74 * 実行. 75 * 76 * @param configureFileName 77 * 設定ファイル名 78 * @param driverClassName 79 * JDBCドライバクラス名 80 * @param url 81 * JDBC接続URL 82 */ 83 public void execute(String configureFileName, String driverClassName, 84 String url) { 85 this.execute(configureFileName, driverClassName, url, null, null); 86 } 87 88 /*** 89 * 実行 90 * 91 * @param configureFileName 92 * 設定ファイル名 93 * @param driverClassName 94 * JDBCドライバクラス名 95 * @param url 96 * JDBC接続URL 97 * @param username 98 * データベースユーザ名 99 * @param password 100 * データベースパスワード 101 */ 102 public void execute(String configureFileName, String driverClassName, 103 String url, String username, String password) { 104 JDBCConnectConfigure configure = new JDBCConnectConfigure(); 105 configure.setDriverClass(driverClassName); 106 configure.setUrl(url); 107 configure.setUser(username); 108 configure.setPassword(password); 109 this.execute(configureFileName, configure); 110 } 111 112 /*** 113 * 実行. 114 * 115 * @param configureFileName 116 * 設定ファイル名 117 * @param configure 118 * JDBCConfigure 119 */ 120 public void execute(String configureFileName, JDBCConnectConfigure configure) { 121 XMLParser impl = XMLParserFactory.create(); 122 chartXML = impl.read(configureFileName); 123 try { 124 Class.forName(configure.getDriverClass()); 125 } catch (ClassNotFoundException e2) { 126 throw new RuntimeException(e2); 127 } 128 129 System.out.println("Connect to " + configure.getUrl()); 130 System.out.println("User : " + configure.getUser() + " Pwd : " 131 + configure.getPassword()); 132 133 Connection connection = null; 134 135 try { 136 connection = DriverManagerWrapper.getConnection(configure); 137 XYChartByJDBC xyChartByJDBC = new XYChartByJDBC(); 138 Dataset dataset = xyChartByJDBC.getChart(connection); 139 JFreeChart myChart = xyChartByJDBC.getJFreeChart(dataset); 140 ChartFile fileType = chartXML.getFile(); 141 fileType.saveChart(myChart); 142 } finally { 143 if (connection != null) { 144 try { 145 connection.close(); 146 } catch (SQLException ignore) { 147 } 148 } 149 } 150 } 151 152 public static void main(String args[]) { 153 if (args.length < 3) { 154 System.err 155 .println("Usage : java XYChartByJDBC xmlfilename driverClassName databaseURL [databaseUser] [databasePassWord]"); 156 System.exit(-1); 157 } 158 159 XYChartByJDBC xyChartByJDBC = new XYChartByJDBC(); 160 161 if (args.length == 5) { 162 xyChartByJDBC.execute(args[0], args[1], args[2], args[3], args[4]); 163 } else if (args.length == 3) { 164 xyChartByJDBC.execute(args[0], args[1], args[2]); 165 } 166 167 } 168 169 }