I just wonder that i wanna make program that show real-time data on DateAxis using javafx chart.
now, i made realtime chart but it doesn't match with DateAxis. I know there is CategoryAxis and NumberAxis and when i searched in Stackoverflow, i need to use NumberAxis for setTickLabelFormatter.
Now, i got date (string type) from database and change it to long type. but i don't know how to insert these data to LineChart xAxis. at first, i need to sorting these data by date and insert to xAxis.
In my code, there are many Errors. I need to the way for solving problem not for errors. I have no idea for resolving this. Please help me.
This is code for data:
while(searchCursor.hasNext())
{
Document element = searchCursor.next();
String datetime = element.getString("Date");
Double value = Double.parseDouble(element.getString("Value"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = format.parse(datetime);
longdate = date.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Datalist.add(new GraphData(longdate, value));
Collections.sort(Datalist, new Comparator<GraphData>() {
@Override
public int compare(GraphData date1, GraphData date2)
{
return date1.Date.compareTo(date2.Date);
}
});
dateAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(dateAxis)
{
@Override
public String toString(Number object)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(object.longValue()), ZoneId.systemDefault());
String formattedDateTime = dateTime.format(formatter);
return formattedDateTime;
}
});
series[i].getData().add(new XYChart.Data<Number, Number>((Datalist.get(eachdatacnt).Date), Datalist.get(eachdatacnt).Value));
eachdatacnt++;
}
series[i].setName(SelectedEndpointNameList.get(i).toString());
lineChart.setAnimated(false);
lineChart.getData().add(series[i]);
lineChart.setAxisSortingPolicy(LineChart.SortingPolicy.X_AXIS);
}