gpt4 book ai didi

java - 如何使用 API 将数据插入条形图

转载 作者:行者123 更新时间:2023-12-02 06:11:13 24 4
gpt4 key购买 nike

这个问题与软件设计更相关。我想创建可用于更新此条形图的 API。这是原始示例 http://docs.oracle.com/javafx/2/charts/bar-chart.htm

我测试过创建这个:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class MainApp extends Application
{


XYChart.Series series1 = new XYChart.Series();
XYChart.Series series2 = new XYChart.Series();
XYChart.Series series3 = new XYChart.Series();

@Override
public void start(Stage stage)
{

setUserAgentStylesheet(STYLESHEET_CASPIAN);

stage.setTitle("Bar Chart Sample");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
bc.setTitle("Country Summary");
xAxis.setLabel("Country");
yAxis.setLabel("Value");

series1.setName("2003");
// series1.getData().add(new XYChart.Data(austria, 25601.34));
// series1.getData().add(new XYChart.Data(brazil, 20148.82));
// series1.getData().add(new XYChart.Data(france, 10000));
// series1.getData().add(new XYChart.Data(italy, 35407.15));
// series1.getData().add(new XYChart.Data(usa, 12000));

series2.setName("2004");
// series2.getData().add(new XYChart.Data(austria, 57401.85));
// series2.getData().add(new XYChart.Data(brazil, 41941.19));
// series2.getData().add(new XYChart.Data(france, 45263.37));
// series2.getData().add(new XYChart.Data(italy, 117320.16));
// series2.getData().add(new XYChart.Data(usa, 14845.27));

series3.setName("2005");
// series3.getData().add(new XYChart.Data(austria, 45000.65));
// series3.getData().add(new XYChart.Data(brazil, 44835.76));
// series3.getData().add(new XYChart.Data(france, 18722.18));
// series3.getData().add(new XYChart.Data(italy, 17557.31));
// series3.getData().add(new XYChart.Data(usa, 92633.68));

AddBarChartDataSeriesOne("Europe", 23);
AddBarChartDataSeriesTwo("Asia", 33);
AddBarChartDataSeriesThree("America", 44);

Scene scene = new Scene(bc, 800, 600);
bc.getData().addAll(series1, series2, series3);
stage.setScene(scene);
stage.show();
}

public void AddBarChartDataSeriesOne(String Name, float value)
{
series1.getData().add(new XYChart.Data(Name, value));
}

public void AddBarChartDataSeriesTwo(String Name, float value)
{
series2.getData().add(new XYChart.Data(Name, value));
}

public void AddBarChartDataSeriesThree(String Name, float value)
{
series3.getData().add(new XYChart.Data(Name, value));
}

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

如您所见,当我使用此 API 时,我无法正确设置一系列数据,也无法设置条形图标签。你能帮忙改进一下吗?

最佳答案

我不确定我是否理解你的意思,但这是我尝试的一种方法:

package javafxstackoverflow;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class JavaFXStackOverflow extends Application {

XYChart.Series series1 = new XYChart.Series();
XYChart.Series series2 = new XYChart.Series();
XYChart.Series series3 = new XYChart.Series();

@Override
public void start(Stage stage)
{
stage.setTitle("Bar Chart Sample");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
bc.setTitle("Country Summary");
xAxis.setLabel("Country");
yAxis.setLabel("Value");

series1.setName("2003");
series2.setName("2004");
series3.setName("2005");

AddBarChartDataSeries(series1, "Europe", 23);
AddBarChartDataSeries(series1, "Asia", 45);
AddBarChartDataSeries(series1, "America", 56);

AddBarChartDataSeries(series2, "Europe", 33);
AddBarChartDataSeries(series2, "Asia", 28);
AddBarChartDataSeries(series2, "America", 49);

AddBarChartDataSeries(series3, "Europe", 53);
AddBarChartDataSeries(series3, "Asia", 43);
AddBarChartDataSeries(series3, "America", 59);

Scene scene = new Scene(bc, 800, 600);
bc.getData().addAll(series1, series2, series3);
stage.setScene(scene);
stage.show();
}

public void AddBarChartDataSeries(XYChart.Series serie, String name, float value)
{
serie.getData().add(new XYChart.Data(name, value));
}

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

}

我刚刚将系列作为参数添加到您的函数中。如果我理解错了,请更详细地解释您的问题。

关于java - 如何使用 API 将数据插入条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21866569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com