尝试构建包含可更新的应用程序LineChart
或AreaChart
我发现奇怪的行为时 - 可能是由于应用程序逻辑中的错误?
目标是在单击“生成”按钮时将数据填充到图表中或更新图表。用户必须输入图表的开始时间和结束时间,此外还必须选择间隔小时/天/周(通过使用 a RadioGroup
)。
初始图表的创建没有任何问题。重新生成图表也可以正常工作,但前提是之前的图表中不存在数据点。如果两个图表(旧的和更新的)中都包含具有相同 x 值的数据点,则排序不再是升序。
例子:
执行开始日期:01.09.2013 结束日期:25.09.2013 间隔:周
x 轴上的值:
01.09.2013 / 08.09.2013 / 15.09.2013 / 22.09.2013
单击“天”RadioButton
重新生成图表会在 x 轴上产生以下值:
01.09.2013 / 08.09.2013 / 15.09.2013 / 22.09.2013 / 02.09.2013 / 03.09.2013 / 04.09.2013 / ...
(值应为 01.09.2013 / 02.09.2013 / 03.09.2013 / ...)
所有已经显示在第一个图表中并且也在第二个图表中的值,在新图表的开头排序(而不是按升序排列)
这是实现技巧的代码(方法代码initializeTimeline
仅用于测试目的(肯定有点可优化;))):
public class ChartDesignController implements Initializable {
@FXML
private AreaChart chartOne;
@FXML
private TextField startDate;
@FXML
private TextField endDate;
@FXML
private RadioButton radioHours;
@FXML
private RadioButton radioDays;
@FXML
private RadioButton radioWeeks;
@FXML
private ToggleGroup timeUnit;
@FXML
private Label msgBox;
@FXML
private void generateGraph(ActionEvent event) {
String timeUnit = getTimeUnit();
Series s = initializeTimeline(startDate.getText(), endDate.getText(), timeUnit);
chartOne.getData().setAll(s);
}
private String getTimeUnit() {
String timeUnitForQuery = "DD";
RadioButton selectedRadio = (RadioButton) timeUnit.getSelectedToggle();
if (selectedRadio == radioHours) {
//System.out.println("RadioHours was selected");
timeUnitForQuery = "HH24";
}
if (selectedRadio == radioDays) {
//System.out.println("RadioDays was selected");
timeUnitForQuery = "DD";
}
if (selectedRadio == radioWeeks) {
//System.out.println("RadioWeeks was selected");
timeUnitForQuery = "IW";
}
//System.out.println("Time unit changed");
return timeUnitForQuery;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
private Series initializeTimeline(String startTime, String endTime, String timeUnit) {
msgBox.setText("");
long delta;
int nrOfTicks = 0;
Data<String, Integer> dp;
Series s = new Series();
ArrayList<Data<String, Integer>> dataPoints = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date startDate = new Date();
Date endDate = new Date();
GregorianCalendar startTimeGC = new GregorianCalendar();
GregorianCalendar endTimeGC = new GregorianCalendar();
if (timeUnit.equalsIgnoreCase("HH24")) {
sdf = new SimpleDateFormat("dd.MM.yyyy HH");
}
try {
startDate = sdf.parse(startTime);
endDate = sdf.parse(endTime);
} catch (ParseException ex) {
msgBox.setText(ex.getMessage() + "\n" + "Format expected: " + sdf.toPattern());
}
startTimeGC.setTimeInMillis(startDate.getTime());
endTimeGC.setTimeInMillis(endDate.getTime());
delta = endTimeGC.getTimeInMillis() - startTimeGC.getTimeInMillis();
if (timeUnit.equalsIgnoreCase("HH24")) {
nrOfTicks = (int) (delta / 1000 / 60 / 60) + 1;
} else if (timeUnit.equalsIgnoreCase("DD")) {
nrOfTicks = (int) (delta / 1000 / 60 / 60 / 24) + 1;
} else if (timeUnit.equalsIgnoreCase("IW")) {
nrOfTicks = (int) (delta / 1000 / 60 / 60 / 24 / 7) + 1;
}
for (int i = 0; i < nrOfTicks; i++) {
dp = new Data(sdf.format(startTimeGC.getTime()), 0);
dataPoints.add(dp);
if (timeUnit.equalsIgnoreCase("HH24")) {
startTimeGC.add(GregorianCalendar.HOUR_OF_DAY, 1);
} else if (timeUnit.equalsIgnoreCase("DD")) {
startTimeGC.add(GregorianCalendar.DAY_OF_MONTH, 1);
} else if (timeUnit.equalsIgnoreCase("IW")) {
startTimeGC.add(GregorianCalendar.WEEK_OF_YEAR, 1);;
}
}
dataPoints.sort(new DataComparator());
s.setData(FXCollections.observableList(dataPoints));
return s;
}
}
下面,DataComparator
负责dataPoints
根据 x 值按字母顺序对它们进行排序:
public class DataComparator implements Comparator<Data> {
@Override
public int compare(Data d1, Data d2) {
String d1Xval = (String) d1.getXValue();
String d2Xval = (String) d2.getXValue();
return d1Xval.compareTo(d2Xval);
}
}
调试时,程序值在ArrayList
dataPoints
.
任何想法为什么值在图表 x 轴中排序不正确,以防它们已经出现在旧图表中?
非常感谢对这个“问题”的帮助。