jfree / jfreechart-fx

Extensions for JFreeChart to support JavaFX client applications.
http://www.jfree.org/jfreechart/
GNU Lesser General Public License v2.1
109 stars 20 forks source link

Charts cannot be seen if pane created in FXML #4

Open SergeyPenza opened 6 years ago

SergeyPenza commented 6 years ago

The crosshair overlay example was modified to appear in a pane defined in the FXML file. in the controller declared:

// some code here// @FXML AnchorPane pane; // Blah-blah some code...// @Override public void initialize(URL url, ResourceBundle rb) { freeCharts charts = new freeCharts(); XYDataset dataset = charts.createDataset(); JFreeChart chart = charts.createChart(dataset); ChartViewer viewer = new ChartViewer(chart); pane.getChildren().add(viewer);

Does not produce a visible chart.

jfree commented 6 years ago

I'm wondering if this is related to bug #3. I fixed that one, but still have to release a new version.

jfree commented 6 years ago

New version is out. I didn't check if it fixes this bug yet (no time), if anyone else does let me know :)

jamesmoney commented 6 years ago

@jfree This is still a problem. Nothing appears and no errors populate with the example similar to above:

@FXML private Pane canvaspane;

....

``

    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries data = new XYSeries("Test");
    data.add(1.0, 50);
    data.add(2.0, 100);
    dataset.addSeries(data);
    JFreeChart chart = ChartFactory.createXYLineChart("Test", "Time", "Value", dataset);
    ChartViewer viewer = new ChartViewer(chart);
    canvaspane.getChildren().add(viewer);

``

and the FXML looks like ``

              <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
                 <children>
                      <Pane fx:id="canvaspane" prefHeight="400.0" prefWidth="460.0" />\                          
                 </children>
              </VBox>

``

If I modify the example slightly to ``

    canvas = viewer.getCanvas();
    canvas.heightProperty().bind(canvaspane.heightProperty());
    canvas.widthProperty().bind(canvaspane.widthProperty());

``

This throws the error

Caused by: java.lang.RuntimeException: ChartCanvas.width : A bound value cannot be set. at javafx.beans.property.DoublePropertyBase.set(DoublePropertyBase.java:143) at javafx.scene.canvas.Canvas.setWidth(Canvas.java:150) at org.jfree.chart.fx.ChartViewer.layoutChildren(ChartViewer.java:194) at javafx.scene.Parent.layout(Parent.java:1087) at javafx.scene.Parent.layout(Parent.java:1093) at javafx.scene.Parent.layout(Parent.java:1093) at javafx.scene.Parent.layout(Parent.java:1093) at javafx.scene.Parent.layout(Parent.java:1093) at javafx.scene.Parent.layout(Parent.java:1093) at javafx.scene.Scene.doLayoutPass(Scene.java:552) at javafx.scene.Scene.preferredSize(Scene.java:1646) at javafx.scene.Scene.impl_preferredSize(Scene.java:1720) at javafx.stage.Window$9.invalidated(Window.java:864) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144) at javafx.stage.Window.setShowing(Window.java:940) at javafx.stage.Window.show(Window.java:955) at javafx.stage.Stage.show(Stage.java:259) at MainApp.start(MainApp.java:415) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

jamesmoney commented 6 years ago

Also, as a workaround I have used this code

``

    canvas = viewer.getCanvas();
    canvas.heightProperty().bind(canvaspane.heightProperty());
    canvas.widthProperty().bind(canvaspane.widthProperty());
    canvaspane.getChildren().add(canvas);

``

and this renders. However, various other items do not function including tooltips.

jfree commented 6 years ago

I think the issue is just that the ChartViewer doesn't have the prefWidth and prefHeight set. You can either do this in the initialize() method in the controller:

        ChartViewer cv = new ChartViewer(chart);
        cv.setPrefWidth(300);
        cv.setPrefHeight(200);

...or you could add the ChartViewer to your FXML and then just set the chart in the initialise method (because the default constructor for ChartViewer sets the chart to null).

<ChartViewer fx:id="chartViewer" prefWidth="300" prefHeight="200">
    </ChartViewer>
JanPastorek commented 3 years ago

still does not work, any ideas how to get it done?