JavaFX horizontal list of charts

2019-07-21 00:47发布

I'm developing a software for the comparison of different amounts of data and I want to use charts to display them in a horizontal way. Important for me is to display just 3 charts on one side. If there are more than 3 the user shall scroll to the other charts.

enter image description here

1条回答
虎瘦雄心在
2楼-- · 2019-07-21 01:27

It won't look exactly like that pic, I'm too lazy for css. The idea is to put all the charts in a GridPane inside a ScrollPane. Bind the charts so the width is exactly 1/3 of the TabPane width.

I used FXML since it's easier to make a scene.

FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<TabPane fx:id="tabPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="horizcharts.FXMLDocumentController">
  <tabs>
    <Tab text="Main View">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
               <children>
                  <Button layoutX="181.0" layoutY="112.0" mnemonicParsing="false" onAction="#addChart" text="Add Chart" />
               </children></AnchorPane>
      </content>
    </Tab>
    <Tab text="Compare">
         <content>
            <ScrollPane maxWidth="1.7976931348623157E308">
               <content>
                  <GridPane fx:id="grid" gridLinesVisible="true">
                  </GridPane>
               </content>
            </ScrollPane>
         </content>
    </Tab>
  </tabs>
</TabPane>

FXMLDocumentController.java

package horizcharts;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.Label;
import javafx.scene.control.TabPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;


public class FXMLDocumentController implements Initializable {

    @FXML GridPane grid;
    @FXML TabPane tabPane;
    private int numCharts = 0;

    @FXML private void addChart(ActionEvent event) {
        VBox vb = randChart(numCharts);
        GridPane.setConstraints(vb, numCharts++,0);
        grid.getChildren().add(vb);
    }

    private VBox randChart(int num){
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();
        BarChart<String, Number> bc = new BarChart(xAxis, yAxis);
        BarChart.Series<String, Number> series = new BarChart.Series<>();
        series.setName("Bar Chart "+num);
        bc.getData().add(series);
        for (int i = 0; i<5; i++){
            series.getData().add(new BarChart.Data("cat "+i, Math.random()*10*i));
        }
        bc.prefWidthProperty().bind(tabPane.widthProperty().subtract(6).divide(3));
        bc.prefHeightProperty().bind(tabPane.heightProperty().subtract(180));//guess heights
        VBox vb = new VBox(5,new Label("Version "+num), bc, new Label("precision"), new Label("recall"));
        return vb;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    

}

Regular main class HorizCharts.java.

package horizcharts;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class HorizCharts extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}

Screenshot

查看更多
登录 后发表回答