public class WriteImage {
public String writeBarImage(HttpSession session, PrintWriter pw) {
//默认种类的数据源
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
//生成自己的数据信息
StudentInfoManager studentInfoManager = new StudentInfoManager();
List result = studentInfoManager.getStudentInfo();
for(int i = 0, n = result.size(); i < n; i++) {
StudentInfo studentInfo = (StudentInfo) result.get(i);
//参数(1.显示数据,2.数据标识名,3.数据持有对象)
defaultcategorydataset.addValue(studentInfo.getStudentPlanAch(),"计划得分", studentInfo.getStudentName());
defaultcategorydataset.addValue(studentInfo.getStudentAchievement(),"实际得分", studentInfo.getStudentName());
}
JFreeChart jfreechart = ChartFactory.createBarChart("JFreeChart测试", "中信学生得分", "得分", defaultcategorydataset, PlotOrientation.VERTICAL, true, true, false);
//设置当前jfreechart图片的背景颜色
jfreechart.setBackgroundPaint(Color.white);
//获取当前jfreechart的点线种类
CategoryPlot categoryplot = jfreechart.getCategoryPlot();
//设置棒状图区域的背景颜色为灰色
categoryplot.setBackgroundPaint(Color.BLUE);
//设置棒状图区域的主格子线条为白色
categoryplot.setDomainGridlinePaint(Color.white);
//将主格子线条设置为可视
categoryplot.setDomainGridlinesVisible(true);
//设置棒状图区域行列的格子线条的颜色
categoryplot.setRangeGridlinePaint(Color.red);
categoryplot.setRangeGridlinesVisible(true);
//设置数字轴,获取当前数字轴的,也就是纵向的轴
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
//设置当前数字轴的单位
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//设置棒状图的样式设置
BarRenderer barrenderer = (BarRenderer)categoryplot.getRenderer();
barrenderer.setDrawBarOutline(false);
//渐变颜色
GradientPaint gradientpaint = new GradientPaint(0.0F, 8.0F, Color.LIGHT_GRAY, 1.0F, 1.0F, new Color(0, 0, 64));
GradientPaint gradientpaint1 = new GradientPaint(1.0F, 9.0F, Color.green, 1.0F, 1.0F, new Color(0, 64, 0));
barrenderer.setSeriesPaint(0, gradientpaint);
barrenderer.setSeriesPaint(1, gradientpaint1);
//添加鼠标提示信息
barrenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
//获取横向的轴也就是主轴
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.52359877559829882D));
//设置图片的绘制信息
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String filename = null;
try {
filename = ServletUtilities.saveChartAsPNG(jfreechart, 500, 300, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info, false);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
}
Jfree实现柱状图1
最新推荐文章于 2022-11-06 09:51:56 发布