java 如何从数据库中查询并且处理数据,用折线图展示数据趋势变化?

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/86656452

需求如下

1、数据库中有一张表名为alarm_data的表,有time(时间),address(地址),shift(位移)三列

     这个表是用来存储当发生位移报警时,发生的时间、报警地址、报警位移的,不存储正常数据;

2、我需要根据地址、日期,来查询当天时间内位移变化趋势

3、要求折线图的形式展现

     折线图横坐标是时间0,1,2,3,4,5,6,7,8,9,10........23

    纵坐标是位移


程序步骤

1、根据时间和地址,按照时间升序,把查询的数据存储在集合里面

2、在存储数据时,要知道报警时间在0:00-1:00,1:00-2:00 ,2:00-3:00........哪个区间内,不然折线图的横坐标无法对应

3、假设查询地址0号,日期2019-01-26日的报警数据的时间区间是(3时7时,12时,21时);

      其他时间点就没有报警,位移就是正常的位移;

      假设正常位移是6.57mm,我们需要得到一个数组
 

时间 0 1 2 3 4 5 6 7 .........
位移 6.57 6.57 6.57 报警位移 6.57 6.57 6.57 报警位移 ..........

 5、折线图的横坐标对应的是时间,纵坐标是位移;只需要循环遍历一下数据就可以得到折线图


代码如下

1、查询数据库里面的数据的方法

package cn.com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import cn.com.pack.Alarm_Data;
import cn.com.pack.Six;
import cn.com.way.Blocks;
public class GetConnection {
	// 连接数据库,把数据存储到数据库中
	// 1.常量
	// 声明Connection对象
	static Connection con = null;
	// 驱动程序名
	static String driver = "com.mysql.jdbc.Driver";
	// URL指向要访问的数据库名mydata
	static String url = "jdbc:mysql://192.168.16.8:3306/lf";
	// MySQL配置时的用户名
	static String user = "root";
	// MySQL配置时的密码
	static String password = "root";
	//查询alarm_data表里的数据
	public static List<Six> selectinfo(String sql, String[] str) {
		ResultSet rs=null;
		PreparedStatement ps=null;
		List<Six> list=new ArrayList<Six>();
		Six six=null;
		// 加载驱动程序
		try {
			Class.forName(driver);
			// 1.getConnection()方法,连接MySQL数据库!!
			con = DriverManager.getConnection(url, user, password);
			ps = con.prepareStatement(sql);
			for (int i = 0; i < str.length; i++) {
				ps.setString(i+1, str[i]);
			}
			rs=ps.executeQuery();
			while(rs.next()){
				//由于报警的时间不是连续的,所以需要判断
				String time=rs.getString(1);
				//根据时间获取想要的int类型的值9:00-10:00之间的就返回9
				int t=Blocks.DayTime(time);
				String shift=rs.getString(3);
				//把t和shift放在一起
				six=new Six(shift, t);
				list.add(six);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		return list;

	}

}

   判断时间在哪个时间区间内

package cn.com.way;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Blocks {
	//给定日期和一个时间,就可以知道这个时间的区间范围
		public static int DayTime(String armtime) throws ParseException{
			//根据具体的时间来获取想要的日期
			String rq=Blocks.debu(armtime);
			int y=0;
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long arm0=sim.parse(armtime).getTime();
				for(int i=0;i<24;i++){
					String begin=rq+" ";
					String end =rq+" ";
					String a=Integer.toString(i);
					String b=Integer.toString(i+1);
					if(a.length()==1){
						a="0"+a;
					}
					if(b.length()==1){
						b="0"+b;
					}
					begin=begin+a+":00:00";
					  end=end+b+":00:00";
					  //把开始区间时间和结束区间时间转换为long类型。然后比较
					  long arm1=sim.parse(begin).getTime();
					  long arm2=sim.parse(end).getTime();
					  if(arm0>arm1&&arm0<arm2){
						  y=i;
						 System.out.println(armtime+"在区间"+begin+"-"+end+"之间"); 
						 break;
					  } 
				}
				return y;     }
		//把字符串根据空格进行切割,来获取想要的日期
		public static String debu(String rq){			
					String [] str=rq.split(" ");
					return str[0];
			}
}

 2、查询数据,处理日期和时间

package cn.com.chart;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import cn.com.jdbc.GetConnection;
import cn.com.pack.Six;
import cn.com.way.InsertNum;
public class Displacement {
	/*
	 * author:命运的信徒 date:2019/1/24 arm:根据一天的24个点来查询位移变化量
	 */
	static int [] stri=new int[24];
	static double [] doub=new double[24];
	public static void selectPlace(String address, String rq) {
		// 1.根据地址、当天的日期、和标准量,来计算偏移量
		int add = Integer.parseInt(address);
		Date date = new Date();
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		String dates = sim.format(date);
		// 2.查询数据库里当天的所有位移量
		String sql = "select * from alarm_data where address=? and time like ? order by time asc";
        rq=rq+" %";
		String [] str=new String[]{address,rq};
        List<Six> list=GetConnection.selectinfo(sql, str);
        //开始排序,填充了
        int m=0;
        for (Six six : list) {
        	//string类型变成double类型
        	double dou=Double.parseDouble(six.getOne());
        	m=InsertNum.fill(m+1,six.getTwo(),dou,stri,doub);
		}
        
	}

}

 //给出一组任意长度的数据(0<=数据的值<=23)9,11,12,15,21
//得到一个固定长度为23的一组数据,数据所对应的位置正是值+1;如9应该放在第10位,0应该放在第一位

package cn.com.way;
public class InsertNum {
//给出一组任意长度的数据(0<=数据的值<=23)9,11,12,15,21
//得到一个固定长度为23的一组数据,数据所对应的位置正是值+1;如9应该放在第10位,0应该放在第一位
public static int fill(int a,int b,double c,int [] stri,double [] doub){
	for (int i = a; i < 24; i++) {
		if(b==i){
			stri[i]=b;
			doub[i]=c-6.5746;
			System.out.println("-时间段-"+b+"-位移-"+c);
			break;
		}else{
			stri[i]=0;
			doub[i]=0;
		}
	}
	return b;
}


}

封装类

package cn.com.pack;

public class Six {
	private String one;
	private int two;
	private String three;
	private int four;
	private String five;
	private int six;
	public String getOne() {
		return one;
	}
	public void setOne(String one) {
		this.one = one;
	}
	public int getTwo() {
		return two;
	}
	public void setTwo(int two) {
		this.two = two;
	}
	public String getThree() {
		return three;
	}
	public void setThree(String three) {
		this.three = three;
	}
	public int getFour() {
		return four;
	}
	public void setFour(int four) {
		this.four = four;
	}
	public String getFive() {
		return five;
	}
	public void setFive(String five) {
		this.five = five;
	}
	public int getSix() {
		return six;
	}
	public void setSix(int six) {
		this.six = six;
	}
	public Six(String one, int two) {
		super();
		this.one = one;
		this.two = two;
	}

}

生成折线图代码

package cn.com.chart;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class TimeChart {
	public static void main(String[] args) {
		CategoryDataset mDataset = GetDataset();
		JFreeChart mChart = ChartFactory.createLineChart("折线图",// 图名字
				"时",// 横坐标
				"位移",// 纵坐标
				mDataset,// 数据集
				PlotOrientation.VERTICAL, true, // 显示图例
				true, // 采用标准生成器
				false);// 是否生成超链接

		CategoryPlot mPlot = (CategoryPlot) mChart.getPlot();
		mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
		mPlot.setRangeGridlinePaint(Color.BLUE);// 背景底部横虚线
		mPlot.setOutlinePaint(Color.black);// 边界线

		ChartFrame mChartFrame = new ChartFrame("折线图", mChart);
		mChartFrame.pack();
		mChartFrame.setVisible(true);

	}

	public static CategoryDataset GetDataset() {
		DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
		//1.执行方法
		Displacement.selectPlace("0", "2019-01-25");
		//获取数组
		int [] str=Displacement.stri;
		double [] dou=Displacement.doub;
		for (int i = 0; i < dou.length; i++) {
			mDataset.addValue(dou[i],"地址0",i+"h");
		}
		
		return mDataset;
	}
}

 折线图如下

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/86656452