JavaFX区域图表行为已更改

时间:2015-11-13 14:30:14

标签: java javafx javafx-8

我有一个swing应用程序,它的面板包含几个带有自定义样式的JavaFX AreaCharts(使用javafx.embed.swing.JFXPanel)。我们使用了jre 8u20和jre 8u25并且一切正常,现在我必须更新到jre 8u66并且我的图表看起来不同。

该问题与我的情况相反:How to add negative values to JavaFx Area Chart?。我已经使用图表系列根据数据为图表的背景着色(例如,我需要在红色背景中为没有数据的X轴间隔着色)。 JavaFX 8u20区域图表背景颜色为图表线的下限,更新区域图表仅填充背景,忽略轴下方或上方的轴。

现在,JavaFX区域图的工作方式与文档中描述的相同:

The default behavior

如何使用jre 8u66返回javaFX区域图表旧的行为以获得类似的东西:

The image was taken here: https://stackoverflow.com/questions/30185035/how-to-add-negative-values-to-javafx-area-chart

编辑:

所以我找到了修正负值背景填充http://hg.openjdk.java.net/openjfx/8u60/rt/rev/a57b8ba039d0?revcount=480的提交。

方法中只有几行,我的第一个想法是快速修复:在我自己的类中覆盖这一个方法,但是我遇到了问题,JavaFX类不是对这些修改很友好许多必需的字段和方法是私有的或包私有的:(

这是我试图改变AreaChart行为的类:

public class NegativeBGAreaChart<X,Y> extends AreaChart<X, Y> {
    public NegativeBGAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) {
        this(xAxis,yAxis, FXCollections.<Series<X,Y>>observableArrayList());
    }

    public NegativeBGAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X,Y>> data) {
        super(xAxis,yAxis, data);
    }


    @Override 
    protected void layoutPlotChildren() {
        List<LineTo> constructedPath = new ArrayList<>(getDataSize());
        for (int seriesIndex=0; seriesIndex < getDataSize(); seriesIndex++) {
            Series<X, Y> series = getData().get(seriesIndex);
            DoubleProperty seriesYAnimMultiplier = seriesYMultiplierMap.get(series);
            double lastX = 0;
            final ObservableList<Node> children = ((Group) series.getNode()).getChildren();
            ObservableList<PathElement> seriesLine = ((Path) children.get(1)).getElements();
            ObservableList<PathElement> fillPath = ((Path) children.get(0)).getElements();
            seriesLine.clear();
            fillPath.clear();
            constructedPath.clear();
            for (Iterator<Data<X, Y>> it = getDisplayedDataIterator(series); it.hasNext(); ) {
                Data<X, Y> item = it.next();
                double x = getXAxis().getDisplayPosition(item.getCurrentX());
                double y = getYAxis().getDisplayPosition( getYAxis().toRealValue(getYAxis().toNumericValue(item.getCurrentY()) * seriesYAnimMultiplier.getValue()));
                constructedPath.add(new LineTo(x, y));
                if (Double.isNaN(x) || Double.isNaN(y)) {
                    continue;
                }
                lastX = x;
                Node symbol = item.getNode();
                if (symbol != null) {
                    final double w = symbol.prefWidth(-1);
                    final double h = symbol.prefHeight(-1);
                    symbol.resizeRelocate(x-(w/2), y-(h/2),w,h);
                }
            }

            if (!constructedPath.isEmpty()) {
                Collections.sort(constructedPath, (e1, e2) -> Double.compare(e1.getX(), e2.getX()));
                LineTo first = constructedPath.get(0);

                final double displayYPos = first.getY();
                final double numericYPos = getYAxis().toNumericValue(getYAxis().getValueForDisplay(displayYPos));

                // RT-34626: We can't always use getZeroPosition(), as it may be the case
                // that the zero position of the y-axis is not visible on the chart. In these
                // cases, we need to use the height between the point and the y-axis line.
                final double yAxisZeroPos = getYAxis().getZeroPosition();
                final boolean isYAxisZeroPosVisible = !Double.isNaN(yAxisZeroPos);
                final double yAxisHeight = getYAxis().getHeight();
                final double yFillPos = isYAxisZeroPosVisible ? yAxisZeroPos : numericYPos < 0 ? numericYPos - yAxisHeight : yAxisHeight;

                seriesLine.add(new MoveTo(first.getX(), displayYPos));
                fillPath.add(new MoveTo(first.getX(), yFillPos));

                seriesLine.addAll(constructedPath);
                fillPath.addAll(constructedPath);
                fillPath.add(new LineTo(lastX, yFillPos));
                fillPath.add(new ClosePath());
            }
        }
    }
}

问题是:

  1. 原始AreaChart的字段是私有且无法访问的

    私人地图,DoubleProperty&gt; seriesYMultiplierMap = new HashMap&lt;&gt;();

  2. 有些方法是package-private,例如

    javafx.scene.chart.XYChart.Data.getCurrentX()

    javafx.scene.chart.XYChart.Data.getCurrentY()

    javafx.scene.chart.XYChart.getDataSize()

1 个答案:

答案 0 :(得分:1)

尽可能的解决方法我已经做到了:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javafx.beans.NamedArg;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.Axis;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;

/**
 * AreaChart - Plots the area between the line that connects the data points     and
 * the 0 line on the Y axis. This implementation Plots the area between the line
 * that connects the data points and the bottom of the chart area.
 * 
 * @since JavaFX 2.0
 */
public class NegativeBGAreaChart<X, Y> extends AreaChart<X, Y> {
    protected Map<Series<X, Y>, DoubleProperty> shadowSeriesYMultiplierMap = new HashMap<>();

    // -------------- CONSTRUCTORS ----------------------------------------------

    public NegativeBGAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) {
        this(xAxis, yAxis, FXCollections.<Series<X, Y>> observableArrayList());
    }

    public NegativeBGAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X, Y>> data) {
        super(xAxis, yAxis, data);
    }

    // -------------- METHODS ------------------------------------------------------------------------------------------
    @Override
    protected void seriesAdded(Series<X, Y> series, int seriesIndex) {
        DoubleProperty seriesYAnimMultiplier = new SimpleDoubleProperty(this, "seriesYMultiplier");
        shadowSeriesYMultiplierMap.put(series, seriesYAnimMultiplier);
        super.seriesAdded(series, seriesIndex);
    }

    @Override
    protected void seriesRemoved(final Series<X, Y> series) {
        shadowSeriesYMultiplierMap.remove(series);
        super.seriesRemoved(series);
    }

    @Override
    protected void layoutPlotChildren() {
        // super.layoutPlotChildren();
        try {
            List<LineTo> constructedPath = new ArrayList<>(getDataSize());
            for (int seriesIndex = 0; seriesIndex < getDataSize(); seriesIndex++) {
                Series<X, Y> series = getData().get(seriesIndex);
                DoubleProperty seriesYAnimMultiplier = shadowSeriesYMultiplierMap.get(series);
                double lastX = 0;
                final ObservableList<Node> children = ((Group) series.getNode()).getChildren();
                ObservableList<PathElement> seriesLine = ((Path) children.get(1)).getElements();
                ObservableList<PathElement> fillPath = ((Path) children.get(0)).getElements();
                seriesLine.clear();
                fillPath.clear();
                constructedPath.clear();
                for (Iterator<Data<X, Y>> it = getDisplayedDataIterator(series); it.hasNext();) {
                    Data<X, Y> item = it.next();
                    double x = getXAxis().getDisplayPosition(item.getXValue());// FIXME: here should be used item.getCurrentX()
                    double y = getYAxis().getDisplayPosition(
                        getYAxis().toRealValue(
                                getYAxis().toNumericValue(item.getYValue()) * seriesYAnimMultiplier.getValue()));// FIXME: here should be used item.getCurrentY()
                    constructedPath.add(new LineTo(x, y));
                    if (Double.isNaN(x) || Double.isNaN(y)) {
                        continue;
                    }
                    lastX = x;
                    Node symbol = item.getNode();
                    if (symbol != null) {
                        final double w = symbol.prefWidth(-1);
                        final double h = symbol.prefHeight(-1);
                        symbol.resizeRelocate(x - (w / 2), y - (h / 2), w, h);
                    }
                }

                if (!constructedPath.isEmpty()) {
                    Collections.sort(constructedPath, (e1, e2) -> Double.compare(e1.getX(), e2.getX()));
                    LineTo first = constructedPath.get(0);

                    seriesLine.add(new MoveTo(first.getX(), first.getY()));
                    fillPath.add(new MoveTo(first.getX(), getYAxis().getHeight()));

                    seriesLine.addAll(constructedPath);
                    fillPath.addAll(constructedPath);
                    fillPath.add(new LineTo(lastX, getYAxis().getHeight()));
                    fillPath.add(new ClosePath());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the size of the data returning 0 if the data is null
     *
     * @return The number of items in data, or null if data is null
     */
    public int getDataSize() {
        final ObservableList<Series<X, Y>> data = getData();
        return (data != null) ? data.size() : 0;
    }
}

我知道它有问题,但我现在别无选择,我希望有一天JavaFX会被改变为对外部变化更友好。

编辑:

这个解决方法是解决所描述的问题,但似乎由于标记为FIXME的代码,系列点出现在错误的坐标上。在下面的屏幕截图中,系列的点有Y坐标3或-3,但它们都放在坐标为0的轴上。

enter image description here

EDIT2:

所以我设法修复了它,动画出了问题,所以我禁用了动画图表:

chart.setAnimated(false);

并修复了方法(删除了第二条FIXME行中的动画乘数),最后我得到了这个:

public class NegativeBGAreaChart<X, Y> extends AreaChart<X, Y> {
    protected Map<Series<X, Y>, DoubleProperty> shadowSeriesYMultiplierMap = new HashMap<>();

    // -------------- CONSTRUCTORS ----------------------------------------------

    public NegativeBGAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) {
        this(xAxis, yAxis, FXCollections.<Series<X, Y>> observableArrayList());
    }

    public NegativeBGAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X, Y>> data) {
        super(xAxis, yAxis, data);
    }

    // -------------- METHODS ------------------------------------------------------------------------------------------
    @Override
    protected void seriesAdded(Series<X, Y> series, int seriesIndex) {
        DoubleProperty seriesYAnimMultiplier = new SimpleDoubleProperty(this, "seriesYMultiplier");
        shadowSeriesYMultiplierMap.put(series, seriesYAnimMultiplier);
        super.seriesAdded(series, seriesIndex);
    }

    @Override
    protected void seriesRemoved(final Series<X, Y> series) {
        shadowSeriesYMultiplierMap.remove(series);
        super.seriesRemoved(series);
    }

    @Override
    protected void layoutPlotChildren() {
//          super.layoutPlotChildren();
        try {
            List<LineTo> constructedPath = new ArrayList<>(getDataSize());
            for (int seriesIndex = 0; seriesIndex < getDataSize(); seriesIndex++) {
                Series<X, Y> series = getData().get(seriesIndex);
                DoubleProperty seriesYAnimMultiplier = shadowSeriesYMultiplierMap.get(series);
                double lastX = 0;
                final ObservableList<Node> children = ((Group) series.getNode()).getChildren();
                ObservableList<PathElement> seriesLine = ((Path) children.get(1)).getElements();
                ObservableList<PathElement> fillPath = ((Path) children.get(0)).getElements();
                seriesLine.clear();
                fillPath.clear();
                constructedPath.clear();
                for (Iterator<Data<X, Y>> it = getDisplayedDataIterator(series); it.hasNext();) {
                    Data<X, Y> item = it.next();
                    double x = getXAxis().getDisplayPosition(item.getXValue());// FIXME: here should be used item.getCurrentX()
                    double y = getYAxis().getDisplayPosition(getYAxis().toRealValue(getYAxis().toNumericValue(item.getYValue())));// FIXME: here should be used item.getCurrentY()
                    constructedPath.add(new LineTo(x, y));
                    if (Double.isNaN(x) || Double.isNaN(y)) {
                        continue;
                    }
                    lastX = x;
                    Node symbol = item.getNode();
                    if (symbol != null) {
                        final double w = symbol.prefWidth(-1);
                        final double h = symbol.prefHeight(-1);
                        symbol.resizeRelocate(x - (w / 2), y - (h / 2), w, h);
                    }
                }

                if (!constructedPath.isEmpty()) {
                    Collections.sort(constructedPath, (e1, e2) -> Double.compare(e1.getX(), e2.getX()));
                    LineTo first = constructedPath.get(0);

                    seriesLine.add(new MoveTo(first.getX(), first.getY()));
                    fillPath.add(new MoveTo(first.getX(), getYAxis().getHeight()));

                    seriesLine.addAll(constructedPath);
                    fillPath.addAll(constructedPath);
                    fillPath.add(new LineTo(lastX, getYAxis().getHeight()));
                    fillPath.add(new ClosePath());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the size of the data returning 0 if the data is null
     *
     * @return The number of items in data, or null if data is null
     */
    public int getDataSize() {
        final ObservableList<Series<X, Y>> data = getData();
        return (data != null) ? data.size() : 0;
    }
}

修复后所有内容都应该看起来(与上一张图片相同的数据集):

enter image description here