1

I have an XYPlot displaying multiple TimeSeries in a TimeSeriesCollection. I'd like to be able to get the list of TimeSeriesDataItems that correspond to a domain crosshair click.

It looks like I can add a PlotChangeListener to be notified of a change in the crosshair value and I can then call XYPlot.getDomainCrosshairValue(), but I can't see how to then use that crosshair value to get the right time series items

KevinS
  • 7,715
  • 4
  • 38
  • 56

1 Answers1

1

I managed to figure it out eventually. I was on the right track originally; add a PlotChangeListener to be notified of a change and then get the crosshair value. Because my domain axis is a DateAxis the value returned as the crosshair value is actually the millisecond value of the date that the crosshair falls on. From that, I can construct a Day instance that can be used to retrieve the TimeSeriesDataItem from each TimeSeries in my TimeSeriesCollection.

plot.addChangeListener(new PlotChangeListener() {

    public void plotChanged(PlotChangeEvent event) {
        double crosshairXValue = this.plot.getDomainCrosshairValue();

        if (crosshairXValue == 0) {
            return;
        }

        Date date = new Date((long) crosshairXValue);
        Day day = new Day(date);

        for (Iterator<TimeSeries> itr = timeseriesCollection.getSeries().iterator(); itr.hasNext(); ) {

            TimeSeries timeSeries = itr.next();
            TimeSeriesDataItem dataItem = timeSeries.getDataItem(day);

            Number balance = dataItem.getValue();

            System.out.println("Balance for day " + day + " is " + balance);
            //TODO publish balances to listeners

        }
    }

});

Then you also have to add a MouseClickListener to the ChartPanel so that you can delegate clicks to the chart. Otherwise your plot change listener will never be invoked.

chartPanel.addChartMouseListener(new ChartMouseListener() {

        public void chartMouseMoved(ChartMouseEvent event) {
            // Do nothing
        }

        public void chartMouseClicked(ChartMouseEvent event) {
            int x = event.getTrigger().getX();
            int y = event.getTrigger().getY();
            chart.handleClick(x, y, chartPanel.getChartRenderingInfo());
        }

    });
KevinS
  • 7,715
  • 4
  • 38
  • 56