JavaFX着色XYChart.Series [英] JavaFX Coloring XYChart.Series
问题描述
我试图风格我的JavaFX区域图,但是找不到任何方法设置颜色的特定系列。我知道,我可以通过CSS样式,但我想在代码中也做。
I am trying to style my JavaFX Area-Chart, but can't find any way to set color to specific series. I know, that I can style via CSS, but i want to do it in the code also.
我该怎么做?
1.)如何使用内联样式将颜色设置为面积图?
How can i do that? 1.) How to set a color to Area-Chart with Inline Styles?
感谢您的帮助。
@ Jose:
我这样做之前,但它对我来说不起作用。
I done it before like this, but it does not work for me!
@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();
}
推荐答案
http://stackoverflow.com/a/26733056/3956070\">answer 在Java 7中不起作用,因为默认的CSS样式表是Caspian,而不是Modena。
This answer does not work in Java 7, because the default CSS stylesheet is Caspian, not Modena.
在Caspian中,未定义主颜色调色板的这些常数: CHART_COLOR_1
, CHART_COLOR_1_TRANS_20
,...
In Caspian these constants for the main color palette are not defined: CHART_COLOR_1
, CHART_COLOR_1_TRANS_20
,...
因此,如果要应用内联样式,可以使用查找方法查找该系列,并将样式应用于符号,线条和/或区域,基于他们的CSS选择器。例如:
So if you want to apply inline styling one way to do it is by finding the series with a lookup and apply your styling to symbols, lines and/or areas, based on their CSS selectors. For instance:
@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 + "); ");
}
EDIT
上面的解决方案最多可以工作8个系列。
The above solution will work up to 8 series.
对于任何数量的系列,其他方法也可以工作:
For any number of series this other approach will work too:
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 + "); ");
}
}
这篇关于JavaFX着色XYChart.Series的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!