You may use an attached application, and attached movie.
Code:
import java.util.Random;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
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");
Pane pane = new Pane();
pane.setPrefSize(600, 600);
pane.setPrefSize(600, 600);
pane.getChildren().add(testedBarChart);
vb.getChildren().addAll(pane, getAddItemHBox(), getRemoveDataDialog());
Scene scene = new Scene(vb, 700, 700);
stage.setScene(scene);
stage.show();
}
public HBox getRemoveDataDialog() {
HBox hb = new HBox();
Label lb = new Label("From position");
final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).build();
Button bt = ButtonBuilder.create().text("Remove!").build();
bt.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
int index = Integer.parseInt(tf.getText());
testedBarChart.getData().remove(index);
}
});
hb.getChildren().addAll(lb, tf, bt);
return hb;
}
public HBox getAddItemHBox() {
HBox hb = new HBox();
Label lb = new Label("Add series named ");
final TextField tf = TextFieldBuilder.create().prefWidth(50).id("Named").build();
Label minLabel = new Label(" min ");
final TextField minText = TextFieldBuilder.create().prefWidth(50).build();
Label maxLabel = new Label(" max ");
final TextField maxText = TextFieldBuilder.create().prefWidth(50).build();
Button bt = ButtonBuilder.create().text("Add!").build();
bt.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
String serieName = tf.getText();
double min = Double.parseDouble(minText.getText());
double max = Double.parseDouble(maxText.getText());
int amount = 3;
ObservableList list = FXCollections.observableArrayList();
XYChart.Series serie = new XYChart.Series(serieName, 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);
}
});
hb.getChildren().addAll(lb, tf, minLabel, minText, maxLabel, maxText, bt);
return hb;
}
}
To reproduce issue:
type values:
serie name: any string;
min: 0;
max: 100;
click add 3 times.
type "1" to remove dialog; and click button to remove the data.
You may see, how some space was left after removed data.
After that click "add" button again to add data again.
You may see, that color was not applied properly.
Code:
import java.util.Random;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
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");
Pane pane = new Pane();
pane.setPrefSize(600, 600);
pane.setPrefSize(600, 600);
pane.getChildren().add(testedBarChart);
vb.getChildren().addAll(pane, getAddItemHBox(), getRemoveDataDialog());
Scene scene = new Scene(vb, 700, 700);
stage.setScene(scene);
stage.show();
}
public HBox getRemoveDataDialog() {
HBox hb = new HBox();
Label lb = new Label("From position");
final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).build();
Button bt = ButtonBuilder.create().text("Remove!").build();
bt.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
int index = Integer.parseInt(tf.getText());
testedBarChart.getData().remove(index);
}
});
hb.getChildren().addAll(lb, tf, bt);
return hb;
}
public HBox getAddItemHBox() {
HBox hb = new HBox();
Label lb = new Label("Add series named ");
final TextField tf = TextFieldBuilder.create().prefWidth(50).id("Named").build();
Label minLabel = new Label(" min ");
final TextField minText = TextFieldBuilder.create().prefWidth(50).build();
Label maxLabel = new Label(" max ");
final TextField maxText = TextFieldBuilder.create().prefWidth(50).build();
Button bt = ButtonBuilder.create().text("Add!").build();
bt.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
String serieName = tf.getText();
double min = Double.parseDouble(minText.getText());
double max = Double.parseDouble(maxText.getText());
int amount = 3;
ObservableList list = FXCollections.observableArrayList();
XYChart.Series serie = new XYChart.Series(serieName, 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);
}
});
hb.getChildren().addAll(lb, tf, minLabel, minText, maxLabel, maxText, bt);
return hb;
}
}
To reproduce issue:
type values:
serie name: any string;
min: 0;
max: 100;
click add 3 times.
type "1" to remove dialog; and click button to remove the data.
You may see, how some space was left after removed data.
After that click "add" button again to add data again.
You may see, that color was not applied properly.