Java tutorial
/* Copyright 2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Developed by : Sandeep Soni [ http://sonisandeep.blogspot.com, Email : Sandeep.Soni at yahoo.com ] */ package com.sun.portal.os.portlets; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.portlet.PortletPreferences; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.general.Dataset; import org.jfree.data.general.PieDataset; import org.jfree.data.jdbc.JDBCCategoryDataset; import org.jfree.data.jdbc.JDBCPieDataset; import org.jfree.data.jdbc.JDBCXYDataset; public class ChartServlet extends HttpServlet { private static final String DEFAULT_TIME_SERIES_SQL = "SELECT date, series1 , series2 , series3 FROM timeseries"; private static final String DEFAULT_BAR_CHART_SQL = "SELECT category, series1 , series2 , series3 FROM barchart"; private static final String DEFAULT_PIE_CHART_SQL = "SELECT product_category, bug_count FROM piechart"; private static String MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver"; private ServletConfig _config; private static final long serialVersionUID = 1L; private static final String CONTENT_TYPE_IMG_PNG = "image/png"; private static final String TIME_CHART = "time"; private static final String BAR_CHART = "bar"; private static final String PIE_CHART = "pie"; private static String JDBC_URL = "jdbc:mysql://localhost/chart?user=chart&password=chart"; public void init(ServletConfig config) throws ServletException { super.init(config); _config = config; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputStream out = response.getOutputStream(); try { JFreeChart chart = createChart(request); if (chart != null) { response.setContentType(CONTENT_TYPE_IMG_PNG); ChartUtilities.writeChartAsPNG(out, chart, 400, 300); } } catch (Exception e) { System.err.println(e.toString()); } finally { out.close(); } } private Connection getDatabaseConnection(HttpServletRequest request) throws SQLException, NoPreferredDbSetException { HttpSession session = request.getSession(true); PortletPreferences prefs = (PortletPreferences) session.getAttribute("PORTLET_PREFERENCES"); if (prefs == null) throw new NoPreferredDbSetException("No Preferences Set.. Don't know what DB to connect to"); String driverUrl = prefs.getValue(Constants.PREF_JDBC_DRIVER_URL, JDBC_URL); String driverClass = prefs.getValue(Constants.PREF_JDBC_DRIVER_CLASS, MYSQL_JDBC_DRIVER); debugMessage("Driver Class: " + driverClass); debugMessage("Driver URL : " + driverUrl); try { Class.forName(driverClass); } catch (ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } return DriverManager.getConnection(driverUrl); } private JFreeChart createChart(HttpServletRequest request) { JFreeChart chart = null; try { HttpSession session = request.getSession(true); PortletPreferences prefs = (PortletPreferences) session.getAttribute("PORTLET_PREFERENCES"); if (prefs == null) throw new NoPreferredDbSetException("Preferences not passed in from portlet"); String type = prefs.getValue(Constants.PREF_CHART_TYPE, PIE_CHART); debugMessage("Chart type :" + type); if (type.equals(PIE_CHART)) { String sql = prefs.getValue(Constants.PREF_PIE_CHART_SQL, DEFAULT_PIE_CHART_SQL); PieDataset data = (PieDataset) generatePieDataSet(getDatabaseConnection(request), sql); chart = ChartFactory.createPieChart("Pie Chart", data, true, true, false); } else if (type.equals(BAR_CHART)) { String sql = prefs.getValue(Constants.PREF_BAR_CHART_SQL, DEFAULT_BAR_CHART_SQL); JDBCCategoryDataset data = (JDBCCategoryDataset) generateBarChartDataSet( getDatabaseConnection(request), sql); chart = ChartFactory.createBarChart3D("Bar Chart", "Category", "Value", data, PlotOrientation.VERTICAL, true, true, false); } else if (type.equals(TIME_CHART)) { String sql = prefs.getValue(Constants.PREF_TIME_SERIES_SQL, DEFAULT_TIME_SERIES_SQL); JDBCXYDataset data = (JDBCXYDataset) generateXYDataSet(getDatabaseConnection(request), sql); chart = ChartFactory.createTimeSeriesChart("Time Series Chart", "Date", "Rate", data, true, true, false); } } catch (NoPreferredDbSetException npdbs) { npdbs.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return chart; } private JDBCXYDataset generateXYDataSet(Connection con, String sql) { JDBCXYDataset data = null; try { data = new JDBCXYDataset(con); data.executeQuery(sql); con.close(); } catch (SQLException e) { System.err.print("SQLException: "); System.err.println(e.getMessage()); } catch (Exception e) { System.err.print("Exception: "); System.err.println(e.getMessage()); } return data; } private JDBCCategoryDataset generateBarChartDataSet(Connection con, String sql) { JDBCCategoryDataset data = null; try { data = new JDBCCategoryDataset(con); data.executeQuery(sql); con.close(); } catch (SQLException e) { System.err.print("SQLException: "); System.err.println(e.getMessage()); } catch (Exception e) { System.err.print("Exception: "); System.err.println(e.getMessage()); } return data; } protected Dataset generatePieDataSet(Connection con, String sql) { JDBCPieDataset data = null; try { data = new JDBCPieDataset(con); data.executeQuery(sql); con.close(); } catch (SQLException e) { System.err.print("SQLException: "); System.err.println(e.getMessage()); } catch (Exception e) { System.err.print("Exception: "); System.err.println(e.getMessage()); } return data; } protected static void debugMessage(String message) { System.out.println("CHARTPORTLET ===> " + message); } public static void main(String[] args) throws FileNotFoundException, IOException { // ChartUtilities.writeChartAsPNG(new // FileOutputStream("c:\\temp\\test.png"), main.createChart(TIME_CHART), // 400, 300); } }