ChartMileage.js 11.5 KB
layui.config({
    base: "/js/"
}).use(['form', 'I18nPage', 'vue', 'layer', 'jquery', 'droptree', 'laydate', 'utils', 'formSelects'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        I18nPage = layui.I18nPage;
    var formSelects = layui.formSelects;

    var Lang = I18nPage.GetLang();
    var _lang = "cn";
    if (Lang !== "cn") {
        _lang = "en";
    }

    I18nPage.GetI18nText({
        Type: "LP",
        Code: "ChartMileage",
    });

    var MessageContainer = {};
    I18nPage.GetI18nMessage({
        Type: "PM",
        Code: "ChartMileage",
        Container: MessageContainer,
    });

    var chartType = $.getUrlParam("chartType");   //ID
    if (chartType == undefined) {
        chartType = "Daily";
    }
    var firstUnit = "";
    var chart1, chart2, chart3;

    var defaultDateRange = "";
    var startdate = new Date();
    var enddate = new Date();
    enddate.setDate(enddate.getDate() + 1)

    var startdate_m = startdate.getMonth() + 1;
    var startdate_d = startdate.getDate();
    if ((startdate_m * 1) < 10) {
        startdate_m = "0" + startdate_m;
    }
    if ((startdate_d * 1) < 10) {
        startdate_d = '0' + startdate_d;
    }
    
    var enddate_m = enddate.getMonth() + 1;
    var enddate_d = enddate.getDate();
    if ((enddate_m * 1) < 10) {
        enddate_m = "0" + enddate_m;
    }
    if ((enddate_d * 1) < 10) {
        enddate_d = '0' + enddate_d;
    }
    defaultDateRange = startdate.getFullYear() + "-" + startdate_m + "-" + startdate_d + " - " + enddate.getFullYear() + "-" + enddate_m + "-" + enddate_d;

    laydate.render({
        elem: '#daterange'
        , range: true
        , lang: _lang
        , value: defaultDateRange
        , isInitValue: true //是否允许填充初始值,默认为 true
        , done: function (value, date, endDate) {
            $('#daterange').val(value);
            $("." + chartType).click();
        }
    });

    //给按钮添加快捷键
    //Ctrl+I键添加,Ctrl+R键刷新,Ctrl+D键删除,Ctrl+E键编辑,Ctrl+F查询,Ctrl+S保存,Ctrl+A全选,Ctrl+Shift+A取消全选,左 上一页 右 下一页
    $(document).bind("keydown", function (e) {
        //禁用页面F5刷新
        if (e.which == 116) {
            e.preventDefault(); //Skip default behavior of the enter key
        }
    });

    //监听页面主按钮操作
    var active = {
        Daily: function () {
            $("." + chartType).toggleClass("layui-btn-normal");
            $(this).toggleClass("layui-btn-normal");
            chartType = "Daily";

            RefreshChart();
        },
        Weekly: function () {
            $("." + chartType).toggleClass("layui-btn-normal");
            $(this).toggleClass("layui-btn-normal");
            chartType = "Weekly";

            RefreshChart();
        },
        Monthly: function () {
            $("." + chartType).toggleClass("layui-btn-normal");
            $(this).toggleClass("layui-btn-normal");
            chartType = "Monthly";

            RefreshChart();
        },
    };

    $('.layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $.ajax({
        type: 'POST',
        url: '/task/TTaskGroup/GetTaskGroup',
        data: null,
        dataType: 'json',
        success: function (data) {
            var init = [];

            $("[name='taskgroup']").html("");
            $("[name='taskgroup']").append("<option style='display: none'></option>");
            $.each(data.data, function (key, value) {
                var option1 = $("<option>").val(value.Groupname).text(value.Groupname);
                $("[name='taskgroup']").append(option1);
                init.push(value.Groupname);
            });
            form.render('select');

            formSelects.selects({
                name: 'select',
                el: 'select[name=taskgroup]',
                filter: 'taskgroup',
                left: '',
                right: '',
                separator: ',',
                init: init,
                change: function (data, arr) {
                    $("." + chartType).click();
                },
            });

            RefreshChart();
        }
    });

    $(document).ready(function () {
        $("#btnDownload").on("click", function () {
            window.print();
        });
    });

    function RefreshChart() {
        Refresh2($('#daterange').val());
    }

    function Refresh1(datavalue) {
        datavalue = Math.ceil(datavalue / 1000000.00 * 100.00) / 100;
        var option1 = {
            tooltip: {
                trigger: 'item',
                formatter: '{b}: {c}'
            },
            legend: {
                orient: 'vertical',
                left: 10,
                data: [MessageContainer.TotalMileage]
            },
            series: [
                {
                    name: MessageContainer.TotalMileage,
                    type: 'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    label: {
                        normal: {
                            show: true,
                            position: 'center',
                            formatter: '{b}:\n {c}',
                            fontSize: 24
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data: [
                        { value: datavalue, name: MessageContainer.TotalMileage },
                    ]
                }
            ],
            color: ['#489C9C', '#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'],
        };
        if (chart1 == undefined) {
            chart1 = echarts.init(document.getElementById('chart1'));
        }
        chart1.setOption(option1);
    }

    function Refresh2(daterange) {
        var xAxisdata = [];
        var seriesdata = [];
        if (daterange == "") {
            daterange = defaultDateRange;
        }
        var chart1Data = 0;
        var startDate = daterange.split(" - ")[0];
        var endDate = daterange.split(" - ")[1];

        $.ajax({
            url: "/chart/Chart/GetMileage",
            data: {
                startDate: startDate,
                endDate: endDate,
                taskGroup: GetTaskGroup(),
                chartType: chartType,
            },
            type: "POST",
            dataType: "json",
            async: true,
            success: function (data) {
                firstUnit = "";

                $.each(data.data, function (key, value) {
                    xAxisdata.push(value.ChartValue);
                    seriesdata.push(Math.ceil(value.Mileage / 1000000.00 * 100.00) / 100);

                    chart1Data += value.Mileage;
                    if (firstUnit == "") {
                        firstUnit = value.ChartValue;
                    }
                });

                //图一
                if (1 == 1) {
                    Refresh1(chart1Data);
                }

                if (1 == 1) {
                    var option2 = {
                        tooltip: {
                            trigger: 'item',
                            formatter: '{b}: {c}'
                        },
                        title: {
                            text: chartType + "(" + daterange + ")",
                            left: 10
                        },
                        xAxis: {
                            type: 'category',
                            data: xAxisdata,
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: seriesdata,
                            type: 'bar'
                        }],
                        color: ['#489C9C', '#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'],
                    };
                    //图二
                    if (chart2 == undefined) {
                        chart2 = echarts.init(document.getElementById('chart2'));
                        chart2.on('click', function (params) {
                            Refresh3(params.name);
                        });
                    }
                    chart2.setOption(option2);
                }

                //图三
                if (1 == 1) {
                    Refresh3(firstUnit);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(errorThrown, { icon: 2, title: MessageContainer.ErrorTitle });
            }
        });
    };

    function Refresh3(unitValue) {
        var xAxisdata = [];
        var seriesdata = [];

        var daterange = $('#daterange').val();
        if (daterange == "") {
            daterange = defaultDateRange;
        }
        var startDate = daterange.split(" - ")[0];
        var endDate = daterange.split(" - ")[1];

        $.ajax({
            url: "/chart/Chart/GetMileageUnit",
            data: {
                startDate: startDate,
                endDate: endDate,
                taskGroup: GetTaskGroup(),
                chartType: chartType,
                unitValue: unitValue,
            },
            type: "POST",
            dataType: "json",
            async: true,
            success: function (data) {
                $.each(data.data, function (key, value) {
                    xAxisdata.push(value.ChartValue);
                    seriesdata.push(Math.ceil(value.Mileage / 1000000.00 * 100.00) / 100);
                });

                var option3 = {
                    tooltip: {
                        trigger: 'item',
                        formatter: '{b}: {c}'
                    },
                    title: {
                        text: unitValue,
                        left: 10
                    },
                    xAxis: {
                        type: 'category',
                        data: xAxisdata
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        data: seriesdata,
                        type: 'bar'
                    }],
                    color: ['#489C9C', '#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'],
                };
                if (chart3 == undefined) {
                    chart3 = echarts.init(document.getElementById('chart3'));
                }
                chart3.setOption(option3);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(errorThrown, { icon: 2, title: MessageContainer.ErrorTitle });
            }
        });
    }

    function GetTaskGroup() {
        var taskGroups = "";
        $.each(formSelects.array('select'), function (key, value) {
            if (taskGroups == "") {
                taskGroups = value.val;
            }
            else {
                taskGroups = taskGroups + "," + value.val;
            }
        });
        return taskGroups;
    }
});