Jfreechart的节点上放上鼠标显示数据

xiaoxiao2026-06-08  5

项目需求:移动鼠标,在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、结束

问题:她这里是将生成的图片保存到某一路径下,

转载请注明原文地址: https://www.6miu.com/read-5049796.html

最新回复(0)