//var colors = ['#999999','#FE8E60','#EDD24D','#B4ECE9','#D2B6E9','#B0CDB1','#EB717E','#BF8A9A','#EDC8B5','#A0A0A0'];
// var colors = ['#1890FF','#B4ECE9','#B0CDB1','#FE8E60','#B4ECE9','#B0CDB1','#FE8E60','#B4ECE9','#B0CDB1','#FE8E60'];
var colors = ['#1890FF', '#1890FF', '#1890FF', '#1890FF', '#1890FF', '#1890FF', '#1890FF', '#1890FF', '#1890FF', '#1890FF'];
var colors2 = ['#FE8E60','#FE8E60','#B4ECE9','#B0CDB1','#FE8E60','#B4ECE9','#B0CDB1','#FE8E60','#B4ECE9','#B0CDB1'];
var colors3 = ['#1890FF','#F6BD16','#E8684A','#6DC8EC','#9270CA','#FF9D4D','#269A99','#73A0FA','#5AD8A6','#5D7092','#6DC8EC','#9270CA','#FF9D4D','#269A99'];

/**
 * 生成饼图
 */
function drawPie(divId, data, title) {
	option = {
		tooltip: {
			trigger: 'item',
			formatter: '{a} <br/>{b} : {c} ({d}%)'
		},
		series: [
			{	
				color : colors3,
				name:title,
				type: 'pie',
				radius: '75%',
				center: ['50%', '50%'],
				data: data,
				// data: [
				// 	{value: 335, name: '直接访问'},
				// 	{value: 310, name: '邮件营销'},
				// 	{value: 234, name: '联盟广告'},
				// 	{value: 135, name: '视频广告'},
				// 	{value: 1548, name: '搜索引擎'}
				// ],
				emphasis: {
					itemStyle: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					}
				}
			}
		]
	};
	var myChart = echarts.init(document.getElementById(divId));
	document.getElementById(divId).setAttribute('_echarts_instance_', '')
	myChart.clear();
	myChart.setOption(option);
}

function trend_pie(title,legend,series,divId){
	var option = {
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    legend: {
		    	x : 'center',
		        y : 'bottom',
		        data:legend
		    },
		    calculable : true,
		    series : [
		        {
		            name:title,
		            type:'pie',
		            radius : [30, 70],
		            center : ['50%', 150],
		            roseType : 'area',
		            x: '50%',               // for funnel
		            max: 40,                // for funnel
		            sort : 'ascending',     // for funnel
		            data:series
		        }
		    ],
		    color : colors
		};
		                    
	require(
			[
		        'echarts',
		        'echarts/chart/pie'
		    ],
		    function(orderTrendEC){
		        var myChart = orderTrendEC.init(document.getElementById(divId));
		        var ecConfig = require('echarts/config');   
		        myChart.setOption(option);
		        window.onresize = myChart.resize;
		    }
	);
}


function trend_pie_normal(title,legend,series,divId){
	var option = {
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    legend: {
		        orient : 'vertical',
		        x : 'left',
		        data:legend
		    },
		    calculable : true,
		    series : [
		        {
		            name:title,
		            type:'pie',
		            radius : [30, 70],
		            center : ['50%', 150],
		            data:series
		        }
		    ],
		    color : colors
		};
		                    
	require(
			[
		        'echarts',
		        'echarts/chart/pie'
		    ],
		    function(orderTrendEC){
		        var myChart = orderTrendEC.init(document.getElementById(divId));
		        var ecConfig = require('echarts/config');   
		        myChart.setOption(option);
		        window.onresize = myChart.resize;
		    }
	);
}




function trendBarWithLine(legend,xdata,ydata,ys,divId){
	var i = 0,barChart = {
		    tooltip : {
		        trigger: 'axis'
		    },
		    grid: {show:'true',borderWidth:'0'},
		    legend: {
	            data:legend
	        },
		    calculable : true,
		    xAxis : [
		        {
		            type : 'category',
		            axisLabel: {
		                interval: 0,
		                rotate: 30
		            },
		            data : xdata,
		            splitLine:{show: false}//去除网格线
		        }
		    ],
		    yAxis : [
		             {  
		                 type : 'value',  
		                 position: 'left',  
		                 splitNumber: 0.1,  
		                 boundaryGap: [0,1],  
		             },  
		             {  
		                 type : 'value',  
		                 splitNumber: 0.1,  
		                 axisLabel : {  
		                     formatter: function (value) {  
		                         // Function formatter  
		                         return value
		                     }  
		                 },  
		                 splitLine : {  
		                     show: false  
		                 }  
		             }  
		    ],
		    grid: { 
	             y2: 100
	        },
		    series : [
		        {
		            name:legend[0],
		            type:'bar',
		            data:ydata,
		            barWidth : 20 ,//柱图宽度
		            itemStyle:{
			            normal:{
			              color:function (params){
	                        return colors2[i++ % 10];
		                  },
		                  label: {  
                              show: true,//是否展示  
                              textStyle: {  
                                  fontWeight:'bolder',  
                                  fontSize : '12',  
                                  fontFamily : '微软雅黑',  
                              }  
                          } 
			          }
			       }
		        },
	            {
	                name:legend[1],
	                type:'line',
	                yAxisIndex: 1,
	                data: ys,
	                itemStyle:{
			            normal:{
			              color:'#FE8E60'
			          }
			       }
	            }
		    ]
		};
	require(
			[
		        'echarts',
		        'echarts/chart/bar',
		        'echarts/chart/line'
		    ],
		    function(orderTrendEC){
		        var myChart = orderTrendEC.init(document.getElementById(divId));
		        var ecConfig = require('echarts/config');
		        myChart.setOption(barChart);
		    }
	);
}


function trendBar(ytitle,name,xdata,ydata,divId){
	var barWidth = 10;
	barWidth = 25;
	// 设置y轴最大、最小值
	var yMax = 0.05;
	for (var i = 0; i < ydata.length; i++) {
		var temp = ydata[i];
		yMax = yMax < temp ? temp : yMax;
	}
	// yMax = yMax + yMax * 0.2
	yMax = (yMax * 1.2).toFixed(4);

	var i=0,barChart = {
		    tooltip : {
		        trigger: 'axis'
		    },
		    calculable : true,
		    xAxis : [
		        {
		            type : 'category',
		            axisLabel: {
		                interval: 0,
		                rotate: 30
		            },
		            data : xdata,
		            splitLine:{show: false}//去除网格线
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		            //splitNumber : 0.1,
					name : ytitle,
					max: yMax,
					min:0,
					// max: function(value) {
					// 	return value.max + (value.max * 0.2);
					// },
		            //splitLine:{show: false}//去除网格线
		        }
		    ],
		    grid: { 
				top:20,
				x:60,
				x2:40,
	            y2: 50,
	            show:'true',borderWidth:'0'
	        },
		    series : [
		        {
		            name:name,
		            type:'bar',
		            data:ydata,
		            barWidth : barWidth,//柱图宽度
		            itemStyle:{
			            normal:{
			              color:function (params){
	                        return colors[i++ % 10];
		                  },
		                  label: {  
                              show: true,//是否展示  
                              textStyle: {  
                                  fontWeight:'bolder',  
                                  fontSize : '12',  
                                  fontFamily : '微软雅黑',  
                              }  
                          } 
			          }
			       }
		        }
		    ]
		};
	require(
			[
		        'echarts',
		        'echarts/chart/bar'
		    ],
		    function(orderTrendEC){
		        var myChart = orderTrendEC.init(document.getElementById(divId));
		        //var ecConfig = require('echarts/config');
		        myChart.setOption(barChart);
		    }
	);
}

/**
 * 横向的柱状图
 * @param name
 * @param xdata
 * @param ydata
 * @param divId
 */
function trendHorizontalBar(name,xdata,ydata,divId,cur_colors){
	var i=0,option = {
		    tooltip : {
		        trigger: 'axis'
		    },
		    grid: {show:'true',borderWidth:'0', x : 135},
		    calculable : true,
		    xAxis : [
		        {
		            type : 'value',
		            boundaryGap : [0, 0.01],
		            splitLine:{show: false},//去除网格线
		            splitNumber : 0.1
		        }
		    ],
		    yAxis : [
		        {
		            type : 'category',
		            data : ydata,
		            splitLine:{show: false},//去除网格线
		            axisLabel: {
		                textStyle: {
		                    fontSize: 11,
		                    align : 'left'
		                },
		            	margin : 115,
		            	formatter : function (value){
		            		if(value.length > 10){
		            			return value.substring(0, 10) + "..";
		            		}else{
		            			return value;
		            		}
		            		
		            	}
		            }
		        }
		    ],
		    series : [
		        {
		            name:name,
		            type:'bar',
		            barWidth : 10,//柱图宽度
		            data:xdata,
		            itemStyle:{
			            normal:{
			              color:function (params){
			            	if(cur_colors.length > 0){
			            		var color = cur_colors[i++ % cur_colors.length];
			            		return color;
			            	}
		                  },
		                  label: {  
                              show: true,//是否展示  
                              textStyle: {  
                                  fontWeight:'bolder',  
                                  fontSize : '11',  
                                  fontFamily : '微软雅黑',  
                              }  
                          } 
			          }
		            }
		        }
		    ]
		};
	require(
			[
		        'echarts',
		        'echarts/chart/bar'
		    ],
		    function(orderTrendEC){
		        var myChart = orderTrendEC.init(document.getElementById(divId));
		        var ecConfig = require('echarts/config');
		        myChart.setOption(option);
		    }
	);
}