this is a follow up to this question: How to get the color from a series in JavaFX Chart and assign it to a checkbox which has 0 answers.
I want to get my LineChart series stroke generated color, in order to set it as the text color of a checkbox.
This is my sample code (the line chart taken from Oracle tutorial):
public class LineChartSample extends Application {
#Override
public void start(Stage stage) {
HBox box = new HBox();
stage.setTitle("Line Chart Sample");
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("Stock Monitoring, 2010");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
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));
lineChart.getData().add(series);
CheckBox test = new CheckBox("test");
test.setStyle("-fx-text-fill: <series color>;");
box.getChildren().addAll(lineChart, test);
Scene scene = new Scene(box, 800, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I tried to add the series style class to the checkbox styleclass (as suggested in Getting series color from LineChart):
test.getStyleClass().addAll(series.getNode().getStyleClass());
where the series style class is a list with the values: chart-series-line, series0, and default-color0 but it didn't work.
Thanks in advance for your help.
Related
I have a problem with the BarChart only at the first upload, the categories labels surpass one another. it looks like this:
and from the second load, everything looks good:
the code:
#FXML
private ComboBox<String> subDepList;
#FXML
private BarChart<String, Number> subDepChart;
#FXML
private Button showButton;
#FXML
private Label sdErrorMessage;
#FXML
void setSubDepBarChart(Event event) {
subDepChart.getData().clear();
sdErrorMessage.setVisible(false);
boolean flag = true;
int sdId = -1;
try {
sdId = Integer.parseInt(subDepList.getValue());
} catch (NumberFormatException e) {
sdErrorMessage.setVisible(true);
flag=false;
}
if (flag) {
SubDepartment sd = Hospital.getInstance().getRealSubDepartment(sdId);
//setting sub-department bar chart
XYChart.Series<String, Number> set1 = new XYChart.Series<>();
set1.getData().add(new XYChart.Data<String, Number>("Patients", sd.getPatients().size()));
set1.getData().add(new XYChart.Data<String, Number>("Doctors", sd.getDoctors().size()));
set1.getData().add(new XYChart.Data<String, Number>("Nurses", sd.getNurses().size()));
set1.getData().add(new XYChart.Data<String, Number>("Reports", sd.getReports().size()));
subDepChart.getData().addAll(set1);
}
}
How can i fix it?
I am trying to get a screenshot of the chart node. So even though the axis values are added to the chart they are not displayed in the screenshot image..
public void saveProfitLineChartToFile(ObservableList<MonthlyRecord> monthlyRecords,String path) throws IOException {
CategoryAxis monthAxis = new CategoryAxis();
monthAxis.setLabel("Month");
NumberAxis profitAxis = new NumberAxis();
profitAxis.setLabel("Profit (Rs)");
LineChart profitLineChart = new LineChart(monthAxis, profitAxis);
XYChart.Series series1 = new XYChart.Series();
int[] profitValues = new int[monthlyRecords.size()];
int[] monthNo = new int[monthlyRecords.size()];
for (int i = 0; i < monthlyRecords.size(); i++) {
profitValues[i] = Integer.parseInt(monthlyRecords.get(i).getProfit());
monthNo[i] = Integer.parseInt(monthlyRecords.get(i).getMonth());
series1.getData().add(new XYChart.Data(monthName[monthNo[i]-1], profitValues[i]));
}
profitLineChart.getData().addAll(series1);
profitLineChart.setLegendVisible(false);
Scene sceneForChart = new Scene(profitLineChart, 800, 600);
WritableImage image = profitLineChart.snapshot(new SnapshotParameters(), null);
File file = new File(path);
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
Is it possible to make right side of BorderPane to have height of window and bottom/top sides then end with the beginning of right side?
AFAIK there is no way to do this, but you can achieve the desired result using a GridPane
private static void setBackground(Region region, Color color) {
region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
}
#Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
RowConstraints rConstranits1 = new RowConstraints();
rConstranits1.setVgrow(Priority.NEVER);
RowConstraints rConstranits2 = new RowConstraints();
rConstranits2.setVgrow(Priority.ALWAYS);
RowConstraints rConstranits3 = new RowConstraints();
rConstranits3.setVgrow(Priority.NEVER);
ColumnConstraints cConstraints1 = new ColumnConstraints();
cConstraints1.setHgrow(Priority.NEVER);
ColumnConstraints cConstraints2 = new ColumnConstraints();
cConstraints2.setHgrow(Priority.ALWAYS);
ColumnConstraints cConstraints3 = new ColumnConstraints();
cConstraints3.setHgrow(Priority.NEVER);
gridPane.getColumnConstraints().addAll(cConstraints1, cConstraints2, cConstraints3);
gridPane.getRowConstraints().addAll(rConstranits1, rConstranits2, rConstranits3);
Region top = new Region();
top.setPrefSize(300, 100);
setBackground(top, Color.RED);
Region bottom = new Region();
bottom.setPrefSize(400, 50);
setBackground(bottom, Color.YELLOW);
Region center = new Region();
setBackground(center, Color.BLUE);
Region right = new Region();
setBackground(right, Color.LIME);
right.setPrefSize(200, 500);
Region left = new Region();
setBackground(left, Color.BROWN);
left.setPrefSize(200, 400);
gridPane.add(right, 2, 0, 1, 3);
cConstraints3.maxWidthProperty().bind(right.prefWidthProperty());
cConstraints3.minWidthProperty().bind(right.prefWidthProperty());
gridPane.add(top, 0, 0, 2, 1);
rConstranits1.minHeightProperty().bind(top.prefHeightProperty());
rConstranits1.maxHeightProperty().bind(top.prefHeightProperty());
gridPane.add(bottom, 0, 2, 2, 1);
rConstranits3.minHeightProperty().bind(bottom.prefHeightProperty());
rConstranits3.maxHeightProperty().bind(bottom.prefHeightProperty());
gridPane.add(center, 1, 1);
gridPane.add(left, 0, 1);
cConstraints1.minWidthProperty().bind(left.prefWidthProperty());
cConstraints1.maxWidthProperty().bind(left.prefWidthProperty());
Scene scene = new Scene(gridPane);
primaryStage.setScene(scene);
primaryStage.show();
}
Nesting two BorderPanes is another option, which then relies on BorderPane's built in height & width management. Credit to fabian for the example basis and setBackground routine!
private static void setBackground(Region region, Color color) {
region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
}
#Override
public void start(Stage primaryStage) {
BorderPane outer = new BorderPane();
BorderPane inner = new BorderPane();
Region top = new Region();
top.setPrefSize(300, 300);
setBackground(top, Color.RED);
Region bottom = new Region();
bottom.setPrefSize(400, 200);
setBackground(bottom, Color.YELLOW);
Region right = new Region();
setBackground(right, Color.BLUE);
right.setPrefSize(200, 500);
inner.setCenter(top);
inner.setBottom(bottom);
outer.setCenter(inner);
outer.setRight(right);
Scene s = new Scene(outer);
primaryStage.setScene(s);
primaryStage.show();
}
I am new to JavaFX/RCP and trying to execute one sample JavaFX graph in RCP application. It is displaying empty dialogue, without graph. Can anyone help me in identifying the issue ?
#Execute
public void execute(final Shell shell) {
if ( shlInfrastructureReport != null && !shlInfrastructureReport.isDisposed()){
shlInfrastructureReport.close();
}
shlInfrastructureReport = new Shell(shell.getDisplay());
final FXCanvas fxCanvas = new FXCanvas(shlInfrastructureReport, SWT.NONE);
Group group = new Group();
Scene scene = new Scene(group);
final NumberAxis xAxis = new NumberAxis(1, 31, 1);
final NumberAxis yAxis = new NumberAxis();
final AreaChart<Number,Number> areaChat = new AreaChart<Number,Number>(xAxis,yAxis);
areaChat.setTitle("Sample Report");
XYChart.Series series= new XYChart.Series();
series.setName("Sample");
series.getData().add(new XYChart.Data(1, 4));
series.getData().add(new XYChart.Data(3, 10));
series.getData().add(new XYChart.Data(6, 15));
series.getData().add(new XYChart.Data(9, 8));
series.getData().add(new XYChart.Data(12, 5));
series.getData().add(new XYChart.Data(15, 18));
series.getData().add(new XYChart.Data(18, 15));
series.getData().add(new XYChart.Data(21, 13));
series.getData().add(new XYChart.Data(24, 19));
series.getData().add(new XYChart.Data(27, 21));
series.getData().add(new XYChart.Data(30, 21));
areaChat.getData().addAll(series);
group.getChildren().add(areaChat);
fxCanvas.setScene(scene);
Thanks, it got resolution. Issue is i missed to set layout.
final RowLayout layout = new RowLayout();
shlInfrastructureReport.setLayout(layout);
As you can see in the picture, the label in the third box, next to the ComboBox, has the text '...' instead of 'fase'.
I have previously attempted to increase the size of the HBox in which it resides, increasing spacing, but to no avail. Any suggestions?
public void start(Stage primaryStage) {
RadioButtonCustom inhoud = new RadioButtonCustom();
Fieldset fs = new Fieldset("eerste", inhoud);
RadioButtonCustom inhoud2 = new RadioButtonCustom();
Fieldset fs2 = new Fieldset("tweede", inhoud2);
//error
HBox hb = new HBox();
hb.minWidth(200);
Label fase = new Label("fase");
fase.setText("Fase");
fase.minWidth(500);
fase.getStyleClass().add("bordered-titled-title");
StackPane.setAlignment(fase, Pos.CENTER);
ComboBox<Integer> cb = new ComboBox<>();
hb.setSpacing(40);
for (Integer i : array) {
cb.getItems().add(i);
}
hb.getChildren().addAll(fase,cb);
Fieldset fs3 = new Fieldset("fase",hb);
// einde error
VBox vb = new VBox();
vb.getChildren().addAll(fs, fs2,fs3);
vb.setSpacing(20);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
BorderPane root = new BorderPane();
root.getChildren().add(vb);
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("/lissa/stijl.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}