Regarding bar chart/pie chart display using JFreechart and mysql database in eclipse

梦想与她 提交于 2019-12-13 03:43:57

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/9431266/regarding-bar-chart-pie-chart-display-using-jfreechart-and-mysql-database-in-ecl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!