JFreeChart: how to get coordinates of an XYItemEntity?

556 views Asked by At

I've been stuck with this problem for quite some time. I need coordinates of certain points that belong to XYItemEntity. I'm drawing candlestick charts, and want to know what are the coordinates of open, close, high, low prices of the item. enter image description here

Is there any way to achieve that?

EDIT: The question is similar to this one: https://stackoverflow.com/questions/30801444/jfreecharthow-to-get-coordinates-of-a-chartentity-and-snap-mouse-cursor-to-the but since then I changed the approach. What I want to achieve is the following: I'm drawing a Candelistick chart. As mentioned above I want the user to be able to "snap" mouse to one of the 4 values of the candle: high, low, open, close. In order to implement this feature I figured I would build a collection of Points on ChartPanel that would correspond to those values of all the candles currently being displayed. Then, by adding ChartMouseListener to the ChartPanel, in the chartMouseMoved method I would check if the cursor is in close proximity to any of those points. Here I add MouseChartListener to the ChartPanel:

chartPanel.addChartMouseListener(new MyChartMouseListener());

In this method I want to check if the cursor is near any of the candles:

@Override
    public void chartMouseClicked(ChartMouseEvent chartMouseEvent) {
        int x = chartMouseEvent.getTrigger().getX();
        int y = chartMouseEvent.getTrigger().getY();
        getCandleCloseToCursor(x,y);
    }

How should my getCandleCloseToCursor method look like?

private Point getCandleCloseToCursor(int x, int y) {
        //return point that belongs to the candle near the x,y position;
        //either high, low, close, open position
        return null;
    }

Hopefully it's a little bit more clear now.

1

There are 1 answers

5
trashgod On

A CandlestickRenderer collects ChartEntity information in its implementation of drawItem(), but it lacks the resolution you want. The required geometry is calculated on the fly for each item and never stored explicitly. Absent an entirely new renderer, one approach would be to update an adjacent JTable with relevant data taken from the OHLCDataset. Give your TableModel access to a ChartMouseListener, obtain a reference to the OHLCDataset dataset, and use its accessor methods to fulfill the TableModel contract, as shown here. The exact details depend on your requirements, but you may be able to leverage JTable filtering.