JavaFX折线图更改颜色形状 [英] JavaFX line chart change color shape

查看:200
本文介绍了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();

}

这是它的外观:

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

I want to change the color of series, but what I can achieve is to change color of line, but shape not.

如何更改形状的颜色?

推荐答案

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

I would suggest using CSS. The key here is looking up all the nodes associated with a series and changing the color of the nodes. The first series is .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);
    }
}

更新:尝试一下!

Update: Try this!

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;");
}

这篇关于JavaFX折线图更改颜色形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆