JavaFX Tutorial : Membuat Grafik Line Chart

Membuat grafik Line Chart pada javaFX tidaklah sesulit yang dibayangkan. Berikut beberapa contoh Line Chart pada JavaFX.

Baca Juga : Bagaimana menggunakan Thread pada Java

1. Membuat Grafik Line Chart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 Main extends Application {
 
@Override
 public void start(Stage stage) {
 final NumberAxis xAxis = new NumberAxis();
 final NumberAxis yAxis = new NumberAxis();
 xAxis.setLabel("Number of Month");
 final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(
 xAxis, yAxis);
 
lineChart.setTitle("Line Chart");
 XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
 series.setName("My Data");
 // populating the series with data
 series.getData().add(new XYChart.Data<Number, Number>(1, 23));
 series.getData().add(new XYChart.Data<Number, Number>(2, 114));
 series.getData().add(new XYChart.Data<Number, Number>(3, 15));
 series.getData().add(new XYChart.Data<Number, Number>(4, 124));
 
Scene scene = new Scene(lineChart, 800, 600);
 lineChart.getData().add(series);
 
stage.setScene(scene);
 stage.show();
 }
 
public static void main(String[] args) {
 launch(args);
 }
 }

OUTPUT:

membuat Grafik Line Chart

2. Menambahkan Serries pada Grafik LineChart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import javafx.application.Application;
 import javafx.scene.Scene;
 import javafx.scene.chart.CategoryAxis;
 import javafx.scene.chart.LineChart;
 import javafx.scene.chart.NumberAxis;
 import javafx.scene.chart.XYChart;
 import javafx.stage.Stage;
 //from ww w . jav a2 s .c o m
 public class Main extends Application {
 
@Override
 public void start(Stage stage) {
 final CategoryAxis xAxis = new CategoryAxis();
 final NumberAxis yAxis = new NumberAxis();
 xAxis.setLabel("Month");
 final LineChart<String, Number> lineChart = new LineChart<String, Number>(
 xAxis, yAxis);
 
lineChart.setTitle("My Chart");
 
XYChart.Series<String, Number> series1 = new XYChart.Series<String, Number>();
 series1.setName("Portfolio 1");
 
series1.getData().add(new XYChart.Data<String, Number>("Jan", 23));
 series1.getData().add(new XYChart.Data<String, Number>("Feb", 14));
 series1.getData().add(new XYChart.Data<String, Number>("Mar", 15));
 
XYChart.Series<String, Number> series2 = new XYChart.Series<String, Number>();
 series2.setName("Portfolio 2");
 series2.getData().add(new XYChart.Data<String, Number>("Jan", 33));
 series2.getData().add(new XYChart.Data<String, Number>("Feb", 34));
 series2.getData().add(new XYChart.Data<String, Number>("Mar", 25));
 series2.getData().add(new XYChart.Data<String, Number>("Apr", 44));
 
XYChart.Series<String, Number> series3 = new XYChart.Series<String, Number>();
 series3.setName("Portfolio 3");
 series3.getData().add(new XYChart.Data<String, Number>("Jan", 44));
 series3.getData().add(new XYChart.Data<String, Number>("Feb", 35));
 series3.getData().add(new XYChart.Data<String, Number>("Mar", 36));
 series3.getData().add(new XYChart.Data<String, Number>("Apr", 33));
 series3.getData().add(new XYChart.Data<String, Number>("May", 31));
 
Scene scene = new Scene(lineChart, 800, 600);
 lineChart.getData().addAll(series1, series2, series3);
 
stage.setScene(scene);
 stage.show();
 }
 
public static void main(String[] args) {
 launch(args);
 }
 }

OUTPUT:

membuat Grafik Line Chart

 

Refrence

  1. Tutorial JavaFX
  2. Using JavaFX Chart

Shareit

 

Satu tanggapan untuk “JavaFX Tutorial : Membuat Grafik Line Chart

  1. Ping-balik: Membuat Grafik Line Chart menggunakan object Chart pada Java FX | Free ALL Script PHP, javascript, C++

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

twenty − 14 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.