首页 > 解决方案 > 获取折线图系列描边颜色

问题描述

这是对这个问题的跟进:如何从 JavaFX 图表中的系列中获取颜色并将其分配给具有 0 个答案的复选框。

我想获取我的 LineChart 系列笔划生成的颜色,以便将其设置为复选框的文本颜色。

这是我的示例代码(取自 Oracle 教程的折线图):

public class LineChartSample extends Application {

    @Override
    public void start(Stage stage) {

        HBox box = new HBox();

        stage.setTitle("Line Chart Sample");
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));

        lineChart.getData().add(series);

        CheckBox test = new CheckBox("test");

        test.setStyle("-fx-text-fill: <series color>;");

        box.getChildren().addAll(lineChart, test);

        Scene scene = new Scene(box, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

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

我尝试将系列样式类添加到复选框样式类中(如从 LineChart 获取系列颜色中所建议的那样):

test.getStyleClass().addAll(series.getNode().getStyleClass());

其中系列样式类是一个包含以下值的列表:chart-series-line、series0 和 default-color0,但它不起作用。

在此先感谢您的帮助。

标签: javafxcharts

解决方案


推荐阅读