I looked at the JavaFX API and got my two graphs I want working. Though I would like them both besides each other, since I'm new I have no idea how to do this. If someone could help it would be great! Here's my code:
我查看了JavaFX API,得到了我想要工作的两张图。虽然我希望他们俩彼此相爱,但是因为我是新手,所以我不知道该怎么做。如果有人可以帮助它会很棒!这是我的代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.*;
import javafx.scene.Group;
public class Main extends Application {
final static String housing = "Avg Housing Prices By Year";
final static String commercial = "AvgCommercialPrices By Year";
private static double[] avgHousingPricesByYear = {
247381.0, 264171.4, 287715.3, 294736.1, 308431.4, 322635.9, 340253.0, 363153.7
};
private static double[] avgCommercialPricesByYear = {
1121585.3, 1219479.5, 1246354.2, 1295364.8, 1335932.6, 1472362.0, 1583521.9, 1613246.3
};
private static String[] ageGroups = {
"18-25", "26-35", "36-45", "46-55", "56-65", "65+"
};
private static int[] purchasesByAgeGroup = {
648, 1021, 2453, 3173, 1868, 2247
};
@Override public void start(Stage stage) {
stage.setTitle("Lab06");
stage.setWidth(1000);
stage.setHeight(450);
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String,Number> bc =
new BarChart<String,Number>(xAxis,yAxis);
bc.setTitle("Lab 06");
xAxis.setLabel("Housing");
yAxis.setLabel("Value");
XYChart.Series series1 = new XYChart.Series();
series1.setName(housing);
for(int i = 0 ; i < 8; i++){
series1.getData().add(new XYChart.Data( Integer.toString(2000 + i), avgHousingPricesByYear[i]));
}
XYChart.Series series2 = new XYChart.Series();
series2.setName(commercial);
for(int i = 0 ; i < 8; i++){
series2.getData().add(new XYChart.Data(Integer.toString(2000 + i), avgCommercialPricesByYear[i]));
}
Scene scene = new Scene(new Group());
bc.getData().addAll(series1, series2);
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList();
for(int i = 0; i < 6; i++){
pieChartData.add(new PieChart.Data(ageGroups[i], purchasesByAgeGroup[i]));
}
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Purchases Example");
((Group) scene.getRoot()).getChildren().add(bc);
((Group) scene.getRoot()).getChildren().add(chart);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
1 个解决方案
#1
1
If you need them beside each other, just put them in a HBox
:
如果你需要它们彼此相邻,只需将它们放在HBox中:
@Override public void start(Stage stage) {
// existing code, but remove this:
// Scene scene = new Scene(new Group());
// and replace this
// ((Group) scene.getRoot()).getChildren().add(bc);
// ((Group) scene.getRoot()).getChildren().add(chart);
// with this:
HBox root = new HBox();
root.getChildren().add(bc);
root.getChildren().add(chart);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
A complete description of layout panes, such as HBox
, is in the tutorial.
本教程中包含布局窗格的完整描述,例如HBox。
#1
1
If you need them beside each other, just put them in a HBox
:
如果你需要它们彼此相邻,只需将它们放在HBox中:
@Override public void start(Stage stage) {
// existing code, but remove this:
// Scene scene = new Scene(new Group());
// and replace this
// ((Group) scene.getRoot()).getChildren().add(bc);
// ((Group) scene.getRoot()).getChildren().add(chart);
// with this:
HBox root = new HBox();
root.getChildren().add(bc);
root.getChildren().add(chart);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
A complete description of layout panes, such as HBox
, is in the tutorial.
本教程中包含布局窗格的完整描述,例如HBox。