How to combine scatter chart with line chart to show line of regression? JavaFX

后端 未结 4 740
小蘑菇
小蘑菇 2020-12-06 01:44

I\'ve created a scatter chart with two sets of data; the first set is the actual data (x = year and y = pence) and the second set produces the same points but for the line o

4条回答
  •  天涯浪人
    2020-12-06 02:18

    I do not have enough "reputation" to add a comment below Mailkov's answer, but I think that there is an issue with the proposed answer.

    1) As a matter of fact, the full orange dots of the scattered chart are not where they are supposed to be (x = 2.8 and x = 4.2).

    import javafx.scene.layout.Pane;
    import javafx.stage.Stage;
    
    public class Exemple158bis_JavaFX_Overlaid_Stacked_Charts extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
    
        NumberAxis xAxis = new NumberAxis();
        NumberAxis yAxis = new NumberAxis();
    
        final ScatterChart sc = new ScatterChart<>(xAxis, yAxis);
        final LineChart lc = new LineChart<>(xAxis, yAxis);
    
        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Equities");
        series1.getData().add(new XYChart.Data(4.2, 193.2));
        series1.getData().add(new XYChart.Data(2.8, 33.6));
    
        XYChart.Series series2 = new XYChart.Series();
        series2.setName("Mutual funds");
        series2.getData().add(new XYChart.Data(5.2, 229.2));
        series2.getData().add(new XYChart.Data(2.4, 37.6));
    
        sc.getData().addAll(series1);
        lc.getData().addAll(series2);
    
        Pane pane = new Pane();
        pane.getChildren().add(sc);
        pane.getChildren().add(lc);
    
        lc.setOpacity(0.5);
    
        Scene scene = new Scene(pane, 800, 600);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    
    }
    

    2) A solution is to set the bounds of the axis from the beginning:

    NumberAxis xAxis = new NumberAxis(0, 5.5, 0.5); 
    NumberAxis yAxis = new NumberAxis(0, 240, 10); 
    

提交回复
热议问题