系列消失到窗口大小调整(JavaFX)

时间:2017-02-06 10:40:38

标签: javafx charts window

我遇到了JavaFX的问题;我解释一下:我有一个有5个系列的类可以绘制,但是4个是散点图(点)和线图1(实线)。折线图在X和Y轴上的参数继续低于其他四个系列,因此我需要做更多的计算。我插入绘制图形的控制器类的代码:

    public class ControllerFormantsSmooth {

        public static void plotFormantSmooth(String path, Stage stage, String name) {
            final NumberAxis xAxis = new NumberAxis(); //asseX
            final NumberAxis yAxis = new NumberAxis(); //asseY
            //POINT GRAPH
            final ScatterChart<Number, Number> scatterFormant = new ScatterChart<Number, Number>(xAxis, yAxis);

            //SOLID LINE GRAPH
            final LineChart<Number, Number> lineChartFull = new LineChart<Number, Number>(xAxis, yAxis);

            ArrayList<Double> extractedF1;
            ArrayList<Double> extractedF2;
            ArrayList<Double> extractedF3;
            ArrayList<Double> extractedF4;

            ArrayList<Double> extractedTimeF1;

            ArrayList<Double> extractedData; 
            ArrayList<Double> extractedTime;

            //Gestione e settaggio del grafico
            scatterFormant.getData().clear();
            scatterFormant.setOpacity(0.8);
            scatterFormant.getStyleClass().add("CSSformant");
            scatterFormant.setAnimated(true);
            scatterFormant.autosize();
            xAxis.setLabel("Time(ms)");
            yAxis.setLabel("Formant(Hz)");

            //Gestione e settaggio del grafico
                    lineChartFull.getData().clear();
                    lineChartFull.setOpacity(0.8);
                    lineChartFull.setCreateSymbols(false);


            try {

                //POPULATE LIST
                extractedF1 = FormantExtractor.pointF1();
                extractedF2 = FormantExtractor.pointF2();
                extractedF3 = FormantExtractor.pointF3();
                extractedF4 = FormantExtractor.pointF4();

                extractedTimeF1 = FormantExtractor.timeF1();

                AudioWaveformCreator.waveForm(path);
                extractedData = AudioWaveformCreator.audioPoints();
                extractedTime = AudioWaveformCreator.pointTimeOscillo();

//POINT SERIES
                XYChart.Series<Number, Number> seriesF1 = new XYChart.Series<Number, Number>();
                XYChart.Series<Number, Number> seriesF2 = new XYChart.Series<Number, Number>();
                XYChart.Series<Number, Number> seriesF3 = new XYChart.Series<Number, Number>();
                XYChart.Series<Number, Number> seriesF4 = new XYChart.Series<Number, Number>();

//LINE SERIE
                XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
                /*
                 * A ogni serie viene aggiunta una formante
                 */
                for (int i = 0; i < extractedF1.size()-1; i++) {
                    if(extractedF1.get(i)+1>extractedF1.get(i+1) && extractedF1.get(i+1)+1>extractedF1.get(i) &&
                            extractedF1.get(i)<FormantExtractor.getF1_avg()+50 && extractedF1.get(i)>FormantExtractor.getF1_avg()-50){
                        seriesF1.getData().add(new XYChart.Data<Number, Number>(extractedTimeF1.get(i), extractedF1.get(i)));           
                    }
                }
                for (int i = 0; i < extractedF2.size()-1; i++) {
                    if(extractedF2.get(i)+10>extractedF2.get(i+1) && extractedF2.get(i+1)+10>extractedF2.get(i) && 
                            extractedF2.get(i)<FormantExtractor.getF2_avg()+50 && extractedF2.get(i)>FormantExtractor.getF2_avg()-50){
                        seriesF2.getData().add(new XYChart.Data<Number, Number>(extractedTimeF2.get(i), extractedF2.get(i)));
                    }
                }
                for (int i = 0; i < extractedF3.size()-1; i++) {
                    if(extractedF3.get(i)+10>extractedF3.get(i+1) && extractedF3.get(i+1)+10>extractedF3.get(i) 
                            && extractedF3.get(i)<FormantExtractor.getF3_avg()+50 && extractedF3.get(i)>FormantExtractor.getF3_avg()-50){
                        seriesF3.getData().add(new XYChart.Data<Number, Number>(extractedTimeF3.get(i), extractedF3.get(i)));
                    }
                }
                for (int i = 0; i < extractedF4.size()-1; i++) {
                    if(extractedF4.get(i)+10>extractedF4.get(i+1) && extractedF4.get(i+1)+10>extractedF4.get(i) 
                            && extractedF4.get(i)<FormantExtractor.getF4_avg()+50 && extractedF4.get(i)>FormantExtractor.getF4_avg()-50){
                        seriesF4.getData().add(new XYChart.Data<Number, Number>(extractedTimeF4.get(i), extractedF4.get(i)));
                    }
                }



                for (int i = 0; i < extractedData.size(); i= i+100) {
                    series.getData().add(new XYChart.Data<Number, Number>(extractedTime.get(i)*2550, (extractedData.get(i)/10)));
                }


                scatterFormant.getData().addAll(seriesF1,seriesF2,seriesF3,seriesF4);
                lineChartFull.getData().add(series);

                StackPane stackpane = new StackPane();
                stackpane.getChildren().addAll(lineChartFull,scatterFormant);

                //CREATE WINDOW
                Scene scene = new Scene(stackpane, 1000, 600);

                scatterFormant.getStylesheets().add("application/application.css");
                stage.setScene(scene);
                stage.show();
                scatterFormant.setLegendVisible(false);
                //lineChartFull.setLegendVisible(false);;
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }

然后这里的窗口是这样的: Correct 这是正确的(正是我想要的!) 但是如果我调整窗口大小(放大),结果就是这个(图形用实线消失):

Incorrect 我只是无法理解为什么......你有想法?提前感谢那些想要帮助我的人。

1 个答案:

答案 0 :(得分:1)

图表的布局似乎有问题。 您可以将容器StackPane替换为这样的自定义类:

import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.StackPane;

/**
 * Container calss for two overlayed charts
 */
public class StackedCharts extends StackPane {

    private Parent _bottomNode = null;
    private Parent _topNode = null;

    /**
     * adds node as first child (i.e. bottom)
     *
     * @param pNode
     */
    void addBottom(Parent pNode) {
        _bottomNode = pNode;
        getChildren().add(0, pNode);
    }

    /**
     * adds node as second child (i.e. top)
     *
     * @param pNode
     */
    void addTop(Parent pNode) {
        _topNode = pNode;
        getChildren().add(pNode);
    }

    public Node getBottomNode() {
        return _bottomNode;
    }

    public Node getTopNode() {
        return _topNode;
    }

    /**
     * Corrects the problem with overlayed charts
     */
    @Override
    protected void layoutChildren() {

        _topNode.layout();
        _bottomNode.layout();

        super.layoutChildren();

    }

}

然后代替

StackPane stackpane = new StackPane();
stackpane.getChildren().addAll(lineChartFull,scatterFormant);

使用

StackedCharts stackpane = new StackedCharts();
stackpane.addBottom(lineChartFull);
stackpane.addTop(scatterFormant);