1. 程式人生 > >ajax動態賦值highcharts柱形圖

ajax動態賦值highcharts柱形圖

實現的效果為


1.jsp頁面

<!-- 引入highcharts官方js -->
<script src="js/highcharts.js"></script>
<script>
$(function(){

//獲得hcharts資料並賦值
$.ajax({
	    type:"post",
		url:rootPath+"/analyze/citySpeed.action",
		data : {"sendCityName":sendCityName,"receiverCityName":receiverCityName},
		dataType:"json",
		cache:false,
		async:false,
		success:function(res){
			console.log(res);
			  var speed=[];//平均速度
			  var time=[];//平均用時
			  var carrierName=[];//橫座標
			for(var i = 0; i<res.length;i++){
		    	time.push(parseFloat(res[i].useTime));
		    	carrierName.push(res[i].carrierName);
		    	speed.push(parseFloat(res[i].speed));
		    };
		    console.log("============speed===========");
		    console.log(speed);
		    console.log(carrierName);
		    console.log(time);
  	     $('#container').highcharts({
  	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           chart: {
  	            type: 'column'
  	        },
  	        title: {
  	            text: '快遞公司平均速度和平均用時'
  	        },
  	        subtitle: {
  	            text: '資料來源: wechat.com'
  	        },
  	        xAxis: {
  	            categories: carrierName,
  	            crosshair: true
  	        },
  	        yAxis: {
  	            min: 0,
  	            title: {
  	                text: '平均速度(km/h)'
  	            }
  	        },
  	        tooltip: {
  	            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
  	            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
  	            '<td style="padding:0"><b>{point.y:.1f}</b></td></tr>',
  	            footerFormat: '</table>',
  	            shared: true,
  	            useHTML: true
  	        },
  	        plotOptions: {
  	            column: {
  	                pointPadding: 0.2,
  	                borderWidth: 0
  	            }
  	        },
  	        series: [{
  	            name: '速度(km/h)',
  	            data: speed
  	        },{
  	            name: '用時(h)',
  	            data: time
  	        }/*,
  	       {
  	            name: '時間(h)',
  	            data: [90,80,70,60,50,40,90,80,70,60,50,40,50]
  	        }*/]
  	    });
		},error:function(){
			
		}
	});

})
</script>


2.後臺返回資料 返回map或者list都可以。