我使用 SceneBuilder 创建了一个 AreaChart
,X 轴上带有 NumberAxis
和 Y 轴上的 NumberAxis
。
@FXML
private AreaChart<Number,Number> areaChart;
和 FXML:
<AreaChart fx:id="areaChart" alternativeColumnFillVisible="true" createSymbols="false" style="-fx-horizontal-grid-lines-color: white;" title="Altura durante la sesión">
<xAxis>
<NumberAxis fx:id="areaX" animated="false" label="Distancia (metros)" style="axis_color: white;" />
</xAxis>
<yAxis>
<NumberAxis fx:id="areaY" label="Altura (metros)" style="axis_color: white;" />
</yAxis>
我想通过按下按钮将该areaChart转换为在Y轴上带有NumberAxis
和在X轴上带有CategoryAxis
的AreaChart
。
因为我必须制作高度 x 距离图表,然后通过按按钮将该图表转换为高度 x 时间图表
按下按钮后的结果必须是这样的:
<AreaChart fx:id="areaChart" alternativeColumnFillVisible="true" createSymbols="false" style="-fx-horizontal-grid-lines-color: white;" title="Altura durante la sesión">
<xAxis>
<CategoryAxis fx:id="areaX" animated="false" label="Distancia (metros)" style="axis_color: white;" />
</xAxis>
<yAxis>
<NumberAxis fx:id="areaY" label="Altura (metros)" style="axis_color: white;" />
</yAxis>
最佳答案
我认为尝试更改图表的轴类型不是一个好主意(我什至不确定您是否可以这样做)。
相反,我建议:
- 创建两个图表。
- 其中一个使用 CategoryAxis,另一个使用 NumberAxis。
- 向两个图表添加相似的数据系列(但可能不同)。
- 将两个图表放入 StackPane 中。
- 添加 ToggleButton , ToggleSwitch , CheckBox或RadioButton (某种切换控制),在两种状态之间切换。
- 将一个图表的可见性绑定(bind)到切换的选定状态,并将另一图表的可见性绑定(bind)到非切换的选定状态。
这应该允许您根据需要显示具有适当轴类型的适当图表。
关于Javafx图表将NumberAxis更改为CategoryAxis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394198/