標籤:style blog color io java ar for 資料 div
jfc在jsp頁面畫圖,不將圖片存在伺服器端,只存入會話session。其中主要用到jfc的一個servlet類。
<%@ page contentType="text/html;charset=gb2312" errorPage="../common/error.jsp" %><%@ page import="dqz.jsp.*" %><%@ page import="java.sql.*" %><%@ page import="javax.sql.*" %><%@ page import="javax.naming.*" %><%@ include file="../common/config.jsp"%><%@ include file="reporter_session.jsp"%><%@ page import=" org.jfree.chart.servlet.ServletUtilities, java.awt.Font, java.awt.Color, org.jfree.chart.ChartFactory, org.jfree.chart.ChartFrame, org.jfree.chart.JFreeChart, org.jfree.chart.StandardChartTheme, org.jfree.chart.plot.CategoryPlot, org.jfree.chart.plot.PlotOrientation, org.jfree.data.category.CategoryDataset, org.jfree.data.category.DefaultCategoryDataset,org.jfree.chart.ChartUtilities,org.jfree.chart.axis.CategoryAxis,org.jfree.chart.axis.NumberAxis,org.jfree.chart.axis.ValueAxis,org.jfree.chart.labels.StandardCategoryItemLabelGenerator,org.jfree.chart.renderer.category.BarRenderer,org.jfree.chart.title.TextTitle, java.io.FileOutputStream,java.io.IOException, java.io.PrintWriter,java.util.ArrayList, java.util.HashMap, java.util.List,java.util.Map,java.text.DecimalFormat, java.text.NumberFormat,java.io.FileOutputStream,java.io.IOException,java.io.PrintWriter,java.sql.Connection,java.sql.DriverManager,java.sql.PreparedStatement,java.sql.ResultSet,java.sql.SQLException,java.util.ArrayList,java.util.List,org.jfree.chart.servlet.ServletUtilities, java.util.Map,org.jfree.chart.labels.StandardPieSectionLabelGenerator,org.jfree.chart.labels.StandardPieToolTipGenerator,org.jfree.chart.plot.PiePlot3D,org.jfree.data.general.DefaultPieDataset,org.jfree.data.general.PieDataset, org.jfree.chart.title.LegendTitle "%>//===========================長條圖測試開始===================================================DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(610, "廣州", "豬肉");dataset.addValue(220, "廣州", "牛肉");dataset.addValue(530, "廣州", "雞肉");dataset.addValue(340, "廣州", "魚肉");JFreeChart mBarChart = ChartFactory.createBarChart3D("肉類銷量統計圖","肉類","銷量",dataset,PlotOrientation.VERTICAL,false,false,false); //圖表標題設定 TextTitle mTextTitle = mBarChart.getTitle(); mTextTitle.setFont(new Font("黑體", Font.BOLD, 20)); //mBarChart.setTitle(new TextTitle("學校人員分布圖",new Font("黑體", Font.BOLD, 20))); //圖表圖例設定 LegendTitle mLegend = mBarChart.getLegend(); if(mLegend != null) mLegend.setItemFont(new Font("宋體", Font.CENTER_BASELINE, 15)); //mBarChart.getLegend().setItemFont(new Font("宋體", Font.CENTER_BASELINE, 15)); //設定柱狀圖軸 CategoryPlot mPlot = (CategoryPlot)mBarChart.getPlot(); //x軸 CategoryAxis mDomainAxis = mPlot.getDomainAxis(); //設定x軸標題的字型 mDomainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 15)); //設定x軸座標字型 mDomainAxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 15)); //y軸 ValueAxis mValueAxis = mPlot.getRangeAxis(); //設定y軸標題字型 mValueAxis.setLabelFont(new Font("宋體", Font.PLAIN, 15)); //設定y軸座標字型 mValueAxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 15)); //柱體顯示數值 BarRenderer mRenderer= new BarRenderer(); mRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); mRenderer.setItemLabelFont(new Font("宋體", Font.PLAIN, 10)); mRenderer.setItemLabelsVisible(true); mPlot.setRenderer(mRenderer); String filename = ServletUtilities.saveChartAsPNG(mBarChart, 800, 400, null, session);String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;//===========================長條圖測試結束===================================================//===========================餅圖測試開始=================================================== //設定資料來源 DefaultPieDataset mpieDataset = new DefaultPieDataset(); mpieDataset.setValue(" 市場前期", new Double(10)); mpieDataset.setValue(" 立項", new Double(15)); mpieDataset.setValue(" 計劃", new Double(20)); mpieDataset.setValue(" 需求與設計", new Double(10)); mpieDataset.setValue(" 執行控制", new Double(25)); mpieDataset.setValue(" 收尾", new Double(10)); mpieDataset.setValue(" 營運",new Double(10)); //建立圖表 JFreeChart mpieChart = ChartFactory.createPieChart3D("項目進度分布", mpieDataset, true, true, false); //設定圖表標題 mpieChart.setTitle(new TextTitle("項目狀態分布", new Font("黑體",Font.CENTER_BASELINE, 20))); //設定Legend字型 mpieChart.getLegend().setItemFont(new Font("宋體", Font.ROMAN_BASELINE, 15)); PiePlot3D mPiePlot = (PiePlot3D)mpieChart.getPlot(); //以預設顯示百分比 //mPiePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT)); // 圖片中顯示百分比:自訂方式,{0} 表示選項, {1} 表示數值, {2} 表示所佔比例 ,小數點後兩位 mPiePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); // 底部圖例顯示百分比:自訂方式, {0} 表示選項, {1} 表示數值, {2} 表示所佔比例 mPiePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})")); //設定餅表徵圖簽字型 mPiePlot.setLabelFont(new Font("宋體", Font.PLAIN, 15)); String piefilename = ServletUtilities.saveChartAsPNG(mpieChart, 800, 400, null, session); String piegraphURL = request.getContextPath() + "/DisplayChart?filename=" + piefilename;//===========================餅圖測試結束===================================================//===========================折線圖測試開始=================================================== StandardChartTheme mChartThemezxt = new StandardChartTheme("CN"); mChartThemezxt.setLargeFont(new Font("黑體", Font.BOLD, 20)); mChartThemezxt.setExtraLargeFont(new Font("宋體", Font.PLAIN, 15)); mChartThemezxt.setRegularFont(new Font("宋體", Font.PLAIN, 15)); ChartFactory.setChartTheme(mChartThemezxt); // row keys... String series1 = "First"; String series2 = "Second"; String series3 = "Third"; // column keys... String type1 = "Type 1"; String type2 = "Type 2"; String type3 = "Type 3"; String type4 = "Type 4"; String type5 = "Type 5"; String type6 = "Type 6"; String type7 = "Type 7"; String type8 = "Type 8"; // create the dataset... DefaultCategoryDataset dataset_zxt = new DefaultCategoryDataset(); dataset_zxt.addValue(1.0, series1, type1); dataset_zxt.addValue(4.0, series1, type2); dataset_zxt.addValue(3.0, series1, type3); dataset_zxt.addValue(5.0, series1, type4); dataset_zxt.addValue(5.0, series1, type5); dataset_zxt.addValue(7.0, series1, type6); dataset_zxt.addValue(7.0, series1, type7); dataset_zxt.addValue(0.0, series1, type8); dataset_zxt.addValue(5.0, series2, type1); dataset_zxt.addValue(7.0, series2, type2); dataset_zxt.addValue(6.0, series2, type3); dataset_zxt.addValue(8.0, series2, type4); dataset_zxt.addValue(4.0, series2, type5); dataset_zxt.addValue(4.0, series2, type6); dataset_zxt.addValue(2.0, series2, type7); dataset_zxt.addValue(1.0, series2, type8); dataset_zxt.addValue(4.0, series3, type1); dataset_zxt.addValue(3.0, series3, type2); dataset_zxt.addValue(2.0, series3, type3); dataset_zxt.addValue(3.0, series3, type4); dataset_zxt.addValue(6.0, series3, type5); dataset_zxt.addValue(3.0, series3, type6); dataset_zxt.addValue(4.0, series3, type7); dataset_zxt.addValue(3.0, series3, type8); JFreeChart mzxtChart = ChartFactory.createLineChart( "折線圖", "時間", "數量", dataset_zxt, PlotOrientation.VERTICAL, true, true, false); CategoryPlot mzxtPlot = (CategoryPlot)mzxtChart.getPlot(); mzxtPlot.setBackgroundPaint(Color.LIGHT_GRAY); mzxtPlot.setRangeGridlinePaint(Color.BLUE);//背景底部橫虛線 mzxtPlot.setOutlinePaint(Color.RED);//邊界線 mzxtChart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));//圖列 CategoryAxis mzxtdomainAxis = mzxtPlot.getDomainAxis(); //X座標 mzxtdomainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); //X mzxtdomainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); ValueAxis mzxtValueAxis = mzxtPlot.getRangeAxis(); mzxtValueAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); //Y mzxtValueAxis.setLabelFont(new Font("黑體", Font.PLAIN, 12)); TextTitle zxttextTitle = mzxtChart.getTitle();//座標標題設定 zxttextTitle.setFont(new Font("黑體", Font.PLAIN, 20)); String zxtfilename = ServletUtilities.saveChartAsPNG(mzxtChart, 800, 400, null, session); String zxtgraphURL = request.getContextPath() + "/DisplayChart?filename=" + zxtfilename;//===========================折線圖測試結束=================================================== %><img src="<%= graphURL %>" width=800 height=400 border=0 usemap="#<%= filename %>"><br/><img src="<%= piegraphURL %>" width=800 height=400 border=0 usemap="#<%= piefilename %>"><br/><img src="<%= zxtgraphURL %>" width=800 height=400 border=0 usemap="#<%= zxtfilename %>">
jfc在jsp頁面畫圖,不將圖片存在伺服器端,只存入會話session(可用)