简体   繁体   中英

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. 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). 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:

//...

<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. It support json format which can simplify your problem.

I figured it out already. I had the fxml loaded into my program twice, by mistake. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM