JavaFX折线图更改颜色形状

时间:2019-05-30 14:37:32

标签: java javafx

我有这个窗口:

@Override
public void start(Stage primaryStage){

    NumberAxis xAxis = new NumberAxis(1960, 2020, 10);
    xAxis.setLabel("Years");

    NumberAxis yAxis = new NumberAxis(0, 350, 50);
    yAxis.setLabel("No.of schools");

    LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
    lineChart.setTitle("Chart");

    XYChart.Series<Number, Number> series = new XYChart.Series<>();
    series.setName("No of schools in an year");
    Platform.runLater(() -> 
        series.getNode().lookup(".chart-series-line").setStyle("-fx-stroke: black;")
    );

    series.getData().add(new XYChart.Data<>(1970, 15));
    series.getData().add(new XYChart.Data<>(1980, 30));
    series.getData().add(new XYChart.Data<>(1990, 60));
    series.getData().add(new XYChart.Data<>(2000, 120));
    series.getData().add(new XYChart.Data<>(2013, 240));
    series.getData().add(new XYChart.Data<>(2014, 300));

    lineChart.getData().add(series);

    var scene = new Scene(lineChart);

    primaryStage.setScene(scene);
    primaryStage.show();

}

外观如下:

我想更改系列的颜色,但是我可以实现的是更改线条的颜色,但不更改形状。

如何更改形状的颜色?

2 个答案:

答案 0 :(得分:2)

我建议使用CSS。此处的关键是查找与系列关联的所有节点并更改节点的颜色。第一个系列是.series0

  

关键代码:

Set<Node> nodes = lineChart.lookupAll(".series" + 0);
for (Node n : nodes) {
    n.setStyle("-fx-background-color: black, white;\n"
            + "    -fx-background-insets: 0, 2;\n"
            + "    -fx-background-radius: 5px;\n"
            + "    -fx-padding: 5px;");
}
  

完整代码

import java.util.Set;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
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 ScatterChartSample extends Application {

    @Override
    public void start(Stage stage) {
        NumberAxis xAxis = new NumberAxis(1960, 2020, 10);
        xAxis.setLabel("Years");

        NumberAxis yAxis = new NumberAxis(0, 350, 50);
        yAxis.setLabel("No.of schools");

        LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
        lineChart.setTitle("Chart");

        XYChart.Series<Number, Number> series = new XYChart.Series<>();
        series.setName("No of schools in an year");
        Platform.runLater(()
                -> {

            Set<Node> nodes = lineChart.lookupAll(".series" + 0);
            for (Node n : nodes) {
                n.setStyle("-fx-background-color: black, white;\n"
                        + "    -fx-background-insets: 0, 2;\n"
                        + "    -fx-background-radius: 5px;\n"
                        + "    -fx-padding: 5px;");
            }

            series.getNode().lookup(".chart-series-line").setStyle("-fx-stroke: black;");
        });

        series.getData().add(new XYChart.Data<>(1970, 15));
        series.getData().add(new XYChart.Data<>(1980, 30));
        series.getData().add(new XYChart.Data<>(1990, 60));
        series.getData().add(new XYChart.Data<>(2000, 120));
        series.getData().add(new XYChart.Data<>(2013, 240));
        series.getData().add(new XYChart.Data<>(2014, 300));

        lineChart.getData().add(series);

        var scene = new Scene(lineChart);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  

更新:试试看!

for (Data<Number, Number> entry : series.getData()) {      
    entry.getNode().setStyle("-fx-background-color: black, white;\n"
        + "    -fx-background-insets: 0, 2;\n"
        + "    -fx-background-radius: 5px;\n"
        + "    -fx-padding: 5px;");
}

enter image description here

答案 1 :(得分:1)

一些CSS呢?这些是可与折线图一起使用的css列表。您可以结合使用lookupsetStyle和css属性来动态更改样式。

.chart {
    -fx-padding: 10px;
    -fx-background-image: url("icon.png");   
}
.chart-content {
    -fx-padding: 30px;    
}

.chart-title {
  -fx-text-fill: #4682b4;
  -fx-font-size: 1.6em;
}

.axis-label {
  -fx-text-fill: #4682b4;
}

.chart-legend {
   -fx-background-color:  transparent;
   -fx-padding: 20px;
}

.chart-legend-item-symbol{
   -fx-background-radius: 0;
}

.chart-legend-item{
    -fx-text-fill: #191970;
}

.chart-plot-background {
    -fx-background-color: #e2ecfe;
}
.chart-vertical-grid-lines {
    -fx-stroke: #3278fa;
}
.chart-horizontal-grid-lines {
    -fx-stroke: #3278fa;
}
.chart-alternative-row-fill {
    -fx-fill: #99bcfd;
    -fx-stroke: transparent;
    -fx-stroke-width: 0;
}

有关https://docs.oracle.com/javafx/2/charts/css-styles.htm的更多信息