<!-- 加载highcharts -->
<script type="text/javascript" src="/static/js/highcharts/highcharts.js"></script>
<script type="text/javascript" src="/static/js/highcharts/exporting.js"></script>

<div id="charts-div" >

</div>

<script type="text/javascript">
function hightcharts(container) {
	container = container?container:document.createElement('div')
	var _this = this
	this.hightcharts_obj = null
        this.opt = {
        			chart: {//配置chart选项
                            renderTo: container, //容器名，和body部分的div id要一致
			                plotBackgroundColor: null,
			                plotBorderWidth: null,
			                plotShadow: false

                    },
	                title: {//配置标题
						text:'',
						align:'center',
						verticalAlign:'',
						margin:0,
						floating:0,
						style:{
							color: 'rgb(84, 126, 168)',
							fontSize: '20px'
						}
	                },
	                subtitle: {//配置副标题
	                    text: '',
	                    //y:30
	                },
	                credits: {
            	 		text: '',
            	 		href:""
            		},
			        tooltip: {
			    	    pointFormat: '{point.percentage:.1f}%({point.y}) ',
			        	percentageDecimals: 1
			        },
			        plotOptions: {
			            pie: {
			                allowPointSelect: true,
			                cursor: 'pointer',
			                dataLabels: {
			                    enabled: true,
			                    color: '#000000',
			                    connectorColor: '#000000',
			                    format: '{point.name} | {point.percentage:.1f}%({point.y}) '
			                },
			                showInLegend: true
			            }
			        },
	                series:[],
                   }
	this._getAvg = function(list){
		var num = 0
		for(i in list){num += list[i]}
		return 	(num / list.length).toFixed(1)
	}
	this.push_data = function(list_data,name,value_index){//增加数据
		value_index = value_index ? value_index : 1
		var data_list = []
		var xAxis = [] //横轴项目，即第一列
		var lines = list_data
		data_list = [{
				"type":"pie",
				'name':name,
				'data':[]
		}]


		for (var i = 0;i<lines.length;i++){
			var items = lines[i]
			var first_item = items[0]
			items[0] = items[0].toString()
			console.dir(first_item)
			if(!first_item){continue}
			first_item = String(first_item)
			if (first_item.indexOf('合计')>=0 || first_item.indexOf('总计')>=0 || first_item.indexOf('总共')>=0  || !first_item) { //不需要的
				continue
			}
			items[1] = parseFloat(items[value_index])
			data_list[0]['data'].push(items)
		}
		// for(var i in  data_list ){
		// 	var o = data_list[i]
		// 	o['name'] += ' (Min:'+ Math.min.apply(Math,o['data'])
		// 	o['name'] += ' Max:'+ Math.max.apply(Math,o['data'])
		// 	o['name'] += ' Avg:'+ _this._getAvg(o['data'])+')'
		// }
		console.dir(data_list)
		_this.opt.series = data_list //增加序列数据
		return [xAxis,data_list]
	}

	this.done = function(){
		Highcharts.setOptions({
								global:{
				        				useUTC: 0 
				        		},
				        		lang:{
				        			resetZoom:"重置",
				        		}
        })
		_this.hightcharts_obj = new Highcharts.Chart(_this.opt)

		return container
	}
}

/*
var h = new hightcharts()
h.opt.title.text='我爱物联网 2012/09/22 访问统计'
h.opt.subtitle.text='数据来源：百度统计'
h.opt.xAxis.categories = [ '1', '2', '3', '4', '5','6', '7', '8', '9', '10', '11','12','13','14','15','16','17','18','19','20','21','22','23']
h.opt.series.push({name: '浏览次数（PV）',data: [16,10, 8, 0, 0, 0, 2, 1, 5,10,25,34,8,12,38,22,13,14,11, 8, 8, 5, 8,33]})
h.opt.series.push({name: 'IP数',data: [ 7, 2, 1, 0, 0, 0, 1, 1, 3, 5, 4, 2,5, 5, 2, 1, 5, 3, 1, 4, 4, 4, 5, 9]})
h.done()
console.dir(h.hightcharts_obj)	
$("#container").append(h.done())	
//document.body.appendChild(h.done())
*/
//---------------------------------

</script>



<script>
if (window.dataTableOption) {
	var LIST_DATA = null
	var $charts_cont = $('#charts-div')
	$charts_cont.insertBefore($QUERY_TABLE)

	dataTableOption.page_size=100
	dataTableOption.page_size_list=[100,200]

	dataTableOption.convertInputDataFuncs.push(function(data){return data})//输入处理
	dataTableOption.convertOutputDataFuncs.push(function(data) {
		LIST_DATA = data.list_data
		var h = new hightcharts('charts-div')
		h.opt.title.text = data.query_name
		h.push_data(data.list_data,data.fields)

		h.done()
	  	return data
	}) 
}
</script>





