jyy
2020-12-07 a86556be815c72da0fa020eb2750fb2fa73c637a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * Crm数据图表
 * Created by jiangyouyao on 2019/4/20.
 */
function ChartsUtils(loadChartData) {
 
    //赋值加载数据方法
    this.loadChartData = loadChartData;
    this.$beginTime = $("#beginTime");
    this.$endTime = $("#endTime");
    this.$unitSelect = $("#unitSelect");
    //记录当前视图的格式化字符串
    this.dateFmt = "";
    //绑定搜索事件
    $('#serach').on("click",{charts:this},this.updateChartsDate);
    //时间单位选择变化
    this.$unitSelect.on("change",{charts:this},this.unitChange);
 
 
}
 
 
 
 
/**
 * 设置时间控件格式
 */
ChartsUtils.prototype.setDataPackTimeValue = function () {
 
    console.log("设置时间");
    var unit = this.$unitSelect.val();
    var startView, minView;
    //控件赋值
    var time = new Date();
    if (unit == '时') {
        startView = 1;
        minView = "day";
        this.dateFmt = "yyyy-MM-dd hh";
        //时间视图默认显示当天
        this.$endTime.val(DateFormat.format(DateFormat.getDateEnd(time), this.dateFmt));
        this.$beginTime.val(DateFormat.format(DateFormat.getDateStart(time), this.dateFmt));
    } else if (unit == '日') {
        console.log(time.getDate());
        startView = 2;
        minView = "month";
        this.dateFmt = "yyyy-MM-dd";
        //日视图默认显示当月
        this.$endTime.val(DateFormat.format(time, this.dateFmt));
        this.$beginTime.val(DateFormat.format(DateFormat.addDay(time, -(time.getDate() - 1)), this.dateFmt));
 
    } else if (unit == '月') {
        startView = 3;
        minView = "year";
        this.dateFmt = "yyyy-MM";
        //月视图默认显示当年
        this.$endTime.val(DateFormat.format(time, this.dateFmt));
        this.$beginTime.val(DateFormat.format(DateFormat.addMonth(time, -time.getMonth()), this.dateFmt));
    } else if (unit == '年') {
        startView = 4;
        minView = "decade";
        this.dateFmt = "yyyy";
        //年视图默认显示5年
        this.$endTime.val(DateFormat.format(time, this.dateFmt));
        this.$beginTime.val(DateFormat.format(DateFormat.addYear(time, -5), this.dateFmt));
    }
    console.log("startView", startView);
    //时间
    var _initParam = {
        format: this.dateFmt.toLowerCase(),
        todayBtn: false,
        autoclose: true,
        startView: startView,
        maxView: 4,
        minView: minView
    };
    MTools.ininDatetimepicker(_initParam);
 
}
 
/**
 * 校验时间范围
 */
ChartsUtils.prototype.checkTime = function () {
    var beginTimeStr = this.$beginTime.val();
    var endTimeStr = this.$endTime.val();
    if (StringUtils.isNotEmpty(beginTimeStr) && StringUtils.isNotBlank(endTimeStr)) {
        //字符串不为空
        var beginTime = moment(beginTimeStr);
        var endTime = moment(endTimeStr);
        console.log(beginTime.unix(), endTime.unix());
        if (beginTime.unix() <= endTime.unix()) {
            return true;
        } else {
            layer.msg("最小日期不能大于最大日期", {icon: 2});
            return false;
        }
    } else {
        layer.msg("请选择日期范围", {icon: 2});
        return false;
    }
}
 
ChartsUtils.prototype.unitChange = function (event) {
    //移除时间控件上的绑定时间
    $("#beginTime").datetimepicker('remove');
    $("#endTime").datetimepicker('remove');
    //根据单位重设定查询条件,新绑定时间控件,
    event.data.charts.setDataPackTimeValue();
    event.data.charts.loadChartData();
}
 
 
/**
 * 更新图表中的数据
 */
ChartsUtils.prototype.updateChartsDate=function(event){
    if(event.data.charts.checkTime()){
        event.data.charts.loadChartData();
    }
}
 
/**
 *指定图表的配置项和数据
  */
ChartsUtils.prototype.getption=function(title) {
    return {
        title: {
            text: title,
            padding: 20
        },
        legend: {},
        toolbox: {
            show: true,
            feature: {
                mark: {show: true},
                dataView: {show: true, readOnly: false},
                magicType: {show: true, type: ['line', 'bar']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            axisLabel: {
                rotate: 30
            },
            splitLine: {
                show: false,
            },
            data: []
        },
        yAxis: {
            type: 'value'
        },
        dataZoom: [
            {
                id: 'dataZoomX',
                type: 'slider',
                xAxisIndex: [0],
                filterMode: 'filter'
            }
        ],
        animation: true,
        animationDuration: 1000,
        animationDurationUpdate: 1000,
        animationEasing: 'linear',
        animationEasingUpdate: 'linear',
    }
 
};