scroll - JavaFX horizontal list of charts -
i'm developing software comparison of different amounts of data , want use charts display them in horizontal way. important me display 3 charts on 1 side. if there more 3 user shall scroll other charts.
it won't pic, i'm lazy css. idea put charts in gridpane inside scrollpane. bind charts width 1/3 of tabpane width.
i used fxml since it's easier make 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); (int = 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); } }
Comments
Post a Comment