0

I would like to create a table which is by the right side of the graph which shows 2 columns of what x and y values have been inputted into the xSpin and ySpin spinners. I have drawn a picture of where I would like the table to reside. I have tried using textboxes in a grid pane layout to create a table and inputting the values directly into a grid of text boxes, however I couldn't add them individually to the series.

picture. enter image description here

package Grava;
public class ScatterAdd extends Application {


    private final XYSeries series = new XYSeries("Voltage");
    private final XYSeries trend = new XYSeries("Trend");
    private final XYSeriesCollection dataset = new XYSeriesCollection(series);


    ChoiceBox<String> domainLabels = new ChoiceBox<>();
    ChoiceBox<String> rangeLabels = new ChoiceBox<>();

    private JFreeChart createChart() {
        return ChartFactory.createScatterPlot("VI Characteristics", "Current", "Voltage", dataset);
    }

    @Override
    public void start(Stage stage) {

        Image image = new Image("Grava.logo.png");
        stage.getIcons().add(image);
        var chart = createChart();



        XYPlot plot = chart.getXYPlot();
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
        r.setSeriesLinesVisible(1, Boolean.TRUE);
        r.setSeriesShapesVisible(1, Boolean.FALSE);


        var equation = new TextField();
        series.addChangeListener((event) -> {
            if (series.getItemCount() > 1) {
                double[] coefficients = Regression.getOLSRegression(dataset, 0);
                double b = coefficients[0]; // intercept
                double m = coefficients[1]; // slope
                equation.setText("y = " + m + " x + " + b);
                double x = series.getDataItem(0).getXValue();
                trend.clear();
                trend.add(x, m * x + b);
                x = series.getDataItem(series.getItemCount() - 1).getXValue();
                trend.add(x, m * x + b);
                dataset.addSeries(trend);

            }
        });
        domainLabels.getSelectionModel().selectedItemProperty().addListener((ov, s0, s1) -> {
            chart.getXYPlot().getDomainAxis().setLabel(s1);
        });
        rangeLabels.getSelectionModel().selectedItemProperty().addListener((ov, s0, s1) -> {
            chart.getXYPlot().getRangeAxis().setLabel(s1);
        });

        domainLabels.getItems().addAll("Current", "Seconds");
        domainLabels.setValue("Current");

        rangeLabels.getItems().addAll("Voltage", "Metres");
        rangeLabels.setValue("Voltage");

        var xSpin = new Spinner<Double>(-10000000.000, 10000000.000, 0, 0.1);
        xSpin.setEditable(true);
        xSpin.setPromptText("Xvalue");

        var ySpin = new Spinner<Double>(-10000000.000, 10000000.000, 0, 0.1);
        ySpin.setEditable(true);
        ySpin.setPromptText("Yvalue");

        var button = new Button("Add");
        button.setOnAction(ae -> series.add(xSpin.getValue(), ySpin.getValue()));
        HBox xBox = new HBox();
        xBox.getChildren().addAll(domainLabels);

        HBox yBox = new HBox();
        yBox.getChildren().addAll(rangeLabels);

        var enter = new ToolBar(xBox, xSpin, yBox, ySpin, button, equation);
        BorderPane.setAlignment(enter, Pos.CENTER);

        BorderPane root = new BorderPane();
        root.setCenter(new ChartViewer(chart));
        root.setBottom(enter);

        stage.setTitle("ScatterAdd");
        stage.setScene(new Scene(root, 720, 480));
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
  • This in an interesting question, but it currently includes multiple questions in one; it should focus on one problem only. Rather than a table, I'd propose editing the point selected using a `ChartMouseListenerFX`. If you agree, please edit your question accordingly, noting the spurious `xSpin` after the button and repeated addition of `trend` to the `dataset`. – trashgod Dec 23 '22 at 00:33
  • i have edited the question with a handdrawn image of what i would like the table to look like – armitageshanks99 Dec 23 '22 at 09:58
  • In this case, you should be able to use a `TableView` and create its model using the `List` returned by `XYSeries::getItems`, but I haven't tried; please [edit] your question to include a [mre] that shows your revised approach. I'll pursue the mouse listener approach separately. – trashgod Dec 23 '22 at 13:20
  • I have attempted to create a table with TableView with 2 columns but don't know how to separate the x and y values from the list returned from getItems().help would be appreciated – armitageshanks99 Jan 02 '23 at 21:50
  • Each item in the list is an `XYDataItem` with accessors for x and y. – trashgod Jan 02 '23 at 22:20
  • how would I update the table with the XYDataItem when values are added when the add button is selected(in the add button handler.) – armitageshanks99 Jan 02 '23 at 22:36
  • I'm guessing the table's cell value factory could retrieve the value; but I haven't tried. – trashgod Jan 02 '23 at 22:48

0 Answers0