如何按日期对LineChart X轴进行排序?
现在我的LineChart像这样
我试图削减日期并将其转换为int,但是现在我不知道该怎么办…
datesToCompare.addAll(LastHoursAndDates.keySet()); // dates in String eg. 2015-12-25
List<Integer> year = new ArrayList<Integer>(); // list for year after split
List<Integer> month = new ArrayList<Integer>();// list for month after split
List<Integer> days = new ArrayList<Integer>(); // list for days after split
for(int i =0;i < LastHoursAndDates.size();i++)
{
sorting = datesToCompare.get(i).split("-");
year.add(Integer.valueOf(sorting[0]));
month.add(Integer.valueOf(sorting[1]));
days.add(Integer.valueOf(sorting[2]));
for(int j =0;j < LastHoursAndDates.size();j++)
{
if(year.get(i) == year.get(j))
{
if(month.get(i) == month.get(j))
{
//???????
}
}
}
}//for
也许有人遇到过同样的问题?
解决方法:
请注意,您使用的格式(YYYY-MM-DD)具有很好的属性,即格式化字符串的字典顺序与日期顺序相同.因此,在此处在x轴上排序字符串就足够了.我看到的最简单的方法是使用“类别”轴,并仅使用提供SortedList< Data< String,Number>>到图表的系列.然后,当您将任何数据添加到基础列表时,数据在图表中仍然保持正确的顺序.
这是SSCCE.在此示例中,对于任何给定日期,我只有一个值. addData(…)方法要么创建一个新的数据点(如果在该日期不存在),要么将y值添加到现有数据点.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Spinner;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class LineChartWithDatesAsStrings extends Application {
private DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE ;
@Override
public void start(Stage primaryStage) {
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
ObservableList<Data<String, Number>> data = FXCollections.observableArrayList() ;
SortedList<Data<String, Number>> sortedData = new SortedList<>(data, (data1, data2) ->
data1.getXValue().compareTo(data2.getXValue()));
LineChart<String, Number> chart = new LineChart<>(xAxis, yAxis);
chart.getData().add(new Series<>(sortedData));
chart.setAnimated(false);
final int dayRange = 60 ;
LocalDate today = LocalDate.now() ;
Random rng = new Random();
for (int i = 0; i < 20 ; i++) {
LocalDate date = today.minusDays(rng.nextInt(dayRange));
String formattedDate = formatter.format(date);
double value = rng.nextDouble() ;
addData(data, formattedDate, value);
}
DatePicker datePicker = new DatePicker();
Spinner<Double> valuePicker = new Spinner<>(0.0, 1.0, 0, 0.1);
valuePicker.setEditable(true);
Button addButton = new Button("Add");
addButton.setOnAction(e -> addData(data, formatter.format(datePicker.getValue()), valuePicker.getValue()));
HBox controls = new HBox(5, datePicker, valuePicker, addButton);
controls.setAlignment(Pos.CENTER);
controls.setPadding(new Insets(5));
BorderPane root = new BorderPane(chart, null, null, controls, null);
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
private void addData(ObservableList<Data<String, Number>> data, String formattedDate, double value) {
Data<String, Number> dataAtDate = data.stream()
.filter(d -> d.getXValue().equals(formattedDate))
.findAny()
.orElseGet(() -> {
Data<String, Number> newData = new Data<String, Number>(formattedDate, 0.0);
data.add(newData);
return newData ;
}) ;
dataAtDate.setYValue(dataAtDate.getYValue().doubleValue() + value);
}
public static void main(String[] args) {
launch(args);
}
}
如果要使用不具有良好顺序保留属性的格式,则只需要在比较器中将字符串解析回日期,然后比较日期即可.例如.如果你有类似的东西
formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) ;
它会与
SortedList<Data<String, Number>> sortedData = new SortedList<>(data, (data1, data2) -> {
LocalDate date1 = LocalDate.parse(data1.getXValue(), formatter);
LocalDate date2 = LocalDate.parse(data2.getXValue(), formatter);
return date1.compareTo(date2);
});
做到这一点的“正确”方法是使数据类型成为LocalDate,即您将具有LineChart< LocalDate,Number> ;:然而,定义一个Axis< LocalDate>即可.似乎比应做的工作还多.