[英]Why can't I can't modify a Chart from the .fxml in my Controller?
I defined a BarChart in my .fxml file that i inject into my controller using the @FXML annotation.我在我的 .fxml 文件中定义了一个 BarChart,我使用 @FXML 注释将它注入到我的控制器中。 But when I insert a data series, the chart still shows up as empty in my application, even though debugger output confirms that I successfully insert the data series into my chart.
但是当我插入一个数据系列时,图表在我的应用程序中仍然显示为空,即使调试器输出确认我成功地将数据系列插入到我的图表中。 Do I need to tell the chart's view to update somehow?
我需要告诉图表的视图以某种方式更新吗?
I could surely just work around the problem by getting a pane from the fxml into which I would then insert the chart (that does work, and I used this workaround in the past).我当然可以通过从 fxml 获取一个窗格来解决这个问题,然后我将在其中插入图表(这确实有效,我过去使用过这种解决方法)。 But I'm curious about what I'm doing wrong.
但我很好奇我做错了什么。
Thanks for your help!谢谢你的帮助!
Controller code:控制器代码:
@FXML private BarChart<Number,String> barChart;
//...
barChart.getData().add( new XYChart.Series<Number,String>("blabla",
eventService
.getTopTen(EventType.MUSICAL)
.entrySet()
.stream()
.map(entry -> new XYChart.Data<>((Number)entry.getValue(),entry.getKey().getName(),entry))
.collect(Collectors.collectingAndThen(toList(), FXCollections::observableArrayList))
));
My FXML:我的 FXML:
//...
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="at.ac.tuwien.inso.sepm.ticketline.client.gui.event.EventTopTenController">
<children>
<Button layoutX="267.0" layoutY="361.0" mnemonicParsing="false" onAction="#MusicalTopTenButtonHandler" text="Literature" />
<BarChart fx:id="barChart" layoutX="52.0" layoutY="14.0" prefHeight="338.0" prefWidth="510.0">
<xAxis>
<NumberAxis side="BOTTOM" />
</xAxis>
<yAxis>
<CategoryAxis side="LEFT" />
</yAxis>
</BarChart>
</children>
You can use highcharts library instead of this.您可以使用 highcharts 库代替这个。 It support json format which can simplify your problem.
它支持 json 格式,可以简化您的问题。
I figured it out already.我已经想通了。 I had the fxml loaded into my program twice, by mistake.
我错误地将 fxml 加载到我的程序中两次。 After calling the method in the controller, one of those Nodes was updated, the other one wasn't.
调用控制器中的方法后,其中一个节点被更新,另一个没有。 I was just looking at the one that wasn't.
我只是在看那个不是的。 Thanks anyways!
不管怎么说,多谢拉!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.