本文共 2961 字,大约阅读时间需要 9 分钟。
org.jfree.chart.JFreeChart,
org.jfree.chart.plot.PlotOrientation,
org.jfree.chart.servlet.ServletUtilities,
org.jfree.data.category.CategoryDataset,
org.jfree.data.general.DatasetUtilities,
org.jfree.chart.plot.*,
org.jfree.chart.labels.*,
org.jfree.chart.renderer.category.BarRenderer3D,
java.awt.*, org.jfree.ui.*,
org.jfree.chart.axis.AxisLocation"%>
//连接数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jfree";
String usr="sa";
String pwd="";
Connection conn=DriverManager.getConnection(url,usr,pwd);
Statement stmt=conn.createStatement();
String sql="select * from jfree order by id";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
int a = rs.getInt("a");
int b = rs.getInt("b");
int c = rs.getInt("c");
int d = rs.getInt("d");
String e = rs.getString("e");
String f = rs.getString("f");
double[][] data = new double[][] {
{a, b, c, d}, {a, b, c, d}, {a, b, c, d}, {a, b, c, d}};String[] rowKeys = {f, f,f, f};
String[] columnKeys = {e, e, e, e};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("城市选择", "城市", "选择", dataset, PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white); //设置网格背景颜色
plot.setDomainGridlinePaint(Color.pink);//设置网格竖线颜色
plot.setRangeGridlinePaint(Color.pink); //设置网格横线颜色
BarRenderer3D renderer = new BarRenderer3D(); //显示每个柱的数值,并修改该数值的字体属性
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //默认的数字显示在柱子中,通过如下两句可调整数字的显示
renderer.setBaseItemLabelsVisible(true); //注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D); //设置每个地区所包含的平行柱的之间距离 //
renderer.setItemMargin(0.3); plot.setRenderer(renderer); //设置地区、销量的显示位置 //将下方的“肉类”放到上方
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); //将默认放在左边的“销量”放到右方
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
String filename = ServletUtilities.saveChartAsPNG(chart, 700, 400, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
少个}不知道改插哪或者请高手帮我改下代码~谢谢啊
加了这个出现
java.lang.IllegalArgumentException: Duplicate items in 'rowKeys'.
org.jfree.data.general.DatasetUtilities.createCategoryDataset(DatasetUtilities.java:412)
org.apache.jsp.sample10_jsp._jspService(sample10_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
展开
转载地址:http://uvdko.baihongyu.com/