1

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.

Community
  • 1
  • 1
Endrew
  • 157
  • 1
  • 2
  • 12

1 Answers1

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I'm trying to understand your solution, but I'm failing. In order to obtain a reference to the OHLCDataset I would first need to hoover the mouse over the candle, wouldn't I? Then, the OHLCDataset accessor methods give me range and domain values, not the x, y coordinates which I need. – Endrew Jun 17 '15 at 09:11
  • Correct; at a minimum, you'd need to rewrite [`drawItem()`](http://www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/chart/renderer/xy/CandlestickRenderer.html#line.627) to collect additional `ChartEntity` objects. My suggestion is to work with the existing implementation and construct a table with a four rows (OHLC) and a column for each item of interest. – trashgod Jun 17 '15 at 10:22
  • I guess you mean something like this? http://i.imgur.com/MzX5O7g.png But how would I then move the mouse cursor to the point on the chart having only domain and range values? – Endrew Jun 17 '15 at 11:46
  • The `XYItemEntity` can tell you its coordinates in the dataset. – trashgod Jun 18 '15 at 17:32
  • thx, I'll try to implement this. – Endrew Jun 18 '15 at 17:54