Difference between revisions of "Java FX LineChart basics"

From MyWiki
Jump to: navigation, search
Line 4: Line 4:
 
To create a line chart, at a minimum, you must define two axes, create the LineChart object by instantiating the LineChart class, create one or more series of data by using the XYChart.Series class, and assign the data to the chart. Example 32-1 implements these tasks.<br><br>
 
To create a line chart, at a minimum, you must define two axes, create the LineChart object by instantiating the LineChart class, create one or more series of data by using the XYChart.Series class, and assign the data to the chart. Example 32-1 implements these tasks.<br><br>
 
Example 32-1 Simple Line Chart<br>
 
Example 32-1 Simple Line Chart<br>
 +
 +
<source lang="java">
 +
 +
import javafx.application.Application;
 +
import javafx.scene.Scene;
 +
import javafx.scene.chart.LineChart;
 +
import javafx.scene.chart.NumberAxis;
 +
import javafx.scene.chart.XYChart;
 +
import javafx.stage.Stage;
 +
 +
 +
public class LineChartSample extends Application {
 +
 +
    @Override public void start(Stage stage) {
 +
        stage.setTitle("Line Chart Sample");
 +
        //defining the axes
 +
        final NumberAxis xAxis = new NumberAxis();
 +
        final NumberAxis yAxis = new NumberAxis();
 +
        xAxis.setLabel("Number of Month");
 +
        //creating the chart
 +
        final LineChart<Number,Number> lineChart =
 +
                new LineChart<Number,Number>(xAxis,yAxis);
 +
               
 +
        lineChart.setTitle("Stock Monitoring, 2010");
 +
        //defining a series
 +
        XYChart.Series series = new XYChart.Series();
 +
        series.setName("My portfolio");
 +
        //populating the series with data
 +
        series.getData().add(new XYChart.Data(1, 23));
 +
        series.getData().add(new XYChart.Data(2, 14));
 +
        series.getData().add(new XYChart.Data(3, 15));
 +
        series.getData().add(new XYChart.Data(4, 24));
 +
        series.getData().add(new XYChart.Data(5, 34));
 +
        series.getData().add(new XYChart.Data(6, 36));
 +
        series.getData().add(new XYChart.Data(7, 22));
 +
        series.getData().add(new XYChart.Data(8, 45));
 +
        series.getData().add(new XYChart.Data(9, 43));
 +
        series.getData().add(new XYChart.Data(10, 17));
 +
        series.getData().add(new XYChart.Data(11, 29));
 +
        series.getData().add(new XYChart.Data(12, 25));
 +
       
 +
        Scene scene  = new Scene(lineChart,800,600);
 +
        lineChart.getData().add(series);
 +
     
 +
        stage.setScene(scene);
 +
        stage.show();
 +
    }
 +
 +
    public static void main(String[] args) {
 +
        launch(args);
 +
    }
 +
}
 +
 +
</source>

Revision as of 08:03, 15 July 2015

Reference : https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/line-chart.htm#CIHGBCFI
Creating a Line Chart

To create a line chart, at a minimum, you must define two axes, create the LineChart object by instantiating the LineChart class, create one or more series of data by using the XYChart.Series class, and assign the data to the chart. Example 32-1 implements these tasks.

Example 32-1 Simple Line Chart

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
 
 
public class LineChartSample extends Application {
 
    @Override public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);
 
        lineChart.setTitle("Stock Monitoring, 2010");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));
 
        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);
 
        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}