gpt4 book ai didi

java - 使用 JFreeChart 创建包含数据库值的条形图

转载 作者:行者123 更新时间:2023-11-29 05:07:23 25 4
gpt4 key购买 nike

我正在从我的数据库中获取值,该数据库有两列。我想通过单击按钮创建条形图并在面板中显示条形图。我如何使用 JFreeChart 做到这一点?我正在使用 mysql 数据库。

最佳答案

我解决了我的问题。只需要阅读一些关于 JFreechart 的内容。这是解决方案:

    String toc = "";
int summary = 0;
try {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Class.forName(JDBC_DRIVER);

con = DriverManager.getConnection(DB_URL, USER, PASS);

String query = "Select toc as TypeOfCall,Sum(toc) as SummaryOfCalls from processeddata_table group by toc";

ps = con.prepareStatement(query);
rs = ps.executeQuery();
while (rs.next()) {
toc = rs.getString("TypeOfCall");
summary = rs.getInt("SummaryOfCalls");
dataset.setValue(summary, toc, toc);
}
JFreeChart chart = ChartFactory.createBarChart("Call cost", "TypeOfCall", "SummaryOfCalls", dataset, PlotOrientation.VERTICAL, false, true, false);
CategoryPlot p = chart.getCategoryPlot();

//p.setRangeGridlinePaint(Color.BLUE);

ChartPanel panel = new ChartPanel(chart);
panel.setVisible(true);
panel.setSize(200, 200);
display_graph.add(panel);

} catch (ClassNotFoundException | SQLException e) {

}

关于java - 使用 JFreeChart 创建包含数据库值的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895005/

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