Data Plotting

Plotting

For plotting in Java, some libraries are necessary. Plots are new windows with visual content using the JavaFX library. Thus, new plots are created calling the launch method.

import javafx.application.Application;

public class MyPlot extends Application {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);   // call of implementation of start()
    }

    @Override
    public void start(Stage stage) {
    	// create your plot here
        }
}

A plot is an object added to Scene, which is added to a Stage. So first you have to create your Plot-object, feed your data into that plot, create a scene using the plot, set the scene on the Stage and then show the stage.


// data
int[] xData = {1, 2, 3, 4, 5};
double[] yData = {1.3, 2.1, 3.3, 4.0, 4.8};

Series series = new Series();
for (int i = 0; i < yData.length; i++) {
	series.getData().add(new Data(xData[i], yData[i]));
}

//defining the axes
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("x");
yAxis.setLabel("y");

//creating the scatter chart
ScatterChart<Number,Number> scatterChart = new ScatterChart<>(xAxis,yAxis);
scatterChart.setAnimated(false);
scatterChart.getData().add(series);
scatterChart.setTitle("x vs. y");
scatterChart.setHorizontalGridLinesVisible(false);
scatterChart.setVerticalGridLinesVisible(false);
scatterChart.setVerticalZeroLineVisible(false);

// create a scene 
Scene scene  = new Scene(scatterChart,800,600);
        
// Show chart
stage.setScene(scene);
stage.show();