实习笔记之Spring MVC + ajax + highcharts

mentor要求我在页面上显示一个折线图出来,花了两天多时间终于做好了,贴部分代码出来,希望能帮到大家。

下面是controller里面的方法,返回一个json数据。

@RequestMapping(value="chart/ajax/{name}", method = RequestMethod.GET)
public @ResponseBody String showChart(@PathVariable String name, Model model){
return jsonObject.toString();
}

下面是jsp里面的代码

<div class="chart">
	<div id = "${ rName }" name="container" style="min-width: 400px; height: 200px; margin: 0 auto">
		<script type="text/javascript">
			function getForm(){
				var name = "${ rName }";
				$.ajax({
					type:"GET",
					url:"chart/ajax/"+name,
					dataType:"JSON",
					success:function(data){
						getChart(data,name);
						}				
				});
			}
			getForm();

		</script>	
	</div>
</div>
下面是js里面的代码

function getChart(data,name){

	eval( "data=" + data );
	var keys = [];
	var values = [];
	for(var key in data){
		keys.push(key);
		values.push(data[key]);
	}
	keys = keys.reverse();
	values = values.reverse();
	 var chart = new Highcharts.Chart({
          chart: {
              renderTo: name,
              type: 'spline'
          },
          credits: {
              enabled : false,                   
          },
          title: {
              text: 'trend chart',
              y:10
          },
        
          xAxis: {
              categories: keys
          },
          yAxis: {
              title: {
                  text: 'Total Count'
              },
              labels: {
                  formatter: function() {
                      return this.value +''
                  }
              }
          },
          tooltip: {
              crosshairs: true,
              shared: true
          },
          plotOptions: {
              spline: {
                  marker: {
                      radius: 4,
                      lineColor: '#666666',
                      lineWidth: 1
                  }
              }
          },
          series: [{
              name: 'count',
              marker: {
                  symbol: 'square'
              },
              data: values}]
      });
 }


就这么多,希望能对大家有所帮助




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值