jsp页面 jfreechart调用数据库中的数据并以柱状图的形式显现出来
<%
CourseDAO dao = new CourseDAO();//dao类
ArrayList<Courseinfo> list = (ArrayList<Courseinfo>) dao.allCourses();//这个方法是返回所有的课程
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < list.size(); i++) { //遍历所有课程,把内容依次添加到dataset里
dataset.addValue(list.get(i).getCoursenum(), "totalnum", list.get(i).getCoursename());
}
JFreeChart freechart = ChartFactory.createBarChart3D("选课信息统计图",
"课程名称", "选课人数", dataset, PlotOrientation.VERTICAL, true,true, true);
freechart.setBackgroundPaint(Color.LIGHT_GRAY);
freechart.getTitle().setPaint(Color.BLUE);
freechart.getLegend().setVisible(true);
Font font = new Font("SimSun", 10, 20);
TextTitle txtTitle = null;
txtTitle = freechart.getTitle();
txtTitle.setFont(font);
CategoryPlot plot = freechart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
//设置距离图片左端距离
domainAxis.setLowerMargin(0.02);
//设置距离图片右端距离
domainAxis.setUpperMargin(0.02);
domainAxis.setLabelFont(new Font("黑体", Font.PLAIN, 14));
//水平底部标题
domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 14));
//垂直标题
ValueAxis rangeAxis = plot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 14));
plot.setDomainAxis(domainAxis);
//2
rangeAxis.setUpperMargin(0.08);
//设置最低的一个柱与图片底端的距离
rangeAxis.setLowerMargin(0.24);
plot.setRangeAxis(rangeAxis);
//3
BarRenderer3D bar = new BarRenderer3D();
bar.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色
bar.setWallPaint(Color.darkGray);
//设置每种代表的柱的颜色
bar.setSeriesPaint(0, new Color(255, 0, 0));
bar.setSeriesPaint(1, new Color(0, 255, 0));
bar.setSeriesPaint(2, Color.BLUE);
//设置每种代表的柱的 Outline 颜色
bar.setSeriesOutlinePaint(0, Color.BLACK);
bar.setSeriesOutlinePaint(1, Color.BLACK);
bar.setSeriesOutlinePaint(2, Color.BLACK);
//设置平行柱的之间距离
bar.setItemMargin(0.02);
//显示每个柱的数值,并修改该数值的字体属性
bar.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
bar.setDrawBarOutline(true);
bar.setIncludeBaseInRange(true);
bar.setItemLabelFont(new Font("华文新魏", Font.PLAIN, 8));
bar.setItemLabelsVisible(true);
plot.setRenderer(bar);
BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
renderer.setWallPaint(Color.PINK);
plot.setForegroundAlpha(1.00f);
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F,
Color.blue, 0.0F, 0.0F, new Color(0, 0, 64)); //设定特定颜色
GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F,
Color.cyan, 0.0F, 0.0F, new Color(0, 64, 0));
renderer.setSeriesPaint(0, gradientpaint);
renderer.setSeriesPaint(1, gradientpaint1);
//设置柱的 Outline 颜色
renderer.setSeriesOutlinePaint(0, Color.BLACK);
renderer.setSeriesOutlinePaint(1, Color.BLACK);
//设置每个category所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
//显示每个柱的数值,并修改该数值的字体属性
renderer
.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
String filename = ServletUtilities.saveChartAsPNG(freechart, 600,
400, null, session);
String grapUrl = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
%>
<center>
<img src="<%=grapUrl%>" width="700" height="500" border="0"
usemap="#<%=filename%>">
</center>
2016-01-24 · 做真实的自己 用良心做教育
jfreechart中显示柱状图到jsp页面:
package com.test.jfreechart;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
public class JFreeChartTest2 extends ApplicationFrame
{
public JFreeChartTest2(String title)
{
super(title);
this.setContentPane(createPanel()); //构造函数中自动创建Java的panel面板
}
public static CategoryDataset createDataset() //创建柱状图数据集
{
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(10,"a","管理人员");
dataset.setValue(20,"b","市场人员");
dataset.setValue(40,"c","开发人员");
dataset.setValue(15,"d","其他人员");
return dataset;
}
public static JFreeChart createChart(CategoryDataset dataset) //用数据集创建一个图表
{
JFreeChart chart=ChartFactory.createBarChart("hi", "人员分布",
"人员数量", dataset, PlotOrientation.VERTICAL, true, true, false); //创建一个JFreeChart
chart.setTitle(new TextTitle("某公司组织结构图",new Font("宋体",Font.BOLD+Font.ITALIC,20)));//可以重新设置标题,替换“hi”标题
CategoryPlot plot=(CategoryPlot)chart.getPlot();//获得图标中间部分,即plot
CategoryAxis categoryAxis=plot.getDomainAxis();//获得横坐标
categoryAxis.setLabelFont(new Font("微软雅黑",Font.BOLD,12));//设置横坐标字体
return chart;
}
public static JPanel createPanel()
{
JFreeChart chart =createChart(createDataset());
return new ChartPanel(chart); //将chart对象放入Panel面板中去,ChartPanel类已继承Jpanel
}
public static void main(String[] args)
{
JFreeChartTest2 chart=new JFreeChartTest2("某公司组织结构图");
chart.pack();//以合适的大小显示
chart.setVisible(true);
}
}
运行结果: