JavaFX 2.x: логарифмическая шкала по оси Y

Как из этого очень хорошего поста здесь

Логарифмическая шкала в Java FX 2

Я изменил этот класс, чтобы получить логарифмическую шкалу по оси Y, и он отлично работает. Единственная проблема, с которой я сталкиваюсь, заключается в том, что горизонтальных линий сетки очень мало, а масштаб всегда начинается от 0 или около нуля.

Вот что я получаю

введите здесь описание изображения

Я хотел бы иметь сетку значений тиков также в минимальном и максимальном диапазоне моей серии данных, в этом случае min = 19,35 max = 20,35; на данный момент все 10 горизонтальных линий сетки нанесены за пределами этого диапазона.

Как это сделать?

Спасибо всем, вот мой код журнала для оси Y

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.chart.ValueAxis;

//http://blog.dooapp.com/logarithmic-scale-strikes-back-in-javafx-20        
public class LogarithmicAxis extends ValueAxis<Number> {

//Create our LogarithmicAxis class that extends ValueAxis<Number> and define two properties that will represent the log lower and upper bounds of our axis.     
private final DoubleProperty logUpperBound = new SimpleDoubleProperty();
private final DoubleProperty logLowerBound = new SimpleDoubleProperty();
//

//we bind our properties with the default bounds of the value axis. But before, we should verify the given range according to the mathematic logarithmic interval definition.
public LogarithmicAxis() {
    super(1, 100);
    bindLogBoundsToDefaultBounds();
}

public LogarithmicAxis(double lowerBound, double upperBound) {
    super(lowerBound, upperBound);
try {
    validateBounds(lowerBound, upperBound);
    bindLogBoundsToDefaultBounds();
} catch (IllegalLogarithmicRangeException e) {
    }
}

/**
 * Bind our logarithmic bounds with the super class bounds, consider the base 10 logarithmic scale.
 */
private void bindLogBoundsToDefaultBounds() {
    logLowerBound.bind(new DoubleBinding() {
        {
            super.bind(lowerBoundProperty());
        }

        @Override
        protected double computeValue() {
            return Math.log10(lowerBoundProperty().get());
        }
    });
    logUpperBound.bind(new DoubleBinding() {
        {
            super.bind(upperBoundProperty());
        }

        @Override
        protected double computeValue() {
            return Math.log10(upperBoundProperty().get());
        }
    });
}

/**
 * Validate the bounds by throwing an exception if the values are not conform to the mathematics log interval:
 * ]0,Double.MAX_VALUE]
 *
 * @param lowerBound
 * @param upperBound
 * @throws IllegalLogarithmicRangeException
 */
private void validateBounds(double lowerBound, double upperBound) throws IllegalLogarithmicRangeException {
    if (lowerBound < 0 || upperBound < 0 || lowerBound > upperBound) {
        throw new IllegalLogarithmicRangeException(
                "The logarithmic range should be include to ]0,Double.MAX_VALUE] and the lowerBound should be less than the upperBound");
    }
}

//Now we have to implement all abstract methods of the ValueAxis class.
//The first one, calculateMinorTickMarks is used to get the list of minor tick marks position that you want to display on the axis. You could find my definition below. It's based on the number of minor tick and the logarithmic formula.
@Override
protected List<Number> calculateMinorTickMarks() {
    Number[] range = getRange();
    List<Number> minorTickMarksPositions = new ArrayList<>();
    if (range != null) {

        Number lowerBound = range[0];
        Number upperBound = range[1];
        double logUpperBound = Math.log10(upperBound.doubleValue());
        double logLowerBound = Math.log10(lowerBound.doubleValue());

        int minorTickMarkCount = getMinorTickCount();

        for (double i = logLowerBound; i <= logUpperBound; i += 1) {
            for (double j = 0; j <= 10; j += (1. / minorTickMarkCount)) {
                double value = j * Math.pow(10, i);
                minorTickMarksPositions.add(value);
            }
        }
    }
    return minorTickMarksPositions;
}

//Then, the calculateTickValues method is used to calculate a list of all the data values for each tick mark in range, represented by the second parameter. The formula is the same than previously but here we want to display one tick each power of 10.
@Override
protected List<Number> calculateTickValues(double length, Object range) {
    List<Number> tickPositions = new ArrayList<Number>();
    if (range != null) {
        Number lowerBound = ((Number[]) range)[0];
        Number upperBound = ((Number[]) range)[1];
        double logLowerBound = Math.log10(lowerBound.doubleValue());
        double logUpperBound = Math.log10(upperBound.doubleValue());
        System.out.println("lower bound is: " + lowerBound.doubleValue());

        for (double i = logLowerBound; i <= logUpperBound; i += 1) {
            for (double j = 1; j <= 10; j++) {
                double value = (j * Math.pow(10, i));
                tickPositions.add(value);
            }
        }
    }
    return tickPositions;
}

//The getRange provides the current range of the axis. A basic implementation is to return an array of the lowerBound and upperBound properties defined into the ValueAxis class.
@Override
protected Number[] getRange() {
    return new Number[] { lowerBoundProperty().get(), upperBoundProperty().get() };
}

//The getTickMarkLabel is only used to convert the number value to a string that will be displayed under the tickMark. Here I choose to use a number formatter.
@Override
protected String getTickMarkLabel(Number value) {
    NumberFormat formatter = NumberFormat.getInstance();
    formatter.setMaximumIntegerDigits(6);
    formatter.setMinimumIntegerDigits(1);
    return formatter.format(value);
}

//The method setRange is used to update the range when data are added into the chart. There is two possibilities, the axis is animated or not. The simplest case is to set the lower and upper bound properties directly with the new values.
@Override    
protected void setRange(Object range, boolean animate) {
    if (range != null) {
        Number lowerBound = ((Number[]) range)[0];
        Number upperBound = ((Number[]) range)[1];
        try {
            validateBounds(lowerBound.doubleValue(), upperBound.doubleValue());
        } catch (IllegalLogarithmicRangeException e) {
        }

        lowerBoundProperty().set(lowerBound.doubleValue());
        upperBoundProperty().set(upperBound.doubleValue());
    }
}

//We are almost done but we forgot to override 2 important methods that are used to perform the matching between data and the axis (and the reverse).
@Override
public Number getValueForDisplay(double displayPosition) {
    double delta = logUpperBound.get() - logLowerBound.get();
    if (getSide().isVertical()) {
        return Math.pow(10, (((displayPosition - getHeight()) / -getHeight()) * delta) + logLowerBound.get());
    } else {
        return Math.pow(10, (((displayPosition / getWidth()) * delta) + logLowerBound.get()));
    }
}

@Override
public double getDisplayPosition(Number value) {
    double delta = logUpperBound.get() - logLowerBound.get();
    double deltaV = Math.log10(value.doubleValue()) - logLowerBound.get();
    if (getSide().isVertical()) {
        return (1. - ((deltaV) / delta)) * getHeight();
    } else {
        return ((deltaV) / delta) * getWidth();
    }
}

/**
 * Exception to be thrown when a bound value isn't supported by the logarithmic axis<br>
 *
 *
 * @author Kevin Senechal mailto: [email protected]
 *
 */
public class IllegalLogarithmicRangeException extends Exception {
/**
 * @param string
 */
    public IllegalLogarithmicRangeException(String message) {
        super(message);
    }
}
}

person Alberto acepsut    schedule 22.01.2013    source источник


Ответы (2)


Я думаю, что ваша проблема заключается в следующем:

    super(1, 100);

Из документации :

Create a non-auto-ranging ValueAxis with the given upper & lower bound

Попробуйте использовать конструктор без параметров, что сделает границы автоматическими.

В итоге у вас должен получиться такой конструктор:

public LogarithmicAxis() {
    // was: super(1, 100);
    super();
    bindLogBoundsToDefaultBounds();
}
person drzymala    schedule 06.02.2013
comment
Спасибо за ваш ответ, у меня до сих пор нет сетки горизонтальных линий, где мне нужно изменить код? - person Alberto acepsut; 07.02.2013

У нас тоже были эти проблемы с предложенной реализацией логарифмической оси, вот полный код с исправлениями, которые нам помогли.

import com.sun.javafx.charts.ChartLayoutAnimator;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.chart.ValueAxis;
import javafx.util.Duration;

//http://blog.dooapp.com/logarithmic-scale-strikes-back-in-javafx-20
//Edited by Vadim Levit & Benny Lutati for usage in AgentZero ( https://code.google.com/p/azapi-test/ )
public class LogarithmicNumberAxis extends ValueAxis<Number> {

    private Object currentAnimationID;
    private final ChartLayoutAnimator animator = new ChartLayoutAnimator(this);

//Create our LogarithmicAxis class that extends ValueAxis<Number> and define two properties that will represent the log lower and upper bounds of our axis.     
    private final DoubleProperty logUpperBound = new SimpleDoubleProperty();
    private final DoubleProperty logLowerBound = new SimpleDoubleProperty();
//

//we bind our properties with the default bounds of the value axis. But before, we should verify the given range according to the mathematic logarithmic interval definition.
    public LogarithmicNumberAxis() {
        super(1, 10000000);
        bindLogBoundsToDefaultBounds();
    }

    public LogarithmicNumberAxis(double lowerBound, double upperBound) {
        super(lowerBound, upperBound);
        validateBounds(lowerBound, upperBound);
        bindLogBoundsToDefaultBounds();
    }

    public void setLogarithmizedUpperBound(double d) {
        double nd = Math.pow(10, Math.ceil(Math.log10(d)));
        setUpperBound(nd == d ? nd * 10 : nd);
    }

    /**
     * Bind our logarithmic bounds with the super class bounds, consider the
     * base 10 logarithmic scale.
     */
    private void bindLogBoundsToDefaultBounds() {
        logLowerBound.bind(new DoubleBinding() {
            {
                super.bind(lowerBoundProperty());
            }

            @Override
            protected double computeValue() {
                return Math.log10(lowerBoundProperty().get());
            }
        });
        logUpperBound.bind(new DoubleBinding() {
            {
                super.bind(upperBoundProperty());
            }

            @Override
            protected double computeValue() {
                return Math.log10(upperBoundProperty().get());
            }
        });
    }

    /**
     * Validate the bounds by throwing an exception if the values are not
     * conform to the mathematics log interval: ]0,Double.MAX_VALUE]
     *
     * @param lowerBound
     * @param upperBound
     * @throws IllegalLogarithmicRangeException
     */
    private void validateBounds(double lowerBound, double upperBound) throws IllegalLogarithmicRangeException {
        if (lowerBound < 0 || upperBound < 0 || lowerBound > upperBound) {
            throw new IllegalLogarithmicRangeException(
                    "The logarithmic range should be in [0,Double.MAX_VALUE] and the lowerBound should be less than the upperBound");
        }
    }

//Now we have to implement all abstract methods of the ValueAxis class.
//The first one, calculateMinorTickMarks is used to get the list of minor tick marks position that you want to display on the axis. You could find my definition below. It's based on the number of minor tick and the logarithmic formula.
    @Override
    protected List<Number> calculateMinorTickMarks() {
        List<Number> minorTickMarksPositions = new ArrayList<>();
        return minorTickMarksPositions;
    }

//Then, the calculateTickValues method is used to calculate a list of all the data values for each tick mark in range, represented by the second parameter. The formula is the same than previously but here we want to display one tick each power of 10.
    @Override
    protected List<Number> calculateTickValues(double length, Object range) {
        LinkedList<Number> tickPositions = new LinkedList<>();
        if (range != null) {
            double lowerBound = ((double[]) range)[0];
            double upperBound = ((double[]) range)[1];

            for (double i = Math.log10(lowerBound); i <= Math.log10(upperBound); i++) {
                tickPositions.add(Math.pow(10, i));
            }

            if (!tickPositions.isEmpty()) {
                if (tickPositions.getLast().doubleValue() != upperBound) {
                    tickPositions.add(upperBound);
                }
            }
        }

        return tickPositions;
    }

    /**
     * The getRange provides the current range of the axis. A basic
     * implementation is to return an array of the lowerBound and upperBound
     * properties defined into the ValueAxis class.
     *
     * @return
     */
    @Override
    protected double[] getRange() {
        return new double[]{
            getLowerBound(),
            getUpperBound()
        };
    }

    /**
     * The getTickMarkLabel is only used to convert the number value to a string
     * that will be displayed under the tickMark. Here I choose to use a number
     * formatter.
     *
     * @param value
     * @return
     */
    @Override
    protected String getTickMarkLabel(Number value) {
        NumberFormat formatter = NumberFormat.getInstance();
        formatter.setMaximumIntegerDigits(10);
        formatter.setMinimumIntegerDigits(1);
        return formatter.format(value);
    }

    /**
     * The method setRange is used to update the range when data are added into
     * the chart. There is two possibilities, the axis is animated or not. The
     * simplest case is to set the lower and upper bound properties directly
     * with the new values.
     *
     * @param range
     * @param animate
     */
    @Override
    protected void setRange(Object range, boolean animate) {
        if (range != null) {
            final double[] rangeProps = (double[]) range;
            final double lowerBound = rangeProps[0];
            final double upperBound = rangeProps[1];

            final double oldLowerBound = getLowerBound();
            setLowerBound(lowerBound);
            setUpperBound(upperBound);
            if (animate) {
                animator.stop(currentAnimationID);
                currentAnimationID = animator.animate(
                        new KeyFrame(Duration.ZERO,
                                new KeyValue(currentLowerBound, oldLowerBound)
                        ),
                        new KeyFrame(Duration.millis(700),
                                new KeyValue(currentLowerBound, lowerBound)
                        )
                );
            } else {
                currentLowerBound.set(lowerBound);
            }
        }
    }

    /**
     * We are almost done but we forgot to override 2 important methods that are
     * used to perform the matching between data and the axis (and the reverse).
     *
     * @param displayPosition
     * @return
     */
    @Override
    public Number getValueForDisplay(double displayPosition) {
        double delta = logUpperBound.get() - logLowerBound.get();
        if (getSide().isVertical()) {
            return Math.pow(10, (((displayPosition - getHeight()) / -getHeight()) * delta) + logLowerBound.get());
        } else {
            return Math.pow(10, (((displayPosition / getWidth()) * delta) + logLowerBound.get()));
        }
    }

    @Override
    public double getDisplayPosition(Number value) {
        double delta = logUpperBound.get() - logLowerBound.get();
        double deltaV = Math.log10(value.doubleValue()) - logLowerBound.get();
        if (getSide().isVertical()) {
            return (1. - ((deltaV) / delta)) * getHeight();
        } else {
            return ((deltaV) / delta) * getWidth();
        }
    }

    /**
     * Exception to be thrown when a bound value isn't supported by the
     * logarithmic axis<br>
     *
     *
     * @author Kevin Senechal mailto: [email protected]
     *
     */
    public class IllegalLogarithmicRangeException extends RuntimeException {

        /**
         * @param string
         */
        public IllegalLogarithmicRangeException(String message) {
            super(message);
        }
    }
}
person bennyl    schedule 15.03.2014
comment
Я добавил этот класс, но его использование приводит к отображению приложения. - person WestCoastProjects; 07.08.2015