var endTime=getEndTime();
var startTime=getStartTime("年份", endTime);
var line_index = window.parent.document.getElementById("lineIndex").value;

$(document).ready(function () {
	i=ityzl_SHOW_LOAD_LAYER();  
	getEventDensity(line_index,startTime,endTime);
    getEventTime(line_index,startTime,endTime);
    getEventData(line_index,startTime,endTime);
    getEventChance(line_index,startTime,endTime);
    //***********关闭loading
	ityzl_CLOSE_LOAD_LAYER(i); 
})

//暂降事件点图
function initEvent(top,bot,pointI,pointIun) {
    var option = {
	        backgroundColor: canvasBG,//背景色,
	        title: {
	            text: '暂降事件点图',
	            x: 'center'
	        },	        color:[ITICTop,ITICBottom,green,red],
	        xAxis: [{
	            type: 'log',
	            min: '0.001',
	            max: '1000',
	            splitLine: {
	                show: false
	            },
	            name: 's'
	        }],
	        yAxis: [{
	            type: 'value',
	            splitNumber: 10,
	            minInterval: 3,
	            name: '%'
	        }],
	        series: [
	            {
	                name: '上限',
	                type: 'line',
	                data: top
	            },
	            {
	                name: '下限',
	                type: 'line',
	                data: bot
	            },
	            {
	                name: '可容忍事件',
	                type: 'scatter',
	                symbol : 'circle',
	                data: pointI
	            },
	            {
	                name: '不可容忍事件',
	                type: 'scatter',
	                symbol : 'circle',
	                data: pointIun
	            }
	        ],
			color:['#FF7E50','#00E3E3','#00DB00','red']
	    };
    
    var event = echarts.init(document.getElementById('event'));
    event.setOption(option);
}

//暂降密度图
function initDensity(sydy,cysj,datalist) {
    var myChart = echarts.init(document.getElementById('density'));

    option = {
    	backgroundColor: canvasBG,
        title: {
            text: '电压暂降表及密度图',
            x: 'center'

        },
        visualMap: {
        	max: 20,
            show: false,
            inRange: {
                color: ['#313695', '#00BB00', '#ff8000', '#a50026']
            }
        },
        xAxis3D: {
            type: 'category',
            name: '剩余电压(%)',
            data: sydy
        },
        yAxis3D: {
            type: 'category',
            name: '持续时间(cyc)',
            data: cysj
        },
        zAxis3D: {
        	type: 'value',
            splitNumber: 10,
            minInterval: 10,
            name: '次数'
        },
        grid3D: {
            boxWidth: 120,
            boxDepth: 80,
            light: {
                main: { 
                    intensity: 1.2
                },
                ambient: {
                    intensity: 0.8
                }
            }
        },
        series: [{
            type: 'bar3D',
            data: datalist.map(function (item) {
                return {
                    value: [item[0], item[1], item[2]]
                }
            }),
            shading: 'realistic',

            label: {
                show: false,
                textStyle: {
                    fontSize: 16,
                    borderWidth: 1
                }
            },

            itemStyle: {
                opacity: 1
            },

            emphasis: {
                label: {
                    textStyle: {
                        fontSize: 20,
                        color: '#900'
                    }
                },
                itemStyle: {
                    color: '#900'
                }
            }
        }]
    }

    myChart.setOption(option);
}

//暂降事件与时间关系图
function initTime(interval) {

	var months = new Array();
	var handle = new Array();
	if (interval != null) {
		for (var i = 0; i < interval.length; i++) {
			if (interval[i].month == "01" | i == 0) {
				months[i] = interval[i].month + "\n(" + interval[i].year
						+ ")";
			} else {
				months[i] = interval[i].month;
			}
			handle[i] = interval[i].times;
		}
	}
	var option = {
		backgroundColor : canvasBG,//背景色,
		title : {
			text : '暂降原因、类型和时间分布统计',
			x : 'center'
		},
		color : [orange],
		legend : {
			data : [ '暂降次数' ],
			x : 'left'
		},
		grid: {
	        left: '3%',
	        bottom: '3%',
	        containLabel: true
		},
		xAxis : [ {
			type : 'category',
			data : months,
			name : '月份',
			splitLine : {
				show : false
			},
			axisLabel : {
				show : true,
				interval : 0
			}
		} ],
		yAxis : [ {
			type : 'value',
			name : '次'
		} ],
		series : [ {
			name : '暂降次数',
			type : 'bar',
			barMaxWidth : barMax,
			itemStyle : {
				normal : {
					label : {
						show : true,
						textStyle : {
							label: {show: true, position: 'top'},
							color : purple
						}
					}
				}
			},
			data : handle
		} ]
	};
    var event = echarts.init(document.getElementById('time'));
    event.setOption(option);
}

function initChance(eventValue){
	var option = {
			backgroundColor: canvasBG,//背景色,
		    title : {
		        text: '暂降幅值的概率分布函数',
		        x : 'center'
		    },
		    legend: {
		    	show:false,
		        data:['暂降幅值']
		    },
		    legend: {
		    	show:true,
		    	left:10,
		        data:['概率分布']
		    },
		    calculable : true,
		    xAxis : [
		        {
		            type : 'category',
		            boundaryGap : false,
		            name : '暂降\n幅值\n(%)',
		            data : ['0','10','20','30','40','50','60','70','80','90']
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		            name : '(%)',
		            axisLabel : {
		                formatter: '{value} '
		            }
		        }
		    ],
		    series : [
		        {
		            name:'概率分布',
		            type:'line',
		            data:eventValue
		        }
		    ]
		};
	
    var chance = echarts.init(document.getElementById('chance'));
    chance.setOption(option);
}

function getEventDensity(line_index,startTime,endTime) {
	jQuery.ajax({
	type : 'POST',
	dataType : "json",
	data : {
		lineIndex:line_index,
		startTime:startTime,
		endTime:endTime
	},
	cache : false,
	url : '/pqs9200/device/eventDensity',
	error : function() {
		layer.msg('无暂降事件',{icon:2,time:1000});
	},
	success : function(data) {
		if(data == null){
			layer.msg('加载失败！',{icon:2,time:1000});
		}else{
			var datalist=[]
		    var sydy = ['0-10', '10-20', '20-30', '30-40', '40-50', '50-60', '60-70',
		        '70-80', '80-90', '90-100'];
		    var cysj = ['1cyc', '2cyc', '3cyc',
		        '4cyc', '5cyc', '6~10cyc','10~20cyc','20~30cyc','30~60cyc'];	
		    
		    for(var i=0;i<data.length;i++){
		    	xx=data[i].x;
		    	yy=data[i].y;
		    	zz=data[i].z;
		    	datalist[i]=[xx,yy,zz]
		    }
		    initDensity(sydy,cysj,datalist);
		}
	}
});
}

function getEventData(line_index,startTime,endTime) {
	jQuery.ajax({
	type : 'POST',
	dataType : "json",
	data : {
		lineIndex:line_index,
		startTime:startTime,
		endTime:endTime
	},
	cache : false,
	url : '/pqs9200/device/eventdatalist',
	error : function() {},
	success : function(data) {
		var pointI = [];
		var pointIun=[];
		
		var top = new Array();
		top[0] = [0.001,200];
		top[1] = [0.003,140];
		top[2] = [0.003,120];
		top[3] = [0.5,120];
		top[4] = [0.5,110];
		top[5] = [10,110];
		top[6] = [1000,110];
		
		var bot = new Array();
		bot[0] = [0.02,0];
		bot[1] = [0.02,70];
		bot[2] = [0.5,70];
		bot[3] = [0.5,80];
		bot[4] = [10,80];
		bot[5] = [10,90];
		bot[6] = [1000,90];
		
		var part = new Array();
		part[0] = [0.05, 0];
		part[1] = [0.05, 50];
		part[2] = [0.12, 50];
		part[3] = [0.12, 70];
		part[4] = [0.16, 70];
		part[5] = [0.16, 80];
		part[6] = [10, 80];
		part[7] = [1000, 80];
		
		var sum1=0;
		var sum2=0;
		var sum3=0;
		var sum4=0;
		var sum5=0;
		var sum=[];
		for ( var i = 0; i < data.length; i++) {
			var point=[];
			xx=data[i].persisttime;
			yy=data[i].eventvalue;
			point = [xx,yy];
			
			if(xx<=0.003){
				var line=0;
				line = 230-30000*xx;
				if(yy>line){
					pointIun.push({value:point,itemStyle:{normal:{color:red}}});
				}else{
					pointI.push({value:point,itemStyle:{normal:{color:green}}});
				}
			}else if(xx<=0.02){
				if(yy>120){
					pointIun.push({value:point,itemStyle:{normal:{color:red}}});
				}else{
					pointI.push({value:point,itemStyle:{normal:{color:green}}});
				}
			}else if(xx<=0.5){
				if(yy>120 || yy<70){
					pointIun.push({value:point,itemStyle:{normal:{color:red}}});
				}else{
					pointI.push({value:point,itemStyle:{normal:{color:green}}});
				}
			}else if(xx<=10){
				if(yy>110 || yy<80){
					pointIun.push({value:point,itemStyle:{normal:{color:red}}});
				}else{
					pointI.push({value:point,itemStyle:{normal:{color:green}}});
				}
			}else{
				if(yy>110 || yy<90){
					pointIun.push({value:point,itemStyle:{normal:{color:red}}});
				}else{
					pointI.push({value:point,itemStyle:{normal:{color:green}}});
				}
			}
		}
		
		initEvent(top,bot,pointI,pointIun);
	}
});
}

function getEventTime(line_index,startTime,endTime) {
	jQuery.ajax({
	type : 'POST',
	dataType : "json",
	data : {
		lineIndex:line_index,
		startTime:startTime,
		endTime:endTime
	},
	cache : false,
	url : '/pqs9200/device/eventTime',
	error : function() {},
	success : function(data) {
		initTime(data);
	}
});
}

function getEventChance(line_index,startTime,endTime){
	jQuery.ajax({
		type : 'POST',
		dataType : "json",
		data : {
			lineIndex:line_index,
			startTime:startTime,
			endTime:endTime
		},
		cache : false,
		url : '/pqs9200/device/eventChance',
		error : function() {},
		success : function(data) {
			if(data == null){
				layer.msg('加载失败！',{icon:2,time:1000});
			}else{
				var eventValue=data.eventvalue;
				
				initChance(eventValue);
			}
		}
	});
}

function refreshState(json){
	if(document.getElementById('sb') != null){
		document.getElementById('sb').contentWindow.refreshState(json);
	}
}