使用SoapUI测试webservice接口详细步骤
321
2022-11-01
JFreeChart 案例1
JFreeChart 是一个非常流行的一个免费开源的图表软件 已经有11年以上的历史
首先要下载jar包 jfree.org
做JFreeChart的时候,一般是以下三步 1:要有数据 比如DefaultPieDataset 2:根据数据生成 JFreeChart 对象 3:显示JFreeChart对象(显示在swing中或生成一个图片在jsp中显示)
下面看一个例子:
主要是用JFreeChart做的饼图和柱状图,用到了struts2
1.Action类(一个处理部门的饼图,一个地市柱形图):
public class DeptPieChartAction extends ActionSupport { /** * Comment for serialVersionUID
*/ private static final long serialVersionUID = 1L; private JFreeChart chart; private Date startTime; private String startTimeStr; @Override public String execute() throws Exception { if(startTime != null){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); startTimeStr = sdf.format(startTime); startTimeStr = startTimeStr + "-01 00:00:00"; } TJByChart tc = new TJByChart(); chart = tc.createDeptPieChart(startTimeStr); return SUCCESS; } public JFreeChart getChart() { return chart; } public void setChart(JFreeChart chart) { this.chart = chart; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public String getStartTimeStr() { return startTimeStr; } public void setStartTimeStr(String startTimeStr) { this.startTimeStr = startTimeStr; } }
public class RNumBarChartAction extends ActionSupport { /** * Comment for serialVersionUID
*/ private static final long serialVersionUID = 1L; private JFreeChart chart; @Override public String execute() throws Exception { TJByChart tc = new TJByChart(); chart = tc.createReceiveCaseBarChart(); return SUCCESS; } public JFreeChart getChart() { return chart; } public void setChart(JFreeChart chart) { this.chart = chart; } }
2.service 类:
import java.awt.Font;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import org.apache.struts2.ServletActionContext;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer3D;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.general.PieDataset;import org.jfree.data.jdbc.JDBCPieDataset;import com.metarnet.cssf.common.PropertiesParser;import com.metarnet.cssf.common.UserManager;import com.metarnet.cssf.common.db.ConnectionPool;import com.metarnet.cssf.model.User;/* * 图形统计表-统计概览 * */public class TJByChart { private User user = UserManager.getInstance().getUserBySessionId(ServletActionContext.getRequest().getRequestedSessionId()); //接单/驳单统计 public JFreeChart createReceiveCaseBarChart(){ CategoryDataset dataset = initReceiveCaseBarDate(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); String name = cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月"; JFreeChart chart = ChartFactory.createBarChart3D( name + "处理/驳回统计", // 图表标题 "地市", "数量", dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 true // 是否生成URL链接 ); chart.getTitle().setFont(new Font("隶书",Font.BOLD,18)); chart.getLegend().setItemFont(new Font("宋体",Font.BOLD, 14)); chart.setBorderVisible(true); CategoryPlot plot = chart.getCategoryPlot(); plot.setForegroundAlpha(0.8F); plot.setBackgroundAlpha(0.5F); CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); categoryAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12)); categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); ValueAxis valueAxis = plot.getRangeAxis(); valueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); BarRenderer3D renderer = new BarRenderer3D(); renderer.setItemMargin(0.32); plot.setRenderer(renderer); return chart; } private CategoryDataset initReceiveCaseBarDate(){ DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); Connection conn = null; Statement stat = null; ResultSet rs = null; try { String sql = "select city_id,city_name,isreject,count(*) as cnum from cssf_ts_statistics where 1 = 1 "; if(user.getCityId() > 0){ //省份 sql = sql + " and city_id = " + user.getCityId(); } //时间 Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String start = sdf.format(cal.getTime()); cal.add(Calendar.MONTH, 1); String end = sdf.format(cal.getTime()); sql = sql + " and sponsor_time >= to_date('" + start + "','yyyy-mm-dd hh24:mi:ss') and sponsor_time < to_date('" + end + "','yyyy-mm-dd hh24:mi:ss') group by city_id,city_name,isreject order by city_id"; conn = ConnectionPool.getInstance().getConnection("cssf"); stat = conn.createStatement(); rs = stat.executeQuery(sql); Map
3.struts.xml
4.jsp
5.需要的JAR包
jcommon-1.0.17.jar,jfreechart-1.0.14.jar;这两个是必须的,当用到struts2时,还需用到struts2-jfreechart-plugin-2.3.14.jar
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~