关于在eclipse中使用JFreechart和mysql数据库显示条形图/饼图

发布于 2025-01-08 09:47:55 字数 1783 浏览 3 评论 0原文

我正在 Java EE 平台上开发一个简单的应用程序,使用 MYSQL 和 JFreeChart 数据库生成 3D 条形图,但 3D 效果可见,但在背景上看不到条形,因此我无法在条形图中显示条形图,同样是当我尝试用饼图来做到这一点时。

这是我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.jfree.data.jdbc.JDBCCategoryDataset" %>
<%@ page import="org.jfree.chart.plot.PlotOrientation" %>
<%@ page import="org.jfree.chart.JFreeChart" %>
<%@ page import="org.jfree.chart.ChartUtilities" %>
<%@ page import="org.jfree.chart.ChartFactory" %>
<%

String connectionURL = "jdbc:mysql://localhost/tester?user=root&password=root&useUnicode=true&characterEncoding=utf-8"; 
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection (connectionURL); 
String query = "SELECT * from charter";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(con);
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createBarChart3D("Test", "Name", "ID",dataset, PlotOrientation.VERTICAL, true, true, false);
try { 
    ChartUtilities.saveChartAsJPEG(new File("E:/project/jfreechart3D/img/barchart_3D.jpeg"),chart,400, 300); 
    } 
catch (IOException e) {
    System.out.println("No chart creation.");
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<IMG SRC="E:/project/jfreechart3D/img/barchart_3D.jpeg" WIDTH="600" HEIGHT="400" BORDER="0" USEMAP="#chart" />
</body>
</html>

I am developing a simple application on Java EE platform using database as MYSQL and JFreeChart to generate 3D Bar Charts but 3D effect is visible but the bars are not seen on that background,thus I am unable to display the bars in bar chart and same is with when I try to do this with Pie Charts.

Here is mine code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.jfree.data.jdbc.JDBCCategoryDataset" %>
<%@ page import="org.jfree.chart.plot.PlotOrientation" %>
<%@ page import="org.jfree.chart.JFreeChart" %>
<%@ page import="org.jfree.chart.ChartUtilities" %>
<%@ page import="org.jfree.chart.ChartFactory" %>
<%

String connectionURL = "jdbc:mysql://localhost/tester?user=root&password=root&useUnicode=true&characterEncoding=utf-8"; 
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection (connectionURL); 
String query = "SELECT * from charter";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(con);
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createBarChart3D("Test", "Name", "ID",dataset, PlotOrientation.VERTICAL, true, true, false);
try { 
    ChartUtilities.saveChartAsJPEG(new File("E:/project/jfreechart3D/img/barchart_3D.jpeg"),chart,400, 300); 
    } 
catch (IOException e) {
    System.out.println("No chart creation.");
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<IMG SRC="E:/project/jfreechart3D/img/barchart_3D.jpeg" WIDTH="600" HEIGHT="400" BORDER="0" USEMAP="#chart" />
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2025-01-15 09:47:55

相反,编写一个调用 ChartUtilities 方法,例如 writeChartAsJPEG()writeChartAsPNG()。前者会更压缩,但后者会更锐利。 此处有一个示例。让您的 JSP 包含一个标记,其中 src 引用您的 servlet。

Instead, write a servlet that invokes one of the ChartUtilities methods such as writeChartAsJPEG() or writeChartAsPNG(). The former will be more compressed, but the latter will be sharper. There's an example here. Have your JSP include a tag where the src refers to your servlet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文