[英]JavaFX Coloring XYChart.Series
我正在嘗試設置JavaFX Area-Chart的樣式,但是找不到任何將顏色設置為特定系列的方法。 我知道,我可以通過CSS設置樣式,但是我也想在代碼中進行樣式設置。
我怎樣才能做到這一點? 1.)如何用內聯樣式為Area-Chart設置顏色?
謝謝您的幫助。
@Jose:
我以前是這樣做的,但是對我不起作用!
@Override
public void start(Stage primaryStage)
{
final AreaChart<String, Number> areaChart = new AreaChart<>(new CategoryAxis(), new NumberAxis());
ObservableList<XYChart.Data<String, Integer>> xyList = FXCollections.observableArrayList(
new XYChart.Data<>("P1", 30),
new XYChart.Data<>("P2", 40),
new XYChart.Data<>("P3", 30));
XYChart.Series series = new XYChart.Series(xyList);
areaChart.getData().addAll(series);
Button button = new Button("Change style");
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent arg0)
{
int redColor = 0, greenColor = 127, blueColor = 195;
double opacity = 0.3;
areaChart.setStyle("CHART_COLOR_1: rgb(" + redColor + "," + greenColor + "," + blueColor + "); "
+ "CHART_COLOR_1_TRANS_20: rgba(" + redColor + "," + greenColor + "," + blueColor + "," + opacity + ");");
}
});
VBox root = new VBox();
root.getChildren().addAll(button, areaChart);
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
這個答案在Java 7中不起作用,因為默認的CSS樣式表是里海而不是摩德納。
在里海CHART_COLOR_1
, CHART_COLOR_1_TRANS_20
主調色板的這些常量: CHART_COLOR_1
, CHART_COLOR_1_TRANS_20
,...
因此,如果要應用內嵌樣式的一種方法是通過查找系列來查找樣式,然后根據其CSS選擇器將樣式應用到符號,線條和/或區域。 例如:
@Override
public void start(Stage primaryStage) {
final AreaChart<String, Number> areaChart = new AreaChart<>(new CategoryAxis(), new NumberAxis());
ObservableList<XYChart.Data<String, Integer>> xyList = FXCollections.observableArrayList(
new XYChart.Data<>("P1", 30),
new XYChart.Data<>("P2", 40),
new XYChart.Data<>("P3", 30));
XYChart.Series series = new XYChart.Series(xyList);
areaChart.getData().addAll(series);
Scene scene = new Scene(areaChart, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
int redColor = 0, greenColor = 127, blueColor = 195;
double opacity = 0.3;
for(Node symbol : areaChart.lookupAll(".default-color0.chart-area-symbol")){
symbol.setStyle("-fx-background-color: rgb(" + redColor + "," + greenColor + "," + blueColor + "), white; ");
}
Node line = areaChart.lookup(".default-color0.chart-series-area-line");
line.setStyle("-fx-stroke: rgb(" + redColor + "," + greenColor + "," + blueColor + "); ");
Node area = areaChart.lookup(".default-color0.chart-series-area-fill");
area.setStyle("-fx-fill: rgba(" + redColor + "," + greenColor + "," + blueColor + "," + opacity + "); ");
}
編輯
上述解決方案最多可以使用8系列。
對於任何數量的系列,其他方法也將起作用:
int numSeries=10;
final int[] redColor = new int[numSeries];
Arrays.fill(redColor, 0);
final int[] greenColor =new int[numSeries];
Arrays.fill(greenColor, 127);
final int[] blueColor = new int[numSeries];
Arrays.fill(blueColor, 195);
double opacity = 0.3;
for(int i=0; i<numSeries; i++){
for(Node n : areaChart.lookupAll(".series"+i)){
n.setStyle("-fx-background-color: rgb(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "), white; "
+ "-fx-stroke: rgb(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "); "
+ "-fx-fill: rgba(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "," + opacity + "); ");
}
}
您可以使用此:
chart-series-area-line series<i> default-color<j>
系列的索引在哪里,系列的顏色索引在哪里
在CSS參考specificated 此相同,你可以用你的areaChart的功能的setStyle使用
您可以使用此:
Node line = areaChart.lookup(".default-color0.chart-series-area-line");
line.setStyle("-fx-stroke: rgb(" + redColor + "," + greenColor + "," + blueColor + "); ");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.