我创建了一个图表栏,我希望根据值的名称更改栏颜色。它有两个系列,一个显示绿色值,绿色值应为绿色,另一个为黄色。我还创建了一个css文件,但第一个问题是css中-fx-bar-fill
的get unknown属性错误。除了我不知道如何在创建系列后调用适当的颜色?我当时面对这个警告:
Mar 23, 2015 8:50:02 AM com.sun.javafx.css.parser.CSSParser parse
WARNING: CSS Error parsing file:/home/iman/workspace/AddNodeDynamicly /bin/application/chart.css: Expected LBRACE at [1,9]
我在这里创建图表
public class MostComputerizedController {
@FXML
private BarChart<String, Number> barChart;
@FXML
private CategoryAxis orgNameAxis;
@FXML
private NumberAxis yAxis;
private ObservableList<String> orgNames = FXCollections
.observableArrayList();
private DataConstructor dc = new DataConstructor();
@FXML
private void initialize() {
orgNames.addAll(dc.getSortedAssignedOrg().values());
orgNameAxis.setCategories(orgNames);
orgNameAxis.setLabel("Name of Organizations");
orgNameAxis.tickLabelFontProperty().set(Font.font(10));
yAxis.setLabel("Saturation");
orgNameAxis.getStylesheets().add(
getClass().getResource("/application/application.css").toExternalForm());
}
/*
* *
* Sets the organization to show the statistics for.
*
* @param
*/
public void setPersonData() {
XYChart.Series<String, Number> series = new XYChart.Series<>();
XYChart.Series<String, Number> seriesy = new XYChart.Series<>();
series.setName("Green");
seriesy.setName("Yellow");
for (String entryOrg : dc.getSortedAssignedOrg().values()) {
for (List<String> entryfuncType : dc.getFuncTypeOrg().values()) {
if (entryOrg.equals(entryfuncType.get(5))
&& entryfuncType.contains("hasType")) {
int yellow = Collections.frequency(entryfuncType, "yellow");
int green = Collections.frequency(entryfuncType, "Green");
int typeNumber = Collections.frequency(entryfuncType,
"hasType");
series.getData().add(
new XYChart.Data<String, Number>(entryOrg, green));
seriesy.getData().add(
new XYChart.Data<String, Number>(entryOrg, yellow));
}
}
}
barChart.getData().addAll(series,seriesy);
}
}
主要是我把它添加到舞台:
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private Model model = new Model();
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("IT-Saturation");
initRootLayout();
showOverView();
}
private void showOverView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
loader.setController(new OverViewController(model));
AnchorPane overView = (AnchorPane) loader.load();
rootLayout.setCenter(overView);
} catch (IOException e) {
e.printStackTrace();
}
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
loader.setController(new RootLayoutController(model));
rootLayout = (BorderPane) loader.load();
// show scene containing the root layout
Scene scene = new Scene(rootLayout);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
public void showMostComputerizedStatistics() {
try {
// Load the fxml file and create a new stage for the popup.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class
.getResource("/view/MostComputerized.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Saturation in Organizations");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
dialogStage.getScene().getStylesheets().add(
getClass().getResource("chart.css").toExternalForm());
MostComputerizedController controller = loader.getController();
controller.setPersonData();
dialogStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是css文件:
@CHARSET "UTF-8";
.default-color0.chart-bar {
-fx-bar-fill: green;
}
.default-color1.chart-bar {
-fx-bar-fill: yellow;
}
答案 0 :(得分:1)
如果可以使用-fx-background-color
而不是
.default-color0.chart-bar { -fx-background-color: rgb(146,208,80); }