How to remove legends in javafx line chart

我与影子孤独终老i 提交于 2019-12-02 02:07:39

Since you are already dealing with Legend, you can work with its items, removing those you don't need, so the legend shows only two items.

Using streams, you can mark the first two items as "Valid"/"Invalid" and the rest as "Remove", for instance, and finally you just remove these last items.

private void updateStyleSheet() {
    Legend legend = (Legend)lineChart.lookup(".chart-legend");
    AtomicInteger count = new AtomicInteger();
    legend.getItems().forEach(item->{
        if(count.get()==0){
            item.setText("Valid");
        } else if(count.get()==1){
            item.setText("Invalid");
        } else {
            item.setText("Remove");
        }
        count.getAndIncrement();
    });
    legend.getItems().removeIf(item->item.getText().equals("Remove"));

    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!