Echarts 后台传入数据

jsp页面:


    
    

 Servlet:

package com.cdm.Hive;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import net.sf.json.JSONObject;
public class AcceptData  extends HttpServlet {
 JSONObject jsonObject=null;
     public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
		try{
		req.setCharacterEncoding("utf-8");
			resp.setContentType("utf-8");
			resp.setCharacterEncoding("utf-8");
			
			PrintWriter write = resp.getWriter();
			Map map = new HashMap(); 
			List xAxisData = new ArrayList();
				List< JSONObject> seriesList = new ArrayList< JSONObject>();
				
				for (int i = 1; i < 13; i++) {
				xAxisData.add(i+"月");
				}


				for (int i = 0; i < 3; i++) {
					List list = new ArrayList();
					for (int j = 1; j < 13; j++) {
						list.add((int)(Math.random()*40));
					}
					Series series = new Series("销售"+i, Series.TYPE_LINE, list);
                                         JSONObject jsonObject2 = new JSONObject();
                                         jsonObject2.put("name", series.toName());
                                         jsonObject2.put("type","bar");
                                          jsonObject2.put("data",series.data);
                                          seriesList.add(jsonObject2);
                                       }

  //xAxisData和seriesList转为json

JSONObject jsonObject1 = new JSONObject();
 
jsonObject1.put("xAxisData", xAxisData);
 
jsonObject1.put("seriesList", seriesList);
//发送给前台
write.write(jsonObject1.toString());
write.flush();
write.close();
}catch (IOException e) {
e.printStackTrace();
}

}
 
 public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException{
 
doGet(req,resp);
}
}

 Series:

import java.util.List;

import org.json.JSONObject;

public class Series {
	
	public String name;
	public String type;
	public List data;
	public static String TYPE_LINE = "line";
public static String TYPE_BAR = "bar";
public Series( String name, String type, List data) {  
    
    this.name = name;  
    this.type = type;  
    this.data = data;  
}  
public String toName(){
	return name;
}

}


结果:

Echarts 后台传入数据_第1张图片







你可能感兴趣的:(jsp,数据,可视化)