JavaFX LineChart: Insert new data in the middle of the chart

混江龙づ霸主 提交于 2019-12-05 20:18:16

I do something like this (my data is string, number). I can't help but thinking there must be a better way. I've put this all in a method so I can just call addSorted(series,newData);

final Comparator<XYChart.Data<String, Number>> comparator = 
        (XYChart.Data<String, Number> o1, XYChart.Data<String, Number> o2) -> 
                o1.getXValue().compareTo(o2.getXValue());
lineChart.getData().get(0).getData().add(new XYChart.Data<>("2020", 1));
XYChart.Series newSeries = new XYChart.Series(lineChart.getData().get(0).getData().sorted(comparator));
lineChart.getData().add(0,newSeries);

There is a new answer to this problem with JavaFX 9.

If you have JDK9, try

lineChart.setAxisSortingPolicy(LineChart.SortingPolicy.X_AXIS);

(this is the default setting for JavaFX 9)

If you want for data to be plotted in the order it was added use:

lineChart.setAxisSortingPolicy(LineChart.SortingPolicy.NONE);

See information here: http://download.java.net/jdk9/jfxdocs/javafx/scene/chart/LineChart.html#axisSortingPolicyProperty

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