ChartViewer inside a Container

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ericode
Posts: 4
Joined: Sat Jan 13, 2018 10:04 pm
antibot: No, of course not.

ChartViewer inside a Container

Post by Ericode » Sat Jan 13, 2018 10:07 pm

Hi, is it possible to put the chartviewer inside a Pane or GridPane? Most of the examples have it inside the scene and I need to put it in a container.

I am trying to have it display in a GridPane and it will not display unless it is the actual scene. I have 3 cases, 2 of them don't work and one does work but the example that works can't be put inside another hbox or vbox or else it stops working.

Code: Select all

//This Doesn't work
ChartViewer chartViewer = new ChartViewer(chart);
  GridPane.setConstraints(chartViewer, 0, 1);
  gridPane.getChildren().add(chartViewer);
  Scene primaryScene = new Scene(gridPane);

// This also doesn't work
VBox vbox = new VBox();
        HBox parent = new HBox();
        Node container = useWorkaround(chartViewer);
        HBox.setHgrow(container, Priority.ALWAYS);
        parent.getChildren().add(container);
        vbox.getChildren().add(parent);
        Scene primaryScene = new Scene(vbox);

// This works
       HBox parent = new HBox();
        Node container = useWorkaround(chartViewer);
        HBox.setHgrow(container, Priority.ALWAYS);
        parent.getChildren().add(container);
        Scene primaryScene = new Scene(parent);

// Method found in #6 used to test
private Node useWorkaround(ChartViewer viewer) {
        if (true) {
            return new StackPane(viewer);
        }
        return viewer;
    }

// This does not work
ChartCanvas chartCanvas = new ChartCanvas(chart);
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(chartCanvas);
        Scene primaryScene = new Scene(stackPane);

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: ChartViewer inside a Container

Post by John Matthews » Sun Jan 14, 2018 7:27 pm

When using GridPane, don't neglect the constraints. Starting from this example, the fragment below uses the pane's convenience methods to add four chart viewers containing the same chart:

Code: Select all

PieDataset dataset = createDataset();
JFreeChart chart = createChart(dataset); 
GridPane p = new GridPane();
p.add(new ChartViewer(chart), 0, 0);
p.add(new ChartViewer(chart), 1, 0);
p.add(new ChartViewer(chart), 0, 1);
p.add(new ChartViewer(chart), 1, 1);
stage.setScene(new Scene(p)); 
stage.setTitle("JFreeChart: PieChartFX"); 
stage.setWidth(640);
stage.setHeight(480);
stage.show(); 
Image

Spin the mouse wheel to see the effect.

Locked