1 数据库sql查询的数据
2 最终echarts画出来的图
3 代码如下
计算主方法方法
/** * echarts算时间轴代码(算流量的代码),这里的时间间隔为5秒钟,一分钟的流量,totalSize = 12 * ebuStreamList 为EbuStream的集合, EbuStream的属性有totalStream,time * totalSize为x轴有多少点 * startTime 为起始时间 * */ private String[] getEbuStream(List<EbuStream> ebuStreamList,int totalSize,String startTime) throws ParseException{ String[] totalStreamArray = new String[totalSize]; String currentTime = startTime; //起始时间变量 int pointer = 0; //时间指针 int counter = 0 ;//计数器 for(int i = 0 ; i < totalSize ; i ++){ // 遍历开始,为每个点赋值 String nextTime = ChartsTimeUtil.getNextTime(currentTime, 1); //下一次时间 //校验时间 if(ebuStreamList!=null&&ebuStreamList.size()>pointer) { String cpuTime = ebuStreamList.get(pointer).getTime();//得到当前流量实体的时间 /** * 假设时间为 * currentTime = 2016-05-07 22:10:46 * cpuTime = 2016-05-07 22:10:48 * nextTime = 2016-05-07 22:10:51 * 则cpuTime在currentTime和nextTime之间 * 执行if( ChartsTimeUtil.isBigger(cpuTime, currentTime) && ChartsTimeUtil.isBigger(nextTime, cpuTime) )里面的代码 * * */ if( ChartsTimeUtil.isBigger(cpuTime, currentTime) && ChartsTimeUtil.isBigger(nextTime, cpuTime) ){ totalStreamArray[i] = ""+ebuStreamList.get(pointer).getTotalStream()+""; pointer++; counter++; } /** * 假设时间为 * cpuTime = 2016-05-07 22:10:45 * currentTime = 2016-05-07 22:10:46 * nextTime = 2016-05-07 22:10:51 * 则cpuTime在currentTime和nextTime的左侧 * 执行else if(ChartsTimeUtil.isBigger(currentTime, cpuTime))里面的代码 * * */ else if(ChartsTimeUtil.isBigger(currentTime, cpuTime)){ pointer++; nextTime = currentTime; i--; } /** * 假设时间为 * currentTime = 2016-05-07 22:10:46 * nextTime = 2016-05-07 22:10:51 * cpuTime = 2016-05-07 22:10:53 * 则cpuTime在currentTime和nextTime的右侧 * 执行下面else里面的代码 * * */ else{ totalStreamArray[i] = ""; counter++; } } /** * 当ebuStreamList为空的时候执行下面else代码 * */ else{ totalStreamArray[i] = ""; counter++; } currentTime = nextTime; // 当前时间往下移一个点 } return totalStreamArray; }
/** * 得到下一个点的时间 * private static Calendar calendar = Calendar.getInstance(); * private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); * ChartsSnmpTimerTask.timeFrequency=1000 * 5; // 时间间隔5秒钟 * * **/ public static String getNextTime(String currentTime ,int next) throws ParseException{ if(currentTime==null||currentTime.replace(" ", "").equals("")) return null; calendar.setTime(simpleDateFormat.parse(currentTime)); calendar.set(Calendar.SECOND , calendar.get(Calendar.SECOND) + ( ChartsSnmpTimerTask.timeFrequency / 1000 ) * next ); return simpleDateFormat.format(calendar.getTime()); }
/** * 比较两个日期之间的大小 * * @param d1 * @param d2 * @return 前者大于后者返回true 反之false * @throws ParseException */ public static boolean isBigger( String time1, String time2) throws ParseException { int result = time1.compareTo(time2); if(result>=0){ return true; }else{ return false; } }
package com.nufront.euht.web.ebu.model; import java.sql.Timestamp; /** * ebu流量实体 * */ public class EbuStream { private String id; private int upstream; private int downstream; private int totalStream; private String time; public String getId() { return id; } public void setId(String id) { this.id = id; } public int getUpstream() { return upstream; } public void setUpstream(int upstream) { this.upstream = upstream; } public int getDownstream() { return downstream; } public void setDownstream(int downstream) { this.downstream = downstream; } public int getTotalStream() { return totalStream; } public void setTotalStream(int totalStream) { this.totalStream = totalStream; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }