Look at the attached movie and use an attached application
import java.util.Random;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaCharts extends Application {
public static void main(String[] args) {
launch(args);
}
NumberAxis axisY = new NumberAxis(0, 100, 10);
ObservableList<String> existingCategories = FXCollections.observableArrayList();
CategoryAxis axisX = new CategoryAxis(existingCategories);
BarChart testedBarChart = new BarChart(axisY, axisX);
VBox vb = new VBox();
@Override
public void start(Stage stage) throws Exception {
testedBarChart.setTitle("BarChart");
testedBarChart.setStyle("-fx-border-color: darkgray;");
existingCategories.addAll("category1", "category2", "category3");
addDataToChart();
Pane pane = new Pane();
pane.setPrefSize(600, 600);
pane.setPrefSize(600, 600);
pane.getChildren().add(testedBarChart);
Button button3 = new Button("Add data to three categories - click me one time");
button3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
addDataToChart();
}
});
Button button1 = new Button("Add data to the first category - click me 5 times");
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
addOneDataToChart();
}
});
vb.getChildren().addAll(pane, button1, button3);
Scene scene = new Scene(vb, 700, 700);
stage.setScene(scene);
stage.show();
}
private void addDataToChart() {
double min = 0;
double max = 100;
int amount = existingCategories.size();
ObservableList list = FXCollections.observableArrayList();
XYChart.Series serie = new XYChart.Series("SeriesName", list);
for (int i = 0; i < amount; i++) {
XYChart.Data newData = new XYChart.Data();
String category = existingCategories.get(i);
Double value = new Random().nextDouble() * (max - min) + min;
newData.setYValue(category);
newData.setXValue(value);
list.add(newData);
System.out.println(newData + " to category " + category + " value " + value);
}
testedBarChart.getData().add(serie);
}
private void addOneDataToChart() {
double min = 0;
double max = 100;
ObservableList list = FXCollections.observableArrayList();
XYChart.Series serie = new XYChart.Series("SeriesName", list);
XYChart.Data newData = new XYChart.Data();
String category = existingCategories.get(0);
Double value = new Random().nextDouble() * (max - min) + min;
newData.setYValue(category);
newData.setXValue(value);
list.add(newData);
System.out.println(newData + " to category " + category + " value " + value);
testedBarChart.getData().add(serie);
}
}
Click first button 5-6 times, and after that click the second button 1-2 times.
On movie you can see, how new data is added - per 1 data item on the first category, at first. After that - 1 data item per each of three categories, a few times. You may see, how some space is left in the second and third categories. This space can be needed, if you want to skip place for empty data, but in such case this issue need renaming.
import java.util.Random;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaCharts extends Application {
public static void main(String[] args) {
launch(args);
}
NumberAxis axisY = new NumberAxis(0, 100, 10);
ObservableList<String> existingCategories = FXCollections.observableArrayList();
CategoryAxis axisX = new CategoryAxis(existingCategories);
BarChart testedBarChart = new BarChart(axisY, axisX);
VBox vb = new VBox();
@Override
public void start(Stage stage) throws Exception {
testedBarChart.setTitle("BarChart");
testedBarChart.setStyle("-fx-border-color: darkgray;");
existingCategories.addAll("category1", "category2", "category3");
addDataToChart();
Pane pane = new Pane();
pane.setPrefSize(600, 600);
pane.setPrefSize(600, 600);
pane.getChildren().add(testedBarChart);
Button button3 = new Button("Add data to three categories - click me one time");
button3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
addDataToChart();
}
});
Button button1 = new Button("Add data to the first category - click me 5 times");
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
addOneDataToChart();
}
});
vb.getChildren().addAll(pane, button1, button3);
Scene scene = new Scene(vb, 700, 700);
stage.setScene(scene);
stage.show();
}
private void addDataToChart() {
double min = 0;
double max = 100;
int amount = existingCategories.size();
ObservableList list = FXCollections.observableArrayList();
XYChart.Series serie = new XYChart.Series("SeriesName", list);
for (int i = 0; i < amount; i++) {
XYChart.Data newData = new XYChart.Data();
String category = existingCategories.get(i);
Double value = new Random().nextDouble() * (max - min) + min;
newData.setYValue(category);
newData.setXValue(value);
list.add(newData);
System.out.println(newData + " to category " + category + " value " + value);
}
testedBarChart.getData().add(serie);
}
private void addOneDataToChart() {
double min = 0;
double max = 100;
ObservableList list = FXCollections.observableArrayList();
XYChart.Series serie = new XYChart.Series("SeriesName", list);
XYChart.Data newData = new XYChart.Data();
String category = existingCategories.get(0);
Double value = new Random().nextDouble() * (max - min) + min;
newData.setYValue(category);
newData.setXValue(value);
list.add(newData);
System.out.println(newData + " to category " + category + " value " + value);
testedBarChart.getData().add(serie);
}
}
Click first button 5-6 times, and after that click the second button 1-2 times.
On movie you can see, how new data is added - per 1 data item on the first category, at first. After that - 1 data item per each of three categories, a few times. You may see, how some space is left in the second and third categories. This space can be needed, if you want to skip place for empty data, but in such case this issue need renaming.