JFreeChart 折线图中,鼠标移动上去显示坐标信息。

我们使用JFreeChart生成的折线图后,鼠标放上去可以动态显示坐标信息,但是怎么在编程中获得此横纵坐标的值呢,请高手指点。
代码如下:
package jfreechart.good;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.Range;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class CopyOfLineChartMyTest_XYData extends ApplicationFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private float start = 0;
private float end = 40;
private int node = 30;
private int length = 600;
private int height = 600;

public CopyOfLineChartMyTest_XYData(final String title) {
super(title);
final XYDataset dataset = createDataset();
final JFreeChart chart = createJFreeChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(length, height));
setContentPane(chartPanel);
}

// 数据源封装
private static XYDataset createDataset()
{
XYSeries xyseries = new XYSeries("First"); //先产生XYSeries 对象
xyseries.add(1.0D, 11.0D);
xyseries.add(2D, 14D);
xyseries.add(3D, 10D);
xyseries.add(4D, 15D);
xyseries.add(5D, 19D);
xyseries.add(6D, 17D);
xyseries.add(7D, 14D);
xyseries.add(8D, 18D);

XYSeries xyseries1 = new XYSeries("Second");
xyseries1.add(1.0D, 15D);
xyseries1.add(2D, 17D);
xyseries1.add(3D, 16D);
xyseries1.add(4D, 18D);
xyseries1.add(5D, 14D);
xyseries1.add(6D, 14D);
xyseries1.add(7D, 12D);
xyseries1.add(8D, 11.0D);

XYSeries xyseries2 = new XYSeries("Third");
xyseries2.add(1D, 16D);
xyseries2.add(2D, 19D);
xyseries2.add(3D, 14D);
xyseries2.add(4D, 13D);
xyseries2.add(5D, 12D);
xyseries2.add(6D, 13D);
xyseries2.add(7D, 16D);
xyseries2.add(8D, 13D);

XYSeriesCollection xyseriescollection = new XYSeriesCollection(); //再用XYSeriesCollection添加入XYSeries 对象
xyseriescollection.addSeries(xyseries);
xyseriescollection.addSeries(xyseries1);
xyseriescollection.addSeries(xyseries2);
return xyseriescollection;
}


public JFreeChart createJFreeChart(XYDataset dataset) {
// 创建图表
JFreeChart chart = ChartFactory.createXYLineChart("achievements compare",
"achievements compare about Company A and B",
"achievements compare", dataset, PlotOrientation.VERTICAL,
true, true, false);

chart.addSubtitle(new TextTitle("http://www.tisson.cn",new Font("Arial",Font.BOLD,15)));


//设置报表背景
Image image = null;
try {
image = ImageIO.read(new File("D:\\公司/xiangmu/TestImage/test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
chart.setBackgroundImage(image);
//设置背景透明度:0.0--1.0
chart.setBackgroundImageAlpha(0.1f);

//设置非数据域颜色
//chart.setBackgroundPaint(Color.cyan);

//设置边界线条
chart.setBorderPaint(Color.RED);
chart.setBorderVisible(true);

// 数据区设置
XYPlot plot = (XYPlot) chart.getPlot();
// 设置背景为灰色
plot.setBackgroundPaint(Color.lightGray);
// X、Y轴属性设置
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
// 数据轴的数据标签(可以只显示整数标签,需要将AutoTickUnitSelection设false)
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// 是否显示零点
rangeAxis.setAutoRangeIncludesZero(true);


// Y轴名称的倾斜角度
rangeAxis.setLabelAngle(0);
// 设置数据轴的数据范围
rangeAxis.setRange(new Range(start, end));
// 设置刻度波动值,可控制刻度的个数。
int nodenum = (int) ((end - start) / node);
if(nodenum==1){
nodenum = 2;
}
rangeAxis.setTickUnit(new NumberTickUnit(nodenum));
// 数据线设置
// 显示折点数据
/*LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot
.getRenderer();
lineandshaperenderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineandshaperenderer.setBaseItemLabelsVisible(true);*/

//设置纵轴的字体格式
rangeAxis.setTickLabelFont(new Font("Arial",Font.BOLD,15));

//设置坐标轴是否显示箭头
ValueAxis valueaxis = plot.getDomainAxis();
valueaxis.setPositiveArrowVisible(true);

//设置每个曲线的颜色(两种设置方式)
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesPaint(0, Color.cyan);
renderer.setSeriesPaint(1, new Color(0, 100, 255));
renderer.setSeriesPaint(2, Color.green);
return chart;

}


@SuppressWarnings("unchecked")
public static void main(String[] args) {
CopyOfLineChartMyTest_XYData lcm = new CopyOfLineChartMyTest_XYData("Compare A and B");
lcm.pack();
RefineryUtilities.centerFrameOnScreen(lcm);
lcm.setVisible(true);
}

}

你可能感兴趣的:(JFreeChart 折线图中,鼠标移动上去显示坐标信息。)