Line Annotation does not draw

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
blahfunk
Posts: 8
Joined: Wed Jan 17, 2018 5:52 pm
antibot: No, of course not.

Line Annotation does not draw

Post by blahfunk » Wed Feb 21, 2018 5:51 pm

I have created an XYLineAnnotation object to draw a line into my graph, but it doesn't draw? I am using a ChartMouseListener so that it will follow the mouse pointer like so:

Code: Select all

someChartPanel.addChartMouseListener(new ChartMouseListener() {
    private int lineLoc = 0;
    private XYLineAnnotation line = null;
    
    @Override
    public void chartMouseClicked(ChartMouseEvent e) {}
    
    @Override
    public void chartMouseMoved(ChartMouseEvent e) {
        lineLoc = e.getTrigger().getX();
        if (line != null) someChartPanel.getChart().getXYPlot().removeAnnotation(line);
        line = new XYLineAnnotation(lineLoc, 0, lineLoc, 1000, new BasicStroke(1.5f), Color.RED);
        someChartPanel.getChart().getXYPlot().addAnnotation(line);
        System.out.println(lineLoc); // This works!!!
    }
});
.. but it never draws. Notice in my code that I put a println just to see if the code is even being executed and the println does execute. What is happening and how do I get the line to draw over my chart?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Line Annotation does not draw

Post by david.gilbert » Thu Feb 22, 2018 6:24 am

The coordinates that you pass to the XYLineAnnotation constructor need to be specified in data space (that is, values relative to the chart's axes) not in screen space.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked