項目需求:移動鼠標,在jfreechart生成的時間曲線圖的節點上,顯示數據。
?
?
具體過程:
資料1 中,列出了用applet實現該功能的例子,
package gg;
import java.awt.Color;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.*;
public class TimeSeriesDemo1 extends ApplicationFrame
{
? ? public TimeSeriesDemo1(String s)
? ? {
? ? ? ? super(s);
? ? ? ? XYDataset xydataset = createDataset();
? ? ? ? JFreeChart jfreechart = createChart(xydataset);
? ? ? ? ChartPanel chartpanel = new ChartPanel(jfreechart, false);
? ? ? ? chartpanel.setPreferredSize(new Dimension(500, 270));
? ? ? ? chartpanel.setMouseZoomable(true, false);
? ? ? ? setContentPane(chartpanel);
? ? }
? ? private static JFreeChart createChart(XYDataset xydataset)
? ? {
? ? ? ? JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", xydataset, true, true, true);
? ? ? ? jfreechart.setBackgroundPaint(Color.white);
? ? ? ? XYPlot xyplot = (XYPlot)jfreechart.getPlot();
? ? ? ? xyplot.setBackgroundPaint(Color.lightGray);
? ? ? ? xyplot.setDomainGridlinePaint(Color.white);
? ? ? ? xyplot.setRangeGridlinePaint(Color.white);
? ? ? ? xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
? ? ? ? xyplot.setDomainCrosshairVisible(true);
? ? ? ? xyplot.setRangeCrosshairVisible(true);
? ? ? ? org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
? ? ? ? if(xyitemrenderer instanceof XYLineAndShapeRenderer)
? ? ? ? {
? ? ? ? ? ? XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
? ? ? ? ? ? xylineandshaperenderer.setBaseShapesVisible(true);
? ? ? ? ? ? xylineandshaperenderer.setBaseShapesFilled(true);
? ? ? ? }
? ? ? ? DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
? ? ? ? dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
? ? ? ? return jfreechart;
? ? }
? ? private static XYDataset createDataset()
? ? {
? ? ? ? TimeSeries timeseries = new TimeSeries("L&G European Index Trust", org.jfree.data.time.Month.class);
? ? ? ? timeseries.add(new Month(2, 2001), 181.80000000000001D);
? ? ? ? timeseries.add(new Month(3, 2001), 167.30000000000001D);
? ? ? ? timeseries.add(new Month(4, 2001), 153.80000000000001D);
? ? ? ? timeseries.add(new Month(5, 2001), 167.59999999999999D);
? ? ? ? timeseries.add(new Month(6, 2001), 158.80000000000001D);
? ? ? ? timeseries.add(new Month(7, 2001), 148.30000000000001D);
? ? ? ? timeseries.add(new Month(8, 2001), 153.90000000000001D);
? ? ? ? timeseries.add(new Month(9, 2001), 142.69999999999999D);
? ? ? ? timeseries.add(new Month(10, 2001), 123.2D);
? ? ? ? timeseries.add(new Month(11, 2001), 131.80000000000001D);
? ? ? ? timeseries.add(new Month(12, 2001), 139.59999999999999D);
? ? ? ? timeseries.add(new Month(1, 2002), 142.90000000000001D);
? ? ? ? timeseries.add(new Month(2, 2002), 138.69999999999999D);
? ? ? ? timeseries.add(new Month(3, 2002), 137.30000000000001D);
? ? ? ? timeseries.add(new Month(4, 2002), 143.90000000000001D);
? ? ? ? timeseries.add(new Month(5, 2002), 139.80000000000001D);
? ? ? ? timeseries.add(new Month(6, 2002), 137D);
? ? ? ? timeseries.add(new Month(7, 2002), 132.80000000000001D);
? ? ? ? TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust", org.jfree.data.time.Month.class);
? ? ? ? timeseries1.add(new Month(2, 2001), 129.59999999999999D);
? ? ? ? timeseries1.add(new Month(3, 2001), 123.2D);
? ? ? ? timeseries1.add(new Month(4, 2001), 117.2D);
? ? ? ? timeseries1.add(new Month(5, 2001), 124.09999999999999D);
? ? ? ? timeseries1.add(new Month(6, 2001), 122.59999999999999D);
? ? ? ? timeseries1.add(new Month(7, 2001), 119.2D);
? ? ? ? timeseries1.add(new Month(8, 2001), 116.5D);
? ? ? ? timeseries1.add(new Month(9, 2001), 112.7D);
? ? ? ? timeseries1.add(new Month(10, 2001), 101.5D);
? ? ? ? timeseries1.add(new Month(11, 2001), 106.09999999999999D);
? ? ? ? timeseries1.add(new Month(12, 2001), 110.3D);
? ? ? ? timeseries1.add(new Month(1, 2002), 111.7D);
? ? ? ? timeseries1.add(new Month(2, 2002), 111D);
? ? ? ? timeseries1.add(new Month(3, 2002), 109.59999999999999D);
? ? ? ? timeseries1.add(new Month(4, 2002), 113.2D);
? ? ? ? timeseries1.add(new Month(5, 2002), 111.59999999999999D);
? ? ? ? timeseries1.add(new Month(6, 2002), 108.8D);
? ? ? ? timeseries1.add(new Month(7, 2002), 101.59999999999999D);
? ? ? ? TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
? ? ? ? timeseriescollection.addSeries(timeseries);
? ? ? ? timeseriescollection.addSeries(timeseries1);
? ? ? ? return timeseriescollection;
? ? }
? ? public static JPanel createDemoPanel()
? ? {
? ? ? ? JFreeChart jfreechart = createChart(createDataset());
? ? ? ? return new ChartPanel(jfreechart);
? ? }
? ? public static void main(String args[])
? ? {
? ? ? ? TimeSeriesDemo1 timeseriesdemo1 = new TimeSeriesDemo1("Time Series Demo 1");
? ? ? ? timeseriesdemo1.pack();
? ? ? ? RefineryUtilities.centerFrameOnScreen(timeseriesdemo1);
? ? ? ? timeseriesdemo1.setVisible(true);
? ? }
}
運行結果如下圖,鼠標放到一數據點,顯示出數值。
?
?
? 需求是在html頁面上顯示,怎樣將applet轉換到html上呢?
?
又找到 資料2 ,關鍵是這句話:“ JFreeChart所產生的原表如何得知滑鼠位置與這個區域的ToolTip、超連結的對應是什麼。事實上,我們還必須替這張圖表在網頁中建立區域對應表”。區域對應表(也稱熱區)怎么生成呢?在她給出了例子中,step5中,提到了生成的方法。
?//Step 1 建立 Dataset 資料結構物件
?HashMap datas = new HashMap();
?datas.put("Java",new Long(721));
?datas.put(".Net",new Long(543));
?datas.put("PHP",new Long(374));
?datas.put("C++",new Long(438));
?datas.put("Others",new Long(424));?
??
?DefaultPieDataset dataSet = new DefaultPieDataset();
?for(Iterator it = datas.keySet().iterator();it.hasNext();){
??String technology = (String)it.next();????
??dataSet.setValue(technology, (Long)datas.get(technology));
?}?
?
?//Step 3 建立 Plot 物件
??????? PiePlot plot = new PiePlot(dataSet);
??????? plot.setInsets(new RectangleInsets(5, 5, 5, 5));
??????? plot.setURLGenerator(new StandardPieURLGenerator("DetailBarChart.jsp","section"));???????
??????? plot.setToolTipGenerator(new StandardPieItemLabelGenerator());
?plot.setExplodePercent(0, 0.2);????????
???????
??????? //Step 4 產生 JFreeChart 物件,並設定圖表底色??????? ?????????
?JFreeChart chart = new JFreeChart("My First Pie Chart",
??????? JFreeChart.DEFAULT_TITLE_FONT,
??????? plot,
??????? true);? ???????????? ?
??????? chart.setBackgroundPaint(Color.LIGHT_GRAY);????????
???????
?//Step 5 輸出圖表,使用 ServletUtilities 將圖表輸出後由 DisplayChart Servlet 讀取
?ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
?String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);???
?PrintWriter writer = new PrintWriter(out);?
?ChartUtilities.writeImageMap(writer, filename, info,false);
?writer.flush();???????? ?
?String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;???????
%>
<html>
?<body>
??<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">
?</body>
</html>
?
step1-step4,生成一個圖表信息chart,只差將其顯示出來。 step5中,
?
?
1、首先調用 ServletUtilities.saveChartAsPNG方法,將chart保存為一張png圖的同時,把圖表的繪制信息保存到info變量中。
2、將圖片路徑和圖片的繪制信息輸出
?ChartUtilities.writeImageMap(writer, filename, info,false);
3、結束
問題:她這里是將生成的圖片保存到某一路徑下,
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
