如何在 JavaFx 中更改 XYChart 的颜色?
How to Change XYChart's color in JavaFx?
我应该如何更改条形图颜色。
谁能给我一些线索:
- 如何给折线图设置颜色?
- 如何设置cssclass为系列?
public class hmw3 extends Application {
@Override public void start(Stage stage) throws Exception{
stage.setTitle("HİSTOGRAM");
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("HİSTOGRAM");
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 {
hmw34.occur(textfield.getText(), series1);
bc.getData().add(series1);
bc.setLegendVisible(false);
} catch (FileNotFoundException e) {
System.out.println("Error : No such file");
}
}
});
// rengini düzenleyecek.
/** 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);
}
}
datad.nodeProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
newNode.setStyle("-fx-bar-fill: red;");
}
});
在我尝试过的不同可能性中,Mevlana Ayas 的 "setStyle" 方法是唯一可行的方法。
假设 "color" 是描述所需颜色的字符串。
例如:
String color = "hsb(0,0,0)";
String opaqueColor = "hsb(0,0,0,0.2)";
对于 ScatterChart、LineChart、AreaChart,在添加到系列的每个数据上添加以下侦听器:
data.nodeProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> ov, Node oldNode, final Node node) {
if (node != null ) {
node.setStyle(String.format("-fx-background-color: %s;", color));
}
}
}
折线图:
serie.nodeProperty().addListener((ObservableValue<? extends Node> o, Node old, Node node) -> {
if (node != null) {
if (node instanceof Path) {
node.setStyle(String.format("-fx-stroke: %s;", chartSerie.color));
}
});
面积图:
serie.nodeProperty().addListener((ObservableValue<? extends Node> o, Node old, Node node) -> {
if (node != null) {
if (node instanceof Group) {
// AreaChart, assuming FILL is FIRST child and LINE is SECOND child
Group group = (Group) node;
Path fill = (Path) group.getChildren().get(0);
fill.setStyle(String.format("-fx-fill: %s;", chartSerie.opaqueColor));
Path line = (Path) group.getChildren().get(1);
line.setStyle(String.format("-fx-stroke: %s;", chartSerie.color));
}
}
});
我应该如何更改条形图颜色。
谁能给我一些线索:
- 如何给折线图设置颜色?
- 如何设置cssclass为系列?
public class hmw3 extends Application {
@Override public void start(Stage stage) throws Exception{
stage.setTitle("HİSTOGRAM");
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("HİSTOGRAM");
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 {
hmw34.occur(textfield.getText(), series1);
bc.getData().add(series1);
bc.setLegendVisible(false);
} catch (FileNotFoundException e) {
System.out.println("Error : No such file");
}
}
});
// rengini düzenleyecek.
/** 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);
}
}
datad.nodeProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
newNode.setStyle("-fx-bar-fill: red;");
}
});
在我尝试过的不同可能性中,Mevlana Ayas 的 "setStyle" 方法是唯一可行的方法。
假设 "color" 是描述所需颜色的字符串。 例如:
String color = "hsb(0,0,0)";
String opaqueColor = "hsb(0,0,0,0.2)";
对于 ScatterChart、LineChart、AreaChart,在添加到系列的每个数据上添加以下侦听器:
data.nodeProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> ov, Node oldNode, final Node node) {
if (node != null ) {
node.setStyle(String.format("-fx-background-color: %s;", color));
}
}
}
折线图:
serie.nodeProperty().addListener((ObservableValue<? extends Node> o, Node old, Node node) -> {
if (node != null) {
if (node instanceof Path) {
node.setStyle(String.format("-fx-stroke: %s;", chartSerie.color));
}
});
面积图:
serie.nodeProperty().addListener((ObservableValue<? extends Node> o, Node old, Node node) -> {
if (node != null) {
if (node instanceof Group) {
// AreaChart, assuming FILL is FIRST child and LINE is SECOND child
Group group = (Group) node;
Path fill = (Path) group.getChildren().get(0);
fill.setStyle(String.format("-fx-fill: %s;", chartSerie.opaqueColor));
Path line = (Path) group.getChildren().get(1);
line.setStyle(String.format("-fx-stroke: %s;", chartSerie.color));
}
}
});