Reputation: 1990
I would like to extend JavaFX LineChart
to add some functionalities (give access to the legend so it can be customized, adding visible bounds, etc.). I've created an EnhancedLineChart
class an declared an instance on my FXML file but it returns an exception:
Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "xAxis" does not exist or is read-only.
Here is my class:
package com.ratp.oam.widgets.graph;
import com.sun.javafx.charts.Legend;
import javafx.collections.ObservableList;
import javafx.scene.chart.Axis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
public class EnhancedLineChart extends LineChart<Number, Number> {
public EnhancedLineChart() {
super(new NumberAxis(), new NumberAxis());
}
public EnhancedLineChart(final NumberAxis xAxis, final NumberAxis yAxis) {
super(xAxis, yAxis);
}
public EnhancedLineChart(final NumberAxis xAxis, final NumberAxis yAxis,
ObservableList<Series<Number, Number>> data) {
super(xAxis, yAxis, data);
}
@Override
public Axis<Number> getXAxis() {
return super.getXAxis();
}
@Override
public Axis<Number> getYAxis() {
return super.getYAxis();
}
/**
* Fournit la légende du graphique telle qu'elle a été implémentée dans la
* classe {@link LineChart}.
*
* @return
*/
public Legend legend() {
return (Legend) super.getLegend();
}
}
and the FXML file where my component is instantiated (DataGraph.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import com.ratp.oam.widgets.graph.EnhancedLineChart?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml/1" spacing="3">
<ScrollPane fitToWidth="true" HBox.hgrow="ALWAYS" styleClass="scroll-pane">
<fx:define>
<EnhancedLineChart fx:id="analogicalChart" createSymbols="false" animated="false" prefHeight="350">
<padding>
<Insets topRightBottomLeft="0" />
</padding>
<xAxis>
<NumberAxis fx:id="analogicalXAxis" animated="false" side="BOTTOM" forceZeroInRange="false" autoRanging="false" />
</xAxis>
<yAxis>
<NumberAxis animated="false" side="LEFT" forceZeroInRange="false" autoRanging="false" />
</yAxis>
</EnhancedLineChart>
</fx:define>
<VBox fx:id="graphBox">
<VBox fx:id="logicalPane" />
</VBox>
</ScrollPane>
</fx:root>
How could I solve my problem?
Nota Bene
Upvotes: 0
Views: 230
Reputation: 1990
Finally I've found the solution by looking at the LineChart
class. If I want to use xAxis
, yAxis
and data
in my FXML file, I need to place the @NamedArg
annotation before constructor arguments, so the instances can be called by the FXML loader. The result looks like this:
package com.ratp.oam.widgets.graph;
import com.sun.javafx.charts.Legend;
import javafx.beans.NamedArg;
import javafx.collections.ObservableList;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
public class EnhancedLineChart extends LineChart<Number, Number> {
public EnhancedLineChart(@NamedArg("xAxis") NumberAxis xAxis, @NamedArg("yAxis") NumberAxis yAxis) {
super(xAxis, yAxis);
}
public EnhancedLineChart(@NamedArg("xAxis") NumberAxis xAxis, @NamedArg("yAxis") NumberAxis yAxis, @NamedArg("data") ObservableList<Series<Number, Number>> data) {
super(xAxis, yAxis, data);
}
/**
* Fournit la légende du graphique telle qu'elle a été implémentée dans la
* classe {@link LineChart}.
*/
public Legend legend() {
return (Legend) super.getLegend();
}
}
Upvotes: 2