
$(function () {
    if (location.search.indexOf("ana=1")!=-1)
    loadAna(startDraw);

});
var __xmlDoc = null;
function loadAna(action) {
    //if (__keys == null) {setTimeout(function () { loadAna(action) }, 500);return; };
    var pathName = location.pathname;
    var lastDot = pathName.lastIndexOf(".");

    var kzm = pathName.substr(pathName.lastIndexOf(".") + 1);
    var keyFile = location.protocol + "//" + location.host + pathName.substring(0, lastDot) + ".a." + kzm + location.search;
    //alert(keyFile)
    $.ajax({
        type: "get",
        url: keyFile,
        dataType: "xml",
        processData: true, global: false,
        cache: true,
        async: true,
        success: function (data, textStatus) {
            __xmlDoc = data;
            if ($.isFunction(action)) {
                action();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
            // 只有一个会包含信息

            alert("试题分析数据可能不存在！"); 
        }
    });

};

function startDraw() {
    if (__xmlDoc == null) return;
    if (__keys == null) { setTimeout(function () { startDraw() }, 500); return; };
    var analyse = __xmlDoc.selectSingleNode("/chart");
    var exmineeCount = parseInt(analyse.getAttribute("examineeCount"));
    
    var tests = analyse.selectNodes("test");
    for (var i = 0; i < tests.length; i++) {
        var x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
        var y = [exmineeCount, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        var testNode = tests[i];
        var testId = testNode.getAttribute("id");
        var examineeNodes = testNode.selectNodes("category");
        var d = [];
        var keyNode = __keys.getKeyById(testId);

        if (keyNode != null) {
            var fullScore = parseFloat(keyNode.getScore());
            //var hasZero = false;
           
            for (var j = 0; j < examineeNodes.length; j++) {
                var examineeNode = examineeNodes[j];
                var score = parseFloat(examineeNode.getAttribute("name"));
                var count = parseInt(examineeNode.text);
                //if (score == 0) hasZero = true;
                if (score >= fullScore) y[10] += count; else {
                    if (score >= fullScore * x[9]) y[9] += count; else {
                        if (score >= fullScore * x[8]) y[8] += count; else {
                            if (score >= fullScore * x[7]) y[7] += count; else {
                                if (score >= fullScore * x[6]) y[6] += count; else {
                                    if (score >= fullScore * x[5]) y[5] += count; else {
                                        if (score >= fullScore * x[4]) y[4] += count; else {
                                            if (score >= fullScore * x[3]) y[3] += count; else {
                                                if (score >= fullScore * x[2]) y[2] += count; else {
                                                    if (score >= fullScore * x[1]) y[1] += count; else y[0] += count;
                                                };
                                            
                                            };


                                        };
                                    
                                    };


                                };
                            
                            };


                        };
                  
                  
                     };


                };
                y[0] -= count
                //d[d.length] = [score, count]
            };

            //if (!hasZero) d[d.length] = [0, 0]
            chart(testId,fullScore,exmineeCount, x,y);
        
        };

    };

    //其他未有分析数据的，填充
    $("div:hidden[id^=chart_]").each(function (i) {
        var testId = this.id.substr(6);

        chart(testId, __keys.getKeyById(testId).getScore(), exmineeCount, x, y);
    });
};
var h_chart = null;
function chart(testId, maxX, maxY, x, y) {

    var g = $("#chart_" + testId);
    if (g.length == 0) return;
    g.css("display", "block");
    //var x = []; var y = [];
    //for (var i = data.length-1; i>=0 ; i--) {
    //    var d = data[i];
     //   x[x.length] = d[0]; y[y.length] = d[1];
    //};
    var d = [];
    for (var i = 0; i < x.length; i++) {
        var b = { name: x[i].toString(), y: y[i], color: '#FF0000' };
        b.name = ">=" + (x[i] * maxX).toFixed(2) + "分";
        switch(x[i]){
            case 1:
                b.color = "#00FF99"; b.name = "满分";
                break;
            case 0.9:
                b.color = "#00FF66"; 
                break;
            case 0.8:
                b.color = "#33FF00";
                break;
            case 0.7:
                b.color = "#99FF00";
                break;
            case 0.6:
                b.color = "#CCFF00";
                break;
            case 0.5:
                b.color = "#FFFF00";
                break;
            case 0.4:
                b.color = "#FFcc00";
                break;
            case 0.3:
                b.color = "#FF9900";
                break;
            case 0.2:
                b.color = "#FF6600";
                break;
            case 0.1:
                b.color = "#FF3300";
                break;
            case 0:
                b.color = "#FF0000"; b.name = "<" + (0.1 * maxX).toFixed(2) + "分";
                break;

        };
        d[d.length] = b;
    //var colors = ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#00FFCC', '#', '#'];" 

    };
  h_chart = new Highcharts.Chart({
        chart: {
            renderTo: "chart_" + testId,
           
            margin: [50, 200, 60, 170]

        },
        
        title: {
            text: ''
        },
        subtitle: {
            text: '分数段人数统计'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    formatter: function () {
                        if (this.y / maxY > 0.1) return this.point.name;
                    },
                    color: 'white',
                    style: {
                        font: '13px Trebuchet MS, Verdana, sans-serif'
                    }
                }
            }
        },
        legend: {
            layout: 'vertical',
            style: {
                left: 'auto',
                bottom: 'auto',
                right: '50px',
                top: '100px'
            }
        },

        tooltip: {
            formatter: function () {
                return this.point.name+'区间'+this.y + '名考生';
            }
        },
        
        credits: {
            enabled: false
        },
        series: [{
            type: 'pie',
            name: '人数',
            data: d
        }]
    });



};

