How can I change the color of a bar chart?
Does anyone have any tips on:
- How to adjust the color of line charts?
- How to assign a CSS class to series?
public class CustomBarChart extends Application {
@Override public void start(Stage stage) throws Exception{
stage.setTitle("CUSTOM HISTOGRAM");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis(0,50,10);
final BarChart<String,Number> bc =
new BarChart<String,Number>(xAxis,yAxis);
bc.setTitle("CUSTOM HISTOGRAM");
final VBox verticalbox = new VBox();
final HBox horizontalbox = new HBox();
final Button draw = new Button("DRAW");
CheckBox red = new CheckBox("RED");
red.setSelected(true);
CheckBox blue = new CheckBox("BLUE");
final TextField textfield = new TextField();
horizontalbox.setAlignment(Pos.BOTTOM_RIGHT);
horizontalbox.setSpacing(46);
String filename = textfield.getText();
XYChart.Series series1 = new XYChart.Series();
bc.setPrefSize(800, 600);
horizontalbox.getChildren().addAll(draw, red, blue,textfield);
verticalbox.getChildren().addAll(bc, horizontalbox);
horizontalbox.setPadding(new Insets(10, 10, 10, 50));
Scene scene = new Scene(new Group());
((Group)scene.getRoot()).getChildren().add(verticalbox);
stage.setScene(scene);
stage.show();
//Setting the button on action if its clicked
draw.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
CustomBarChartUtil.occur(textfield.getText(), series1);
bc.getData().add(series1);
bc.setLegendVisible(false);
} catch (FileNotFoundException e) {
System.out.println("Error : No such file");
}
}
});
// Customize the color.
/** if (red.isSelected())
bc.setStyle("-fx-bar-fill: #000080;"); // red box checked
else if (blue.isSelected())
bc.setStyle("-fx-bar-fill: #b22222;");// The Blue check box checked*/
}
public static void main(String[] args) {
launch(args);
}
}