繁体   English   中英

JavaFX折线图更改颜色形状

[英]JavaFX line chart change color shape

我有这个窗口:

@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();

}

它是这样的:

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

如何更改形状的颜色?

我建议使用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;");
}

在此处输入图片说明

那一些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的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM