I have 4 series plotted on XYPlot
. I want to obtain series ID when I click on XYPlot
(see //int seriesId = ???;
). Is it possible?
_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);
}
}
As shown here, you can retrieve the ChartEntity
from the ChartMouseEvent
. For a ChartEntity
of type XYItemEntity
, you can get the dataset via getDataset()
and the series index via 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());
}