2
我期望有條件地更改折線圖的背景顏色。在給定示例,我正在尋找修改的圖表,使得與Y值> = 3的任何部分在背景中得到紅色陰影:有條件的顏色背景JavaFX LineChart
LineChartJavaFXTest.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartJavaFXTest extends Application {
@Override public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
//defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Seconds");
yAxis.setLabel("Volume");
//creating the chart
final LineChart<Number,Number> lineChart =
new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Test Chart");
//defining a series
XYChart.Series series = new XYChart.Series();
//populating the series with data
series.getData().add(new XYChart.Data(1, 0));
series.getData().add(new XYChart.Data(2, 1));
series.getData().add(new XYChart.Data(3, 2));
series.getData().add(new XYChart.Data(4, 2));
series.getData().add(new XYChart.Data(5, 1));
series.getData().add(new XYChart.Data(6, 2));
series.getData().add(new XYChart.Data(7, 3));
series.getData().add(new XYChart.Data(8, 3));
series.getData().add(new XYChart.Data(9, 4));
series.getData().add(new XYChart.Data(10, 3));
series.getData().add(new XYChart.Data(11, 2));
series.getData().add(new XYChart.Data(12, 1));
Scene scene = new Scene(lineChart,800,600);
scene.getStylesheets().add("LineChart.css");
lineChart.getData().add(series);
lineChart.applyCss();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LineChart.css
.chart {
-fx-padding: 10px;
-fx-background-color: #010101;
-fx-legend-visible: false;
}
.chart-content {
-fx-padding: 30px;
}
.chart-title {
-fx-text-fill: #ffffff;
-fx-font-size: 1.6em;
}
.axis-label {
-fx-text-fill: #ffffff;
}
.chart-plot-background {
-fx-background-color:transparent;
}
.chart-vertical-grid-lines {
-fx-stroke:#505050;
}
.chart-horizontal-grid-lines {
-fx-stroke: #505050;
}
.chart-alternative-row-fill {
-fx-fill:transparent;
-fx-stroke: transparent;
}
.default-color0.chart-series-line {
-fx-stroke: #0000ff;
}
.default-color0.chart-line-symbol {
-fx-background-color: #0000ff, white;
}
回答了這個問題完美(覆蓋佈局劇情孩子是我想要的東西)。我發佈我的時候沒有看到這個問題。謝謝。 – Ironcache
當我調整窗口大小時,會看到奇怪的渲染效果。 –
@Holger請發表一個新問題。 – ItachiUchiha