单击XYPlot系列时获取系列标识符

时间:2016-02-08 10:04:52

标签: java jfreechart

我在XYPlot上绘制了4个系列。我想在点击XYPlot时获取系列ID(请参阅//int seriesId = ???;)。有可能吗?

   _chartPanel.addChartMouseListener(new ChartMouseListener() {
        @Override
        public void chartMouseClicked(ChartMouseEvent cme) 
        {
            MouseEvent me = cme.getTrigger();
            XYPlot plot = (XYPlot) cme.getChart().getPlot();
            if (me.getClickCount() == 2)
            {
                plot.clearAnnotations();
            }
            else
            {
                Rectangle2D dataArea = _chartPanel.getScreenDataArea();
                plot.clearAnnotations();
                ValueAxis xAxis = plot.getDomainAxis();
                ValueAxis yAxis = plot.getRangeAxis();
                double x = xAxis.java2DToValue(cme.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
                double y = yAxis.java2DToValue(cme.getTrigger().getY(), dataArea, RectangleEdge.LEFT);
                if (!xAxis.getRange().contains(x)) { 
                    x = Double.NaN;                  

                //int seriesId = ???;

                DecimalFormat df = new DecimalFormat("#.##");
                df.setRoundingMode(RoundingMode.CEILING);
                XYPointerAnnotation pt = new XYPointerAnnotation("Lat: " + df.format(y) + "\n Lon: " + df.format(x), x, y, 0.2);
                pt.setBackgroundPaint(new Color(103,154,236));
                // pt.setArrowPaintnew Color(103,154,236)d);
                pt.setFont(_font);
                pt.setPaint(Color.LIGHT_GRAY);
                plot.addAnnotation(pt);
            }
        }

1 个答案:

答案 0 :(得分:2)

如图here所示,您可以从ChartMouseEvent检索ChartEntity。对于ChartEntity类型getDataset(),您可以通过getSeriesIndex()获取数据集,通过ChartEntity ce = cme.getEntity(); if (ce instanceof XYItemEntity) { XYItemEntity e = (XYItemEntity) ce; System.out.println("Dataset: " + e.getDataset()); System.out.println("Series index: " + e.getSeriesIndex()); } 获取系列索引。

highchart