📄 graphaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.segsec.chart.struts.action;
import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
public class GraphAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//drawChartPicture dcp = new drawChartPicture();
//GraphLogic gl = new GraphLogic();
//gl.setChartInfo(request, response);
//String img = dcp.getDrawChartInfo(request, response);
HttpSession session = request.getSession();
double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
{ 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
String[] rowKeys = { "苹果", "梨子", "葡萄" };
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计", null, null,
dataset, PlotOrientation.VERTICAL, true, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
//domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色<BR>
renderer.setWallPaint(Color.gray);
//设置每种水果代表的柱的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
renderer.setSeriesPaint(1, new Color(0, 100, 255));
renderer.setSeriesPaint(2, Color.GREEN);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
//显示每个柱的数值,并修改该数值的字体属性<BR>
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);
//设置柱的透明度<BR>
plot.setForegroundAlpha(0.6f);
//设置地区、销量的显示位置<BR>
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
OutputStream out = new BufferedOutputStream(new FileOutputStream("F:\\项目状态分布1.jpg"));
try {
ChartUtilities.writeChartAsPNG(out, chart, 640, 480, null);
}
finally {
out.close();
}
FileOutputStream fos_jpg = null;
try {
System.out.println("开始生成图表!");
fos_jpg = new FileOutputStream("F:\\项目状态分布2.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f, chart, 640, 480, null);
//ChartUtilities.writeChartAsJPEG(fos_jpg,chart, 640, 480, null);
System.out.println("图表生成完毕!");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename="+ filename;
System.out.println("graphURL:"+graphURL);
System.out.println("filename.length():"+filename.length());
if (filename.length() != 0) {
System.out.println("graphname:"+filename);
request.setAttribute("graphname", filename);
System.out.println("graphURL:"+graphURL);
request.setAttribute("graphURL", graphURL);
return mapping.findForward("success");
} else {
return mapping.findForward("fail");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -