Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Charts cannot be seen if pane created in FXML #4

Open
SergeyPenza opened this issue Nov 26, 2017 · 6 comments
Open

Charts cannot be seen if pane created in FXML #4

SergeyPenza opened this issue Nov 26, 2017 · 6 comments

Comments

@SergeyPenza
Copy link

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
Copy link
Owner

jfree commented Nov 29, 2017

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

@jfree
Copy link
Owner

jfree commented Dec 30, 2017

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

@jamesmoney
Copy link

@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
Copy link

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
Copy link
Owner

jfree commented Jun 8, 2018

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
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants