简体   繁体   English

为什么我不能从控制器中的 .fxml 修改图表?

[英]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.

相关问题 注入到我的控制器类的JavaFx @FXML分页无法修改 - JavaFx @FXML Pagination injected to my controller class can't be modified 无法执行控制器类中的按钮样式方法(FXML按钮)。 如何设置FXML按钮的样式而不为此使用CSS? - Can't execute button style methods (FXML button) from controller class. How can I setstyles of a FXML button and not using CSS for this? 混合Swing / FX:无法从fxml控制器处理对话框 - Mixing Swing/FX: can't dispose a dialog from fxml controller 为什么我看不到我的项目中的更改,但我在 fxml 文件中看到了它们? - Why can't I see the changes in my project but I see them in the fxml file? FXML ListView,我无法添加我的ObservableArrayList <Pane> 它 - FXML ListView, I can't add my ObservableArrayList<Pane> to it 为什么我的fxml控制器类不移动我的按钮? - Why doesn't my fxml controller class move my button? 为什么不能在控制器中的新线程中使用@In(使用jboss seam)注入依赖项? - Why can't I inject dependencies with @In (using jboss seam) in a new thread from within my controller? 无法使用控制器类的构造函数加载fxml阶段 - fxml stage can't be load with a controller-class constructor 无法重定向到其他 FXML - Can't redirect to an other FXML 无法通过Webstart加载FXML - Can't load FXML with webstart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM