JavaFX ScatterChart使用空数据集初始化时不显示图例符号

时间:2015-05-11 15:08:44

标签: java plot javafx

我有一个ScatterChart,我想用空数据集初始化;但是,当我初始化所有内容时,图例符号将永远不会显示出来。我使用Oracle的ScatterChart示例来模拟问题:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class ScatterChartSample extends Application {

    @Override public void start(Stage stage) {
        stage.setTitle("Scatter Chart Sample");
        final NumberAxis xAxis = new NumberAxis(0, 10, 1);
        final NumberAxis yAxis = new NumberAxis(-100, 500, 100);        
        final ScatterChart<Number,Number> sc = new
            ScatterChart<>(xAxis,yAxis);
        xAxis.setLabel("Age (years)");                
        yAxis.setLabel("Returns to date");
        sc.setTitle("Investment Overview");
        ObservableList<XYChart.Data> data =
                   FXCollections.observableArrayList();
        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Equities");
        series1.setData(data);

        XYChart.Series series2 = new XYChart.Series();
        series2.setName("Mutual funds");
        series2.getData().add(new XYChart.Data(5.2, 229.2));
        series2.getData().add(new XYChart.Data(2.4, 37.6));
        series2.getData().add(new XYChart.Data(3.2, 49.8));
        series2.getData().add(new XYChart.Data(1.8, 134));
        series2.getData().add(new XYChart.Data(3.2, 236.2));
        series2.getData().add(new XYChart.Data(7.4, 114.1));
        series2.getData().add(new XYChart.Data(3.5, 323));
        series2.getData().add(new XYChart.Data(9.3, 29.9));
        series2.getData().add(new XYChart.Data(8.1, 287.4));

        sc.getData().addAll(series1, series2);
        Scene scene  = new Scene(sc, 500, 400);
        stage.setScene(scene);
        stage.show();
        XYChart.Series series3 = new XYChart.Series();
        series3.setName("Equities");
        series1.getData().add(new XYChart.Data(4.2, 193.2));
    }

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

我会发布一张我得到的结果的图片,但我没有足够的代表。

1 个答案:

答案 0 :(得分:1)

问题是因为您的数据集为空。

查看ScatterChart.java(JAVA 1.8u66)中的代码,您会看到他们只会将系列中的样式类复制到图例符号!series.getData().isEmpty()

/**
 * This is called whenever a series is added or removed and the legend needs to be updated
 */
@Override protected void updateLegend() {
    legend.getItems().clear();
    if (getData() != null) {
        for (int seriesIndex=0; seriesIndex< getData().size(); seriesIndex++) {
            Series<X,Y> series = getData().get(seriesIndex);
            LegendItem legenditem = new LegendItem(series.getName());
            if (!series.getData().isEmpty() && series.getData().get(0).getNode() != null) {
                legenditem.getSymbol().getStyleClass().addAll(series.getData().get(0).getNode().getStyleClass());
            }
            legend.getItems().add(legenditem);
        }
    }
    if (legend.getItems().size() > 0) {
        if (getLegend() == null) {
            setLegend(legend);
        }
    } else {
        setLegend(null);
    }
}

他们需要一个非空系列来复制,因为样式是数据点的属性,而不是像你期望的那样整个系列。

无论如何,它很容易解决。只需在设置图例属性之前添加数据点,即可按需要调用代码。例如,我这样做:

    rollSeries.getData().add(new XYChart.Data<>(0,0));
    rollSeries.setName("Roll");

如果您注意到所有教程和示例都预装了数据集。他们必须没有涵盖最初空白系列的测试用例。