我正在尝试遵循以下指南: https://docs.oracle.com/javafx/2/charts/scatter-chart.htm Guide的示例工作正常,但是当尝试将其与场景构建器和FXML结合使用时,会出现一些奇怪的错误。首先,默认使用X轴作为类别轴,并且必须在XML中手动更改它。现在,代码段如下所示:
<ScatterChart fx:id="modScatChart" layoutX="303.0" layoutY="122.0" title="Modify timeline" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<xAxis>
<NumberAxis fx:id="scatXAxis" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="scatYAxis" side="LEFT" />
</yAxis>
</ScatterChart>
X轴必须为NumberAxis,因为X值将是标准化的浮点日期值,例如2018年第180天。 2018,5。现在,当尝试将最小,最大,刻度值提供给NumberAxis时,似乎一切都被忽略了。看起来总是这样: enter image description here 这是相关的代码片段(GraphBean基本上提供了HashMap(毫秒/位置,其中ms将转换为日期),x轴的最小值和y轴的最大值。
public class FXMLDocumentController implements Initializable {
@FXML NumberAxis scatXAxis = new NumberAxis();
@FXML NumberAxis scatYAxis = new NumberAxis();
@FXML ScatterChart modScatChart = new ScatterChart(scatXAxis,scatYAxis);
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
setupGraphs();
} catch(SQLException e){
System.out.println(e);
} catch(Exception e){
System.out.println(e);
}
}
private void setupGraphs() throws SQLException {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(graphsBean.getMinModMs()));
int minYear = cal.get(Calendar.YEAR);
cal.setTime(new Date(System.currentTimeMillis()));
int thisYear = cal.get(Calendar.YEAR);
scatXAxis = new NumberAxis(minYear, thisYear, 1);
scatYAxis = new NumberAxis(0, graphsBean.getMaxLoc(), Math.round(graphsBean.getMaxLoc()/10));
scatXAxis.setLabel("Time");
scatYAxis.setLabel("Application size in loc");
Series series1 = new XYChart.Series();
series1.setName("All");
HashMap<Long, Integer> scatMap = graphsBean.getScatMap();
for (Map.Entry<Long, Integer> entry : scatMap.entrySet()) {
cal.setTime(new Date(entry.getKey()));
int curYear = cal.get(Calendar.YEAR);
float yearDayFlt = curYear+(float)(cal.get(Calendar.DAY_OF_YEAR))/365;
series1.getData().add(new XYChart.Data(yearDayFlt, entry.getValue()));
}
modScatChart.getData().addAll(series1);
}
}
在类开始时还尝试使用手动数据完全初始化NumberAxis,但结果仍然相同。我在这里想念什么?