内容:x轴为时间轴和双y轴的散点图形和折线图线
解决问题:
1.线图上标记(时间类型数据转化为long型);
2.双y轴;
3.图例显示;
4.曲线可随意添加;
5.鼠标指向数据点,提示数据内容;
6.Applet 嵌入JSP中
package applet; import java.awt.Color; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.swing.JApplet; import javax.swing.JList; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.annotations.XYPointerAnnotation; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.labels.StandardXYToolTipGenerator; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.StandardXYItemRenderer; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.data.time.Second; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.IntervalXYDataset; import org.jfree.data.xy.XYDataset; import org.jfree.ui.TextAnchor; import Util.MessageBean; public class AstpApplet extends JApplet { public static String StrToGB2312(String str) { if (str == null) str = ""; try { str = new String(str.getBytes("ISO-8859-1"), "GB2312"); } catch (Exception ex) { return ""; } return str; } private static String selectedParaNo = null; private static String beginDate = null; private static String endDate = null; private JList dwplList = null; public void init() { super.init(); // selectedParaNo = getParameter("SelectedParaNo"); // beginDate = getParameter("BeginDate"); // endDate = getParameter("EndDate"); selectedParaNo = "para1;para2;para3;para4;para5;para6;para7;para8;para9;para10;para11"; beginDate = "2010-05-31 07:48:55"; endDate = "2010-05-31 20:48:55"; } public void start() { mainApplet(); } public void destory() { super.destroy(); } public void mainApplet() { JPanel chartPanel = createDemoPanel(); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); } public static JPanel createDemoPanel() { ArrayList dataRslt = new ArrayList(); ServletToApplet servlettoApplet = new ServletToApplet(); MessageBean messageBean = servlettoApplet.getParaNameAndNoMap(beginDate,endDate,selectedParaNo); dataRslt = (ArrayList)messageBean.getMsg(); String[] pParaList = selectedParaNo.split(";"); JFreeChart localJFreeChart = createChart(dataRslt, pParaList); return new ChartPanel(localJFreeChart); } private static JFreeChart createChart(List dwplList, String[] pParaList) { XYDataset xydataset = createPriceDataset(dwplList,pParaList); String s = "CHART1 TEST"; JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(s, "Date", "A", xydataset, true, true, false); XYPlot xyplot = (XYPlot) jfreechart.getPlot(); DecimalFormat decimalformat2 = new DecimalFormat("00.00"); NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis(); numberaxis.setNumberFormatOverride(decimalformat2); numberaxis.setAutoRangeIncludesZero(false); XYItemRenderer xyitemrenderer = xyplot.getRenderer(); xyitemrenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator( "{0}: ({1}, {2})", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new DecimalFormat("0,000.00"))); StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer(); standardxyitemrenderer1.setSeriesPaint(0, Color.blue); standardxyitemrenderer1.setBaseShapesVisible(true); NumberAxis numberaxis1 = new NumberAxis("T °C"); numberaxis1.setAutoRangeIncludesZero(false); numberaxis1.setNumberFormatOverride(decimalformat2); numberaxis1.setLabelPaint(Color.blue); numberaxis1.setTickLabelPaint(Color.blue); xyplot.setBackgroundPaint(new Color(205, 241, 197)); xyplot.setRangeAxis(1, numberaxis1); xyplot.setDataset(1, createVolumeDataset(dwplList)); xyplot.setRangeAxis(1, numberaxis1); xyplot.mapDatasetToRangeAxis(1, 1); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); XYPointerAnnotation xypointerannotation2 = null; String CTime = ""; double fvalue = 0.0f; double fmaxvalue = 0.0f; for (int i = 0; i < dwplList.size(); i++) { fvalue =((Double) ((Map) dwplList.get(i)).get("para11")).doubleValue(); if(fmaxvalue < fvalue){ CTime = (String) ((Map) dwplList.get(i)).get("Ctime"); fmaxvalue = fvalue; } } try { xypointerannotation2 = new XYPointerAnnotation( "T ("+ CTime + "," + fmaxvalue+")", (double)(format.parse(CTime).getTime()), fmaxvalue, 1.5707963267948966D); xypointerannotation2.setTextAnchor(TextAnchor.BOTTOM_LEFT); xypointerannotation2.setPaint(Color.black); xypointerannotation2.setArrowPaint(Color.black); xypointerannotation2.setTextAnchor(TextAnchor.TOP_CENTER); standardxyitemrenderer1.addAnnotation(xypointerannotation2); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } standardxyitemrenderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator( "{0}: ({1}, {2})", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new DecimalFormat("00.00"))); xyplot.setRenderer(1, standardxyitemrenderer1); return jfreechart; } private static XYDataset createPriceDataset(List hbaceList,String[] pParaList) { try { final TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.setDomainIsPointsInTime(true); TimeSeries timeseries[] = new TimeSeries[pParaList.length]; for (int i = 0; i < pParaList.length; i++) { timeseries[i] = new TimeSeries(ParaNoToParaName(pParaList[i]),org.jfree.data.time.Second.class); } for (int i = 0; i < hbaceList.size(); i++) { String CTime = (String) ((Map) hbaceList.get(i)).get("Ctime"); String[] strs = CTime.split("[- :]"); int a = Integer.parseInt(strs[0]); int b = Integer.parseInt(strs[1]); int c = Integer.parseInt(strs[2]); int d = Integer.parseInt(strs[3]); int e = Integer.parseInt(strs[4]); int f = Integer.parseInt(strs[5].substring(0, 2)); for (int j = 0; j < pParaList.length - 1; j++) { timeseries[j].add(new Second(f, e, d, c, b, a), (float)((Double) ((Map) hbaceList.get(i)).get(pParaList[j])) .doubleValue()); } } for (int i = 0; i < pParaList.length - 1; i++) dataset.addSeries(timeseries[i]); return dataset; } catch (Exception e) { e.printStackTrace(); } return null; } private static IntervalXYDataset createVolumeDataset(List dwplList) { try { TimeSeries timeseries = new TimeSeries("T", org.jfree.data.time.Second.class); for (int i = 0; i < dwplList.size(); i++) { String CTime = (String) ((Map) dwplList.get(i)).get("Ctime"); String[] strs = CTime.split("[- :]"); int a = Integer.parseInt(strs[0]); int b = Integer.parseInt(strs[1]); int c = Integer.parseInt(strs[2]); int d = Integer.parseInt(strs[3]); int e = Integer.parseInt(strs[4]); int f = Integer.parseInt(strs[5].substring(0, 2)); timeseries.add(new Second(f, e, d, c, b, a), (float)((Double) ((Map) dwplList.get(i)).get("para11")) .doubleValue()); } return new TimeSeriesCollection(timeseries); } catch (Exception e) { e.printStackTrace(); } return null; } public static String ParaNoToParaName(String pString) { String ParaName = ""; if(null == pString || "".equals(pString)) { return ParaName; } int cmd = Integer.valueOf(pString.substring(4,pString.length())); switch (cmd){ case 1: ParaName = "SUN_1020 nm"; break; case 2: ParaName = "SUNI_1246 nm"; break; case 3: ParaName = "SUN_870 nm"; break; case 4: ParaName = "SUN_670 nm"; break; case 5: ParaName = "SUN_440 nm"; break; case 6: ParaName = "SUN_500 nm"; break; case 7: ParaName = "SUNI_1020 nm"; break; case 8: ParaName = "SUN_936 nm"; break; case 9: ParaName = "SUN_380 nm"; break; case 10: ParaName = "SUN_340 nm"; break; case 11: ParaName = "Temperature"; break; } return ParaName; } }