How to get x,y for XYPointerAnnotation in simple line chart?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
erikvanoosten
Posts: 8
Joined: Tue Aug 30, 2005 10:42 am

How to get x,y for XYPointerAnnotation in simple line chart?

Post by erikvanoosten » Mon Sep 12, 2005 11:01 am

I am trying to add an XYPointerAnnotation to a combined line chart. The XYPointerAnnotation constructor requires an X and Y value which I think are absolute to the complete chart area.

How can I calculate the X and Y value from a given domain and range value?

Regards,
Erik.

erikvanoosten
Posts: 8
Joined: Tue Aug 30, 2005 10:42 am

How to get x,y for XYPointerAnnotation in simple line chart?

Post by erikvanoosten » Mon Sep 12, 2005 11:35 am

Of course, x and y are not absolute, but very much exactly the domain and range values. Problem solved!

Guest

How to get x,y for XYPointerAnnotation in simple line chart?

Post by Guest » Mon Sep 12, 2005 1:37 pm

Next problem: you can not specify to which vertical axis the annotation should be resolved. I have several but the range value (the Y) is always resolved against the first.

The following method may be a temporal solution in solving this:

Code: Select all

    XYPlot chartPlot = ...;
    ValueAxis defaultRangeAxis = chartPlot.getRangeAxis();
    ValueAxis usedRangeAxis = chartPlot.getRangeAxis(........);

    String msg = ...;
    double domainValue = ...;                
    double rangeValue = ...;                
    double annotationValue = convertToAxis(rangeValue, usedRangeAxis, defaultRangeAxis);
                    
    XYPointerAnnotation pointerAnnotation = new XYPointerAnnotation(
             msg, domainValue, annotationValue, angle);
    chartPlot.addAnnotation(pointerAnnotation );

Code: Select all

    /**
     * Converts a number so that it can be used on another axis.
     * @param source a number against source axis
     * @param srcAxis the source axis
     * @param dstAxis the destination axis
     * @return the number on the same number against the destination axis
     */
    private static double convertToAxis(double source, ValueAxis srcAxis, ValueAxis dstAxis) {
        double result;
        if (srcAxis == dstAxis) {
            result = source;
        } else {
            Range srcRange = srcAxis.getRange();
            Range dstRange = dstAxis.getRange();
            result = dstRange.getLowerBound() + ((source - srcRange.getLowerBound()) / srcRange.getLength()) * dstRange.getLength();
        }
        return result;
    }
A final solution should be additional methods on XYPlot.

Regards,
Erik.

Locked