如何为BarChart设置描边(Color)?我总是有黄条
这是我的代码
import java.util.Set;
import javafx.application.Application;
import javafx.scene.Node;
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.layout.StackPane;
import javafx.stage.Stage;
public class StackPaneTest extends Application {
@Override
public void start(Stage primaryStage) {
final CategoryAxis xAxis1 = new CategoryAxis();
final NumberAxis yAxis1 = new NumberAxis();
final BarChart<String, Number> barChart = new BarChart<String, Number>(xAxis1, yAxis1);
barChart.setAlternativeRowFillVisible(false);
barChart.setLegendVisible(false);
barChart.setAnimated(false);
XYChart.Series serie1 = new XYChart.Series();
serie1.getData().add(new XYChart.Data("Jan", 1));
serie1.getData().add(new XYChart.Data("Feb", 2));
serie1.getData().add(new XYChart.Data("Mar", 1.5));
serie1.getData().add(new XYChart.Data("Apr", 3));
serie1.getData().add(new XYChart.Data("May", 2.5));
serie1.getData().add(new XYChart.Data("Jun", 5));
serie1.getData().add(new XYChart.Data("Jul", 4));
serie1.getData().add(new XYChart.Data("Aug", 8));
serie1.getData().add(new XYChart.Data("Sep", 6.5));
serie1.getData().add(new XYChart.Data("Oct", 13));
serie1.getData().add(new XYChart.Data("Nov", 10));
serie1.getData().add(new XYChart.Data("Dec", 20));
barChart.getData().addAll(serie1);
Set<Node> barNode = barChart.lookupAll(".default-color0.chart-bar");
for(final Node bar : barNode){
bar.setStyle("-fx-stroke: GREEN");
}
StackPane stack = new StackPane();
stack.getChildren().addAll(barChart);
Scene scene = new Scene(stack, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:1)
如果您希望栏为绿色,请使用setStyle(“ - fx-bar-fill:GREEN”);
关于笔划应用 - 这是一个单独的麻烦,但它不是关于应用于栏的样式。
在一般情况下,您可以使用caspian.css或modena.css文件来观察样式应用的样本。