(function(){
    X.pageLogic['retained'] = {
        init : function(parms){

            X.template("grouped","shushi");
            // X.template("toptab","analysis-zhanwei",X.DATA['projectarr']);
            var data ={
                "eventView":{
                    "cksql":"retention",
                    "endTime":"2021-06-07 23:59:59",
                    "filts":[],
                    "groupBy":[],
                    "e_days":0,
                    "s_days":7,
                    "relation":"and",
                    "startTime":"2021-05-09 00:00:00",
                    "statType":"retention",
                    "timeParticleSize":"P1D",
                    "unitNum":7
                },
                "events":[
                    
                ],
                "cachedata":{
                    eventdata:[],
                    initialevent:[],
                    returnevent:[],
                    filtersymbols:[],
                    groupitemeventsdata:[],
                    type2data:[]
                }
            };

            // 日期渲染
            X.daterender("#retained-condition-date",function(start, end, label){
                var startTime = start.format('YYYY-MM-DD')+ " "+ "00:00:00";
                var endTime = end.format('YYYY-MM-DD')+ " "+ "23:59:59";
                data['eventView']['startTime'] = startTime;
                data['eventView']['endTime'] = endTime;

                var sameday = Date.parse(new Date()) / 1000; //当天时间戳
                
                var enddate = new Date(end.format('YYYY-MM-DD'));
                var endmonth = Date.parse(enddate)/1000;

                var startdate = new Date(start.format('YYYY-MM-DD'));
                var startmonth = Date.parse(startdate)/1000;
                data['eventView']['e_days'] = parseInt((sameday - endmonth)/86400);
                data['eventView']['s_days'] = parseInt((sameday - startmonth)/86400);
                retaineddata();
            });

            var time = $("#retained-condition-date").val();
            var timearr = time.split(" ");
            data['eventView']['startTime'] = timearr[0] + " "+"00:00:00";
            data['eventView']['endTime'] = timearr[2]+" "+"23:59:59";

            var eventdata;
            var filtersymbols = [[],[]];//默认两个事件
            var groupitemeventsdata=[[],[]]//分组项里的事件
            var type2data = [[],[]];

            X.api("data_auth/my_event","get",{},function(d){
                eventdata = d;

                X.api("data_auth/load_prop_quotas","post",{event_name:eventdata[0]['category'][0]['event_name']},function(val){
                    
                    type2data[0] = val.props;
                    type2data[1] = val.props;

                    var arr = [{
                        "eventDesc":eventdata[0]['category'][0]['event_desc'],
                        "eventName":eventdata[0]['category'][0]['event_name'],
                        "filts":[],
                        "relation":"and",
                        "relationUser":"and",
                        "renameindicator":"",
                        "event_attr": type2data[0][0]['title'],
                        "event_attr_id": type2data[0][0]['id'],
                    },{
                        "eventDesc":eventdata[0]['category'][0]['event_desc'],
                        "eventName":eventdata[0]['category'][0]['event_name'],
                        "filts":[],
                        "relation":"and",
                        "relationUser":"and",
                        "renameindicator":"",
                    }]
                    data['events'] = arr;
                    X.api("data_auth/load_filter_props","post",{event_name:d[0]['category'][0]['event_name']},function(val){
                        groupitemeventsdata[0] = val;
                        groupitemeventsdata[1] = val;
                        //初始事件
                        // console.log(data['events'][0]);
                        X.template("event","initial-event",data['events'][0]);
                        X.template("returnvisitevent","returnvisit-event",data['events'][1]);

                        //从数据看板点击进来触发事件
                        if(X.DATA.retentionid && X.DATA.retentionid != ''){
                            var report_idarr = [];
                            report_idarr.push(X.DATA.retentionid);
                            X.api('report/read_report','post',{project_id:X.DATA.projectid,report_id:report_idarr},function(d){
                                backfilldata(d[0]);
                            })
                        }


                    })

                })
                
            });

            $(document).off('click','.analysis-zhibiao-list-type2').on('click','.analysis-zhibiao-list-type2',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                if(type2data.length >0){
                    X.querycriteriapop(type2data[index],'dropdownlist',offset.left,offset.top+obj.height(),function(val){
                        // if(val != undefined){
                            console.log(val);
                            $("#analysis-zhibiao-list-type2-"+index).html(val.title);
                            data['events'][index]['event_attr'] = val.title;
                            data['events'][index]['event_attr_id'] = val.id;
                        // }
                    })
                }
            });
           
            //开启重命名
            $(document).off('click','.analysis-chongmingming-initial-event').on('click','.analysis-chongmingming-initial-event',function(){
                $("#analysis-con-left-screen-list-left-zhibiaoname-initial-event").show();
                $(this).hide();
                $(".analysis-chongmingming-del-initial-event").show();
            });

            $(document).off('click','.analysis-chongmingming-return-event').on('click','.analysis-chongmingming-return-event',function(){
                $("#analysis-con-left-screen-list-left-zhibiaoname-return-event").show();
                $(this).hide();
                $(".analysis-chongmingming-del-return-event").show();
            });

            //取消重命名
            $(document).off('click','.analysis-chongmingming-del-initial-event').on('click','.analysis-chongmingming-del-initial-event',function(){
                $("#analysis-con-left-screen-list-left-zhibiaoname-initial-event").hide();
                $("#analysis-con-left-screen-list-left-zhibiaoname-initial-event input").val('');
                $(this).hide();
                $(".analysis-chongmingming-initial-event").show();
            });

            $(document).off('click','.analysis-chongmingming-del-return-event').on('click','.analysis-chongmingming-del-return-event',function(){
                $("#analysis-con-left-screen-list-left-zhibiaoname-return-event").hide();
                $("#analysis-con-left-screen-list-left-zhibiaoname-return-event input").val('');
                $(this).hide();
                $(".analysis-chongmingming-return-event").show();
            });

            // 添加初始事件筛选
            // $(document).off('click','.analysis-zhibiao-updata-list-box .analysis-zhibiao-updata-initial-event').on('click','.analysis-zhibiao-updata-list-box .analysis-zhibiao-updata',function(){
            //     var arr = {
            //         "columnDesc":groupitemeventsdata[0][0]['category'][0]['title'],
            //         "columnName":groupitemeventsdata[0][0]['category'][0]['id'],
            //         "comparator":"==",
            //         "comparatorName":"等于",
            //         "ftv":[],
            //         "strftv":"",
            //         "table_type":groupitemeventsdata[0][0]['category'][0]['data_type'],
            //         "timeUnit":""
            //     }
            //     data['events'][0]['filts'].push(arr);
            //     filtersymbols[0].push(groupitemeventsdata[0][0]['category'][0]['category']);
            //     X.template("event","initial-event",data['events'][0]);
            // })

            // $(document).off('click','.analysis-zhibiao-updata-list-box .analysis-zhibiao-updata-return-event').on('click','.analysis-zhibiao-updata-list-box .analysis-zhibiao-updata',function(){
            //     var arr = {
            //         "columnDesc":groupitemeventsdata[1][0]['category'][0]['title'],
            //         "columnName":groupitemeventsdata[1][0]['category'][0]['id'],
            //         "comparator":"==",
            //         "comparatorName":"等于",
            //         "ftv":[],
            //         "strftv":"",
            //         "table_type":groupitemeventsdata[1][0]['category'][0]['data_type'],
            //         "timeUnit":""
            //     }
            //     data['events'][1]['filts'].push(arr);
            //     filtersymbols[1].push(groupitemeventsdata[1][0]['category'][0]['category']);
            //     X.template("returnvisitevent","returnvisit-event",data['events'][1]);
            // })

            // 添加初始事件筛选
            var initialevent = []
            $(document).off('click','.analysis-shaix-initial-event').on('click','.analysis-shaix-initial-event',function(){
                var arr = {
                    "columnDesc":groupitemeventsdata[0][0]['category'][0]['title'],
                    "columnName":groupitemeventsdata[0][0]['category'][0]['id'],
                    "comparator":"==",
                    "comparatorName":"等于",
                    "ftv":[],
                    "strftv":"",
                    "table_type":groupitemeventsdata[0][0]['category'][0]['data_type'],
                    "timeUnit":""
                }
                initialevent.push(arr);
                data['events'][0]['filts'] = initialevent;
                filtersymbols[0].push(groupitemeventsdata[0][0]['category'][0]['category']);

                X.template("event","initial-event",data['events'][0]);
            });
            var returnevent = [];
            $(document).off('click','.analysis-shaix-return-event').on('click','.analysis-shaix-return-event',function(){
                var arr = {
                    "columnDesc":groupitemeventsdata[1][0]['category'][0]['title'],
                    "columnName":groupitemeventsdata[1][0]['category'][0]['id'],
                    "comparator":"==",
                    "comparatorName":"等于",
                    "ftv":[],
                    "strftv":"",
                    "table_type":groupitemeventsdata[1][0]['category'][0]['data_type'],
                    "timeUnit":""
                }
                // data['events'][1]['filts'].push(arr);
                returnevent.push(arr);
                data['events'][1]['filts'] = returnevent;
                filtersymbols[1].push(groupitemeventsdata[1][0]['category'][0]['category']);
                X.template("returnvisitevent","returnvisit-event",data['events'][1]);
            });

            // 删除指定初始事件筛选
            $(document).off('click','.analysis-zhibiao-list-del-initial-event').on('click','.analysis-zhibiao-list-del-initial-event',function(){
                var index = $(this).attr("data-twonum");
                data['events'][0]['filts'].splice(index,1);
                filtersymbols[0].splice(index,1);
                X.template("event","initial-event",data['events'][0]);
            });

            $(document).off('click','.analysis-zhibiao-list-del-return-event').on('click','.analysis-zhibiao-list-del-return-event',function(){
                var index = $(this).attr("data-twonum");
                data['events'][1]['filts'].splice(index,1);
                filtersymbols[1].splice(index,1);
                X.template("returnvisitevent","return-event",data['events'][1]);
            });
            
            $(document).off('click','.analysis-con-left-screen-list-left-zhibiaoname-initial-event input').on('blur','.analysis-con-left-screen-list-left-zhibiaoname-initial-event input',function(){
                var txt = $(this).val();
                data['events'][0]['renameindicator'] = txt;
            });

            $(document).off('click','.analysis-con-left-screen-list-left-zhibiaoname-return-event input').on('blur','.analysis-con-left-screen-list-left-zhibiaoname-return-event input',function(){
                var txt = $(this).val();
                data['events'][1]['renameindicator'] = txt;
            });

            $(document).off('click','.ant-dropdown-trigger').on('click','.ant-dropdown-trigger',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                var dataevent= data['events'][index];
                // console.log(dataevent);
                X.querycriteriapop(eventdata,'setgrouping',offset.left,offset.top+obj.height(),function(val){
                    console.log(val);
                    obj.html(val.id);
                    dataevent['eventName'] = val.name;
                    dataevent['eventDesc'] = val.id;

                    X.api("data_auth/load_prop_quotas","post",{event_name:val.name},function(val){
                        type2data[index] = val.props;
                    })

                    X.api("data_auth/load_filter_props","post",{event_name:val.name},function(val){
                        groupitemeventsdata[index] = val;
                    })
                    // console.log(data['events']);

                })
            });

            $(document).off('click','.ta-property-select').on('click','.ta-property-select',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                var indexs = obj.attr("data-indexs");
                X.querycriteriapop(groupitemeventsdata[0],'category',offset.left,offset.top+obj.height(),function(val){
                    console.log(val);
                    var htmlstring = val.title;
                    obj.html(htmlstring);
                    filtersymbols[index][indexs] = val.category;
                    data['events'][index]['filts'][indexs]['columnDesc'] = htmlstring;
                    data['events'][index]['filts'][indexs]['columnName'] = val.id;
                    data['events'][index]['filts'][indexs]['data_type'] = val.type;
                    data['events'][index]['filts'][indexs]['table_type'] = val.table_type;
                })
            });

            // 过滤数据
            function filteringdata(){
                var screentypedata;
                console.log(groupitemeventsdata);
                if(groupitemeventsdata.length>1){
                    for(var i =0; i < groupitemeventsdata.length - 1; i++){
                        if(i == 0){
                            X.groupintersection(groupitemeventsdata[i] , groupitemeventsdata[Number(i) + Number(1)],function(d){
                                screentypedata = d;
                            });
                        }else {
                            X.groupintersection(screentypedata , groupitemeventsdata[Number(i) + Number(1)],function(d){
                                screentypedata = d;
                            })
                        }
                    }
                }else { 
                    screentypedata = groupitemeventsdata[0];
                }
                return screentypedata;
            };
            
            $(document).off('click','.analysis-choicetypename').on('click','.analysis-choicetypename',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = $(this).attr("data-index");
                var indexs = $(this).attr("data-indexs");
                X.querycriteriapop(filtersymbols[index][indexs],'dropdownlist',offset.left,offset.top+obj.height(),function(val){
                    if(val != undefined){
                        obj.html(val.title);
                        data['events'][index]['filts'][indexs]['comparatorName'] = val.title;
                        data['events'][index]['filts'][indexs]['comparator'] = val.id;
                    }
                })
            });

            $(document).off('click','.analysis-zhibiao-list-type4 input').on('blur','.analysis-zhibiao-list-type4 input',function(){
                var index = $(this).attr('data-index');
                var indexs = $(this).attr('data-indexs');
                var txt = $(this).val();
                var txtarr = txt.split(",");
                data['events'][index]['filts'][indexs]['strftv'] = txt;
                data['events'][index]['filts'][indexs]['ftv'] = txtarr;
            });

            //全局筛选
            var filtsftvdata=[];
            $(document).off('click','.analysis-overall-situation-add').on('click','.analysis-overall-situation-add',function(){
                var screentypedata = groupitemeventsdata[0];
                var drr={
                    "columnName":screentypedata[0]['category'][0]['title'],//事件中文显示
                    "column_id":screentypedata[0]['category'][0]['id'],//事件id
                    'data_type':screentypedata[0]['category'][0]['data_type'],//类型
                    'comparator_name':'等于',//默认等于
                    "comparator_id":'==',//符号id
                    "ftv":[],//条件
                    "strftv":'',//显示条件
                    "section":[-1,1],//区间
                    "table_type":screentypedata[0]['id']
                };
                data['eventView']['filts'].push(drr);
                filtsftvdata.push(screentypedata[0]['category'][0]['category']);
                if(data['eventView']['filts'].length > 1){
                    $(".analysis-overall-situation-left-guanxi").show();
                }else{
                    $(".analysis-overall-situation-left-guanxi").hide();
                }
                X.template("globalfilter","xinwei-whole",data['eventView']['filts'],function(){
                    analysisdown();
                });
            });

            //删除指定全局数据
            $(document).off('click','.analysis-overall-situation-del').on('click','.analysis-overall-situation-del',function(){
                var index = $(this).attr('data-index');
                data['eventView']['filts'].splice(index,1);
                filtsftvdata.splice(index,1);
                if(data['eventView']['filts'].length > 1){
                    $(".analysis-overall-situation-left-guanxi").show();
                }else{
                    $(".analysis-overall-situation-left-guanxi").hide();
                }
                X.template("globalfilter","xinwei-whole",data['eventView']['filts'],function(){
                    analysisdown();
                });
            });

            $(document).off('click','.analysis-overall-situation-attr').on('click','.analysis-overall-situation-attr',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                X.querycriteriapop(groupitemeventsdata[0],'category',offset.left,offset.top+obj.height(),function(val){
                    console.log(val);
                    obj.html(val.title);
                    data['eventView']['filts'][index]['columnName'] = val.title;
                    data['eventView']['filts'][index]['column_id'] = val.id;
                    data['eventView']['filts'][index]['data_type'] = val.type;
                    data['eventView']['filts'][index]['comparator_name'] = '等于';
                    data['eventView']['filts'][index]['comparator_id'] = '==';
                    data['eventView']['filts'][index]['table_type'] = val.table_type;
                    filtsftvdata[index]=val.category;
                    analysisdown();//渲染下拉框
                })
            });

            //循环给条件框渲染下拉框
            function analysisdown(){
                for(let i in data['eventView']['filts']){
                    X.laydropdown("#analysis-overall-situation-fuhao-"+i,filtsftvdata[i],function(val){
                        $(this).html(val.title);
                        data['eventView']['filts'][i]['comparator_name'] = val.title;
                        data['eventView']['filts'][i]['comparator_id'] = val.id;
                        if(val.id == "range"){
                            data['eventView']['filts'][i]['ftv'] = [-1,1];
                        }
                        X.template("globalfilter","xinwei-whole",data['eventView']['filts'],function(){
                            analysisdown();
                        });
                    })
                }
            };

            // 分组项
            $(document).off('click','.analysis-action-right').on('click','.analysis-action-right',function() {
                var index = $(this).attr("data-index");
                data.eventView.groupBy.splice(index,1);
                X.template("grouped","shushi",data['eventView']['groupBy']);
            });

            $(document).off('click','.analysis-footadd___2D4YB').on('click','.analysis-footadd___2D4YB',function() {
                
                var arr = {
                    "columnName": "#account_id",//事件中文显示
                    "column_id": "#account_id",//事件id
                    'data_type': "string",//类型
                    'table_type': "event",
                    "ftv":'',//条件
                    "section":''//区间
                };
                data['eventView']['groupBy'].push(arr);
                X.template("grouped","shushi",data['eventView']['groupBy']);

            });

            $(document).off('click','.analysis-ant-dropdown-trigger').on('click','.analysis-ant-dropdown-trigger',function(){
                var obj = $(this);
                var offset = obj.offset();
                var index = obj.attr("data-index");
                X.querycriteriapop(groupitemeventsdata[0],'category',offset.left,offset.top+obj.height(),function(val){
                    console.log(val);
                    var htmlstring = val.title;
                    obj.html(htmlstring);
                    data.eventView.groupBy[index]['columnName'] = val.title;
                    data.eventView.groupBy[index]['column_id'] = val.id;
                    data.eventView.groupBy[index]['data_type'] = val.type;
                    data.eventView.groupBy[index]['table_type'] = val.table_type;
                })
            });

            // sql语句显示
            $(document).off('click','.sqlquery').on('click','.sqlquery',function(){
                    var obj = $(this);
                    var offset = obj.offset();
                    X.querycriteriapop(data,'conditionquery',offset.left - 410,offset.top+obj.height(),function(){
                        
                    })
            });

            // 时间粒度
            var timeParticleSizearr = [
                {'title':'当日','id':'1'},
                {'title':'次日','id':'2'},
                {'title':'7日','id':'7'},
                {'title':'14日','id':'14'},
                {'title':'30日','id':'30'},
            ];
            X.laydropdown('.analtsis-timeParticleSize',timeParticleSizearr,function(d){
                // console.log(d);
                $(".analtsis-timeParticleSize").html(d.title);
                data['eventView']['unitNum'] = parseInt(d.id);
                retaineddata();
            });

            // 计算按钮
            $(document).off('click','.analysis-calculation').on('click','.analysis-calculation',function(){
                // console.log(data);
                retaineddata()
            });

            function retaineddata(){
                data['cachedata']['eventdata'] = eventdata;
                data['cachedata']['initialevent'] = initialevent;
                data['cachedata']['returnevent'] = returnevent;
                data['cachedata']['groupitemeventsdata'] = groupitemeventsdata;
                data['cachedata']['filtersymbols'] = filtersymbols;
                var dataArr = {
                    eventView: data.eventView,
                    events: data.events
                }
                X.api("ck/retention_model","post",dataArr,function(d){
                    // console.log(data);
                    querydata = d;
                    updata(d)
                })
            };

            $(document).off('click','.analysis-savereport').on('click','.analysis-savereport',function(){
                if(baobiaodata != '' && baobiaodata != undefined && baobiaodata != null){
                    X.parametersopen(baobiaodata,'savereport','auto');
                }else{
                    X.parametersopen(data,'savereport','auto');          
                }
            });

            $(document).off('click','.analysis-navigation-bar-right-img-baobiao').on('click','.analysis-navigation-bar-right-img-baobiao',function(){
                X.parametersopen(data['eventView']['cksql'],'savedbaobiao','rt',function(d){
                    console.log(d);
                    if(d){
                        backfilldata(d);
                    }

                })
            });

            var baobiaodata;//以存报表数据
            function backfilldata(d){
                data = d.query;
                eventdata = data.cachedata.eventdata;
                initialevent = data.cachedata.initialevent;
                returnevent = data.cachedata.returnevent;
                filtersymbols = data.cachedata.filtersymbols;
                groupitemeventsdata = data.cachedata.groupitemeventsdata;
                baobiaodata = d;

                X.template("event","initial-event",data['events'][0]);
                X.template("returnvisitevent","returnvisit-event",data['events'][1]);
                X.template("globalfilter","xinwei-whole",data['eventView']['filts'],function(){
                    analysisdown();
                });
                X.template("grouped","shushi",data['eventView']['groupBy']);
      
                retaineddata();
            };

            function updata(d){
                var title = $(".select-chart").attr("title");
                if(title == '数据报表'){ $(".table-div-box").show(); $(".analysis-echarts").hide(); retainedtable(d)}
                else if(title == '第N日留存'){ $(".table-div-box").hide(); $(".analysis-echarts").show(); retainednRetained(d) }
                else if(title == '每日留存'){ $(".table-div-box").hide(); $(".analysis-echarts").show(); retaineddayRetained(d) }
            };

            function retainedtable(d){
                var tabledata=[];
                for(let i in d['summary_values']){
                    var arr = {
                        date: i,
                        d0:d['summary_values'][i]['d0'],
                        data:[]
                    }
                    for(let j in d['summary_values'][i]["n"]){
                        var nrr = {
                            n:0,
                            p:0
                        }
                        nrr["n"] = d['summary_values'][i]["n"][j];
                        nrr["p"] = d['summary_values'][i]["p"][j]
                        arr['data'].push(nrr);
                    }
                    tabledata.push(arr)
                }
    
                var titdata = [];
                for(let i in d['days']){
                    var tit = "第" + i + "日";
                    titdata.push(tit);
                }
                titdata.unshift("日期",d.title);
                X.laytpldata("#table-fenxi-th-dot",titdata,".table-fenxi-th");
    
                X.laytpldata("#table-fenxi-td-dot",tabledata,".table-fenxi-td");
            };

            // 每日留存
            function retaineddayRetained(d){
                var arr = {};
                var xAxisData=[];
                var seriesData=[];
                var legenddata = [];
                if(jQuery.isEmptyObject(d.values)){
                    legenddata = d.date_range;
                    for(let i in d['days']){
                        var tit = "第" + i + "日";
                        xAxisData.push(tit);
                    }
                    for(let i in d['summary_values']){
                        var arr = {
                            name: i,
                            type: 'line',
                            data: d['summary_values'][i]['p']
                        }
                        seriesData.push(arr);
                    }
                }else {
                    var timearr = [];
                    for(let i in d.values){
                        timearr.push(i);
                        for(let j in d.values[i]){
                            if(arr[j]){
                                arr[j][i] = d.values[i][j]['p'];
                            }else {
                                arr[j]=[];
                                arr[j][i] = d.values[i][j]['p'];
                            }
                        }
                    }
    
                    var ydata = {};
                    for(let j in arr){
                        if(!ydata[j]){
                            ydata[j] = [];
                        }
                        for( let i in timearr ){
                            ydata[j].push("");
                            for(let z in arr[j]){
                                if(timearr[i] == z){
                                    var index = arr[j][z].length - 1;
                                    ydata[j][i] =arr[j][z][index];
                                }
                            }
                        }
                    }
                    console.log(ydata);
                    // for(let i in ydata){    
                    //     legenddata.push(i);
                    //     var arr = {
                    //         name: i,
                    //         type: 'line',
                    //         data: ydata[i]
                    //     }
                    //     seriesData.push(arr)
                    // } 
                }
                return;
                var myChart = echarts.init(document.getElementById('analysis-echarts'));
                var option = {
                    title: {
                        text: ''
                    },
                    color:  X.DATA.echartscolor,
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    legend: {
                        type:'scroll',
                        data: legenddata,
                    },
                    grid: {
                        left: '2%',
                        right: '2%',
                        bottom: '2%',
                        top: '10%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: xAxisData
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: seriesData
                };
                myChart.setOption(option,true);
            };

            function retainednRetained(d){
               var arr = {};
               var legenddata = [];
               var seriesData = [];
               if(jQuery.isEmptyObject(d.values)){
                var arr = []
                for(let i in d['summary_values']){
                    var index = d['summary_values'][i]['p'].length - 1;
                    arr.push(d['summary_values'][i]['p'][index]);
                }

                seriesData = {
                    type: 'line',
                    data: arr
                }
               }else {
                var timearr = [];
                for(let i in d.values){
                    timearr.push(i);
                    for(let j in d.values[i]){
                        if(arr[j]){
                            arr[j][i] = d.values[i][j]['p'];
                        }else {
                            arr[j]=[];
                            arr[j][i] = d.values[i][j]['p'];
                        }
                    }
                }
                var ydata = {};
                for(let j in arr){
                    if(!ydata[j]){
                        ydata[j] = [];
                    }
                    for( let i in timearr ){
                        ydata[j].push("");
                        for(let z in arr[j]){
                            if(timearr[i] == z){
                                var index = arr[j][z].length - 1;
                                ydata[j][i] =arr[j][z][index];
                            }
                        }
                    }
                }
                for(let i in ydata){    
                    legenddata.push(i);
                    var arr = {
                        name: i,
                        type: 'line',
                        data: ydata[i]
                    }
                    seriesData.push(arr)
                } 

               }

               console.log(seriesData);

                var myChart = echarts.init(document.getElementById('analysis-echarts'));
                var option = {
                    title: {
                        text: ''
                    },
                    color: X.DATA.echartscolor,
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    legend: {
                        type:'scroll',
                        data: legenddata
                    },
                    grid: {
                        left: '2%',
                        right: '2%',
                        bottom: '2%',
                        top: '10%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: d.date_range
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: seriesData
                };
                myChart.setOption(option,true);

            };

            var querydata;
            $(document).off('click','.analtsis-chart-switch').on('click','.analtsis-chart-switch',function(){
                if(querydata){
                    $('.analtsis-chart-switch-box .analtsis-chart-switch').removeClass('select-chart');
                    $(this).addClass('select-chart');
                    if($(this).attr('title') == '数据报表'){ $(".table-div-box").show(); $(".analysis-echarts").hide(); retainedtable(querydata)}
                    else if($(this).attr('title') == '第N日留存'){ $(".table-div-box").hide(); $(".analysis-echarts").show(); retainednRetained(querydata) }
                    else if($(this).attr('title') == '每日留存'){ $(".table-div-box").hide(); $(".analysis-echarts").show(); retaineddayRetained(querydata) }
                }else{
                    layer.msg('请先计算数据,在切换图表');
                }
            });

        }
    };
})();