Echarts使用json异步通信

前端 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<script type="text/javascript" src="jquery-3.2.1.js"></script>
	<script type="text/javascript" src="echarts.js"></script>
  </head>
  
  <body>
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
    	var myChart = echarts.init(document.getElementById('main'));
    	$.getJSON("dataServlet").done(function (data) {
    	
    	 myChart.setOption({
        title: {
            text: '异步数据加载示例'
        },
        tooltip: {},
        legend: {
            data:['销量']
        },
        xAxis: {
            data: data.categories
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: data.data
        }]
        });
    });
    
    </script>
  </body>
</html>


后台

package Servlet;

import java.io.IOException;
import java.util.ArrayList;

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

import net.sf.json.JSONObject;

public class DataServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doGet(req, resp);
	
;
		ArrayList<String> categories = new ArrayList<String>();
		categories.add("s1");
		categories.add("s2");
		categories.add("s3");
		categories.add("s4");
		categories.add("s5");
		categories.add("s6");
		
		ArrayList<Integer> data = new ArrayList<Integer>();
		data.add(5);
		data.add(20);
		data.add(36);
		data.add(10);
		data.add(10);
		data.add(20);
	
		
		JSONObject jsonObj = new JSONObject();
	
		jsonObj.put("categories", categories);
		jsonObj.put("data", data);
		resp.getWriter().write(jsonObj.toString());
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值