JFreeChart:单击鼠标即可获取数据源值 [英] JFreeChart : obtain data source value on mouse click

查看:430
本文介绍了JFreeChart:单击鼠标即可获取数据源值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示进程内存状态的JFreeChart实例,其初始化如下:

I have a JFreeChart instance that displays process memory status, initialized as follows:

m_data = new TimeSeriesCollection();
TimeSeries vmsize = new TimeSeries("VMSize");
TimeSeries resident = new TimeSeries("Resisdent");
TimeSeries shared = new TimeSeries("Shared memory");
TimeSeries code = new TimeSeries("Code");
TimeSeries data = new TimeSeries("Data");
m_data.addSeries(vmsize);
m_data.addSeries(resident);
m_data.addSeries(shared);
m_data.addSeries(code);
m_data.addSeries(data);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Memory usage", "Time", "Size", m_data, true, true, false);
m_chart = new ChartPanel(chart);

稍后,我将值添加到TimeSeriesCollection中的每个TimeSeries中.我想以某种方式知道-当用户单击图表时-与该列关联的时间是什么时间,或者甚至更好的时间-值的索引是什么.

Later I add values to each TimeSeries in the TimeSeriesCollection. I would like to somehow know - when the user clicks on the Chart - either what time associated with that columm, or even better - what is the index of the value.

我看了看JFreeChart和ChartMouseListener类,但是我不知道该怎么做(而且JFreeChart的文档非常匮乏,我想他们正试图吸引人们购买他们的开发人员指南).

I looked at the JFreeChart and ChartMouseListener classes, but I could not figure out how to do that (also the documentation of JFreeChart is annoyingly scarce, I guess they are trying to get people to buy their developer's guide).

推荐答案

如果在项目上单击死",event.getEntity()函数将返回XYItem,然后从此开始

if you click dead on the item, the event.getEntity() function returns XYItem and then from there onwards

XYItemEntity xyitem=(XYItemEntity) event.getEntity(); // get clicked entity
XYDataset dataset = (XYDataset)xyitem.getDataset(); // get data set
System.out.println(xyitem.getItem()+" item of "+xyitem.getSeriesIndex()+"series");
System.out.println(dataset.getXValue(xyitem.getSeriesIndex(), xyitem.getItem()));
System.out.println(dataset.getYValue(xyitem.getSeriesIndex(), xyitem.getItem()));
Comparable comparable=dataset.getSeriesKey(0);
XYPlot xyplot = (XYPlot) event.getChart().getPlot();
System.out.println(xyplot.getRangeCrosshairValue());

但是,如果您没有单击项目本身,但是将十字准线设置为自动锁定数据,则在这种情况下,十字准线将移动到最近的项目,但是由于未单击该项目,您将无法获得该数据XYItem,因此您不知道序列和项目索引,要解决此问题,请在下面的代码中将此代码放在catch子句中,而上述代码应在try子句中

however incase you do not click on the item itself but your crosshair is set to auto lock on data, in such case the crosshair will move to nearest item but since the item has not been clicked, you will not be able to get the XYItem and hence you cannot know the series and item index, to solve this problem there is this code below, it should be put in the catch clause while the above mentioned code should be in try clause

首先定义一个函数,该函数将在域和范围以及Xydataset处获取十字准线值,此函数将返回一个内部类对象,该对象将项目索引和系列索引进行分组

first define a function which will take crosshair value at domain and range and also Xydataset, this functions returns an inner class object that groups item index and series index

public static SeriesAndItemIndex getItemIndex(double domainVal,double rangeVal,XYDataset xydataset){
Comparable comparable;
int indexOf;
for(int i=0;i<xydataset.getSeriesCount();i++){

comparable =  xydataset.getSeriesKey(i);
     indexOf=xydataset.indexOf(comparable);
for(int j=0 ; j<xydataset.getItemCount(indexOf);j++){

    double x=xydataset.getXValue(indexOf, j);
    double y=xydataset.getYValue(indexOf, j);

    if(x == domainVal && y==rangeVal){
        return  new SeriesAndItemIndex(j,indexOf);//return item index and series index
                }


            }
        }
        return null;
    }

private static class SeriesAndItemIndex{ ///inner CLASS to group series and item clicked index
        public int itemIndex;
        public int seriesIndex;
        public SeriesAndItemIndex(int i,int s){
            itemIndex=i;
            seriesIndex=s;
        }

        @Override
        public String toString(){
            return "itemIndex="+itemIndex+",seriesIndex="+seriesIndex;
        }
    }

如何使用它?

try{......code block from the top

}catch(Exception e){

Object source=event.getSource();
JFreeChart chartpanel=(JFreeChart)source;
XYPlot xyplot = (XYPlot) chartpanel.getPlot();
XYDataset xydataset= xyplot.getDataset();
double d=xyplot.getDomainCrosshairValue(); //get crosshair X value
double r =xyplot.getRangeCrosshairValue(); //get crosshair y value
SeriesAndItemIndex index=getItemIndex(d,r,xydataset);
if(index != null){
    System.out.println(index.toString());
}
}

这篇关于JFreeChart:单击鼠标即可获取数据源值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆