日期:2014-05-17  浏览次数:20668 次

探讨 Jfreechart的节点上点击鼠标显示数据!!!!!!
楼里的各位大哥大姐,现在俺有一个想法,不知道能不能得到赐教.问题如下: 
现在我用jfreechart做了折线图,如果数据多的时候显示到节点上,会都挤到一起,看不清楚,我现在想,在得到图形的时候不在节点上显示数据,而是当把鼠标放到某个节点上的时候,显示此节点对应的数据,不知道我这个想法是不是有道理?如果有,有没有哪位大哥大姐给点实例代码,感谢感谢啊!

------解决方案--------------------
在代码里设置图片的宽度,比如按数据量动态设置宽度。

另外,貌似jfreechart不能实现鼠标点上去显示数据。因为它生成的图片是png,jpeg,bmp等静态的图片。最新版的能不能做成flash,不知道。
------解决方案--------------------
不好意思 现在才来 。
 你在4楼的代码应该可以实现啊。哦,对了,你用的版本是不是是1.0.6的?

给你一个例子,你试试吧:
 



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.a