我是CSS的新手,并试图为JavaFX中的图表制作MWE。在某些情况下,我已经能够使它工作,但是当我尝试在root用户中定义变量时,我的问题弹出了。我最终要做的是更新代码中的chart0-color
和chart1-color
的值,但是现在我正试图将它们定义为.css中的变量。
我通过CSSLint运行它,它给出了有关邻接类的警告,但没有错误。
如果我用-chart0-color
或-chart1-color
之类的任何其他颜色字符串替换red
和green
,问题就消失了。
您在我的CSS中看到任何问题吗?
dummy.css:
.root{
-chart0-color: #ff0000;
-chart1-color: #00ff00;
}
.default-color0.chart-legend-item-symbol { -fx-background-color: -chart0-color;}
.default-color0.chart-series-line {-fx-stroke: -chart0-color;}
.default-color0.chart-series-area-line {-fx-stroke: -chart0-color;}
.default-color0.chart-series-area-fill {-fx-fill: -chart0-color;}
.default-color1.chart-legend-item-symbol { -fx-background-color: -chart1-color;}
.default-color1.chart-series-line {-fx-stroke: -chart1-color;}
.default-color1.chart-series-area-line {-fx-stroke: -chart1-color;}
.default-color1.chart-series-area-fill {-fx-fill: -chart1-color;}
CSSAreaChartMWE.java:
package csstest;
import java.util.function.Function;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CSSAreaChartMWE extends Application {
private ObservableList<XYChart.Data<Number, Number>> functionToData(Function<Double, Double> f, double minx, double maxx) {
ObservableList<XYChart.Data<Number, Number>> list = FXCollections.observableArrayList();
double dx = (maxx-minx)/(100);
double x = minx;
while (x < maxx) {
list.add(new XYChart.Data<>(x, f.apply(x))); x+=dx;
}
return list;
}
@Override
public void start(Stage primaryStage) throws Exception {
double minx = 0;
double maxx = 6*Math.PI;
double amp = 5;
double period = (maxx-minx)/6;
Series<Number, Number> sin = new Series<>("sin", functionToData((x) -> amp*Math.sin(x/period), minx, maxx));
Series<Number, Number> cos = new Series<>("cos", functionToData((x) -> amp*Math.cos(x/period), minx, maxx));
ObservableList<Series<Number, Number>> series = FXCollections.observableArrayList();
series.add(sin);
series.add(cos);
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
AreaChart chart = new AreaChart(xAxis, yAxis, series);
chart.getStylesheets().add(getClass().getResource("dummy.css").toExternalForm());
chart.setStyle("chart1-color: black; chart2-color: red");
VBox box = new VBox();
box.getChildren().add(chart);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String [] args) {
launch(args);
}
}
最后是错误:
WARNING: Could not resolve '-chart0-color' while resolving lookups for '-fx-fill' from rule '*.default-color0.chart-series-area-fill' in stylesheet file:/*****/csstest/dummy.css
Jan 31, 2019 6:01:09 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Could not resolve '-chart0-color' while resolving lookups for '-fx-stroke' from rule '*.default-color0.chart-series-area-line' in stylesheet file:/*****/classes/csstest/dummy.css
.......