我正在尝试在JavaFX选项卡中添加JFreeChart的ChartViewer对象,但相应的图表并没有占据整个选项卡空间,尽管我已经使用了具有适当设置的AnchorPanes。我错过了什么或者我的方法是错的吗?
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.fx.ChartViewer;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
public class VolumeBarChartJava extends Application {
private TimeSeriesCollection createDataset() {
TimeSeries ts = new TimeSeries("Data");
ts.add(new Day(1,1,2017), 100.0);
ts.add(new Day(2,1,2017), 135.0);
ts.add(new Day(3,1,2017), 140.0);
ts.add(new Day(4,1,2017), 109.0);
TimeSeriesCollection tsc = new TimeSeriesCollection();
tsc.addSeries(ts);
return tsc;
}
@Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
Tab tab = new Tab("Chart");
tabPane.getTabs().add(tab);
AnchorPane anchorPane = new AnchorPane();
tab.setContent(anchorPane);
VBox vBox = new VBox();
anchorPane.getChildren().add(vBox);
AnchorPane.setBottomAnchor(vBox, 0.0);
AnchorPane.setLeftAnchor(vBox, 0.0);
AnchorPane.setRightAnchor(vBox, 0.0);
AnchorPane.setTopAnchor(vBox, 0.0);
vBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("BLUE"), CornerRadii.EMPTY, Insets.EMPTY)));
AnchorPane anchorPane1 = new AnchorPane();
vBox.getChildren().add(anchorPane1);
TimeSeriesCollection tsc = createDataset();
NumberAxis rangeAxis = new NumberAxis("Price");
XYBarRenderer renderer = new XYBarRenderer();
renderer.setShadowVisible(false);
renderer.setMargin(.5);
XYPlot plot = new XYPlot(tsc, null, rangeAxis, renderer);
DateAxis domainAxis = new DateAxis("Date");
CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(domainAxis);
combinedPlot.add(plot, 4);
JFreeChart chart = new JFreeChart("Volume Bar Chart", null, combinedPlot, false);
ChartViewer chartViewer = new ChartViewer(chart);
anchorPane1.getChildren().add(chartViewer);
AnchorPane.setBottomAnchor(chartViewer, 0.0);
AnchorPane.setLeftAnchor(chartViewer, 0.0);
AnchorPane.setRightAnchor(chartViewer, 0.0);
AnchorPane.setTopAnchor(chartViewer, 0.0);
chartViewer.setBackground(new Background(new BackgroundFill(Paint.valueOf("RED"), CornerRadii.EMPTY, Insets.EMPTY)));
Scene scene = new Scene(tabPane);
primaryStage.setTitle("Volume Bar Chart");
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}