/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function PopulateBarChart(toDate,fromDate,distCode,reportSelection) {

    // get inputs
    var BucketWiseLoanOutstandingBean = new Object();
    /*BucketWiseLoanOutstandingBean.fromDate=$("[name='fromDate']").val();
    BucketWiseLoanOutstandingBean.toDate=$("[name='toDate']").val();
    BucketWiseLoanOutstandingBean.distCode=$("[name='distCode']").val();*/


    BucketWiseLoanOutstandingBean.fromDate=fromDate;
    BucketWiseLoanOutstandingBean.toDate=toDate;
    BucketWiseLoanOutstandingBean.distCode=distCode;
    BucketWiseLoanOutstandingBean.reportSelection=reportSelection;
    


    $.ajax({
        url: "/PacsApplication/JsonDashboardServlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(BucketWiseLoanOutstandingBean),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {

            
            BucketWiseLoanOutstandingBean = data;
            //alert(barChartData.labels[0]);
            //alert(barChartData.datasets[0].label);
            window.saket = data;
            
           // $('#canvas').remove();
            //$('#canvas-div').append(' <canvas id="canvas" onclick="javascript:return doClick(event);"></canvas>');



            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: BucketWiseLoanOutstandingBean,
                options: {
                    // Elements options apply to all of the options unless overridden in a dataset
                    // In this case, we are setting the border of each bar to be 2px wide and green
                    elements: {
                        rectangle: {
                            borderWidth: 2,
                            borderColor: 'rgb(0, 255, 0)',
                            borderSkipped: 'bottom'
                        }
                    },
                    responsive: true,
                    legend: {
                        position: 'top'
                    },
                    title: {
                        display: true,
                        text: 'LOAN OUTSTANDING BUCKETWISE'
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Bucket'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Loan Outstanding(In Rs Crore)'
                            }
                        }]
                    }
                }
            });

                        
        },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
        }
    });
}

function PopulateBarChart2(toDate,fromDate,distCode,reportSelection) {

    // get inputs
    var BucketWiseLoanOutstandingBean = new Object();
    /*BucketWiseLoanOutstandingBean.fromDate=$("[name='fromDate']").val();
    BucketWiseLoanOutstandingBean.toDate=$("[name='toDate']").val();
    BucketWiseLoanOutstandingBean.distCode=$("[name='distCode']").val();*/


    BucketWiseLoanOutstandingBean.fromDate=fromDate;
    BucketWiseLoanOutstandingBean.toDate=toDate;
    BucketWiseLoanOutstandingBean.distCode=distCode;
    BucketWiseLoanOutstandingBean.reportSelection=reportSelection;


        $.ajax({
            url: "/PacsApplication/JsonDashboardServlet",
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(BucketWiseLoanOutstandingBean),
            contentType: 'application/json',
            mimeType: 'application/json',

            success: function (data) {


                BucketWiseLoanOutstandingBean = data;
                //alert(barChartData.labels[0]);
                //alert(barChartData.datasets[0].label);
                window.saket = data;
                var ctx = document.getElementById("canvas").getContext("2d");
                window.myBar = new Chart(ctx, {
                    type: 'bar',
                    data: BucketWiseLoanOutstandingBean,
                    options: {
                        // Elements options apply to all of the options unless overridden in a dataset
                        // In this case, we are setting the border of each bar to be 2px wide and green
                        elements: {
                            rectangle: {
                                borderWidth: 2,
                                borderColor: 'rgb(0, 255, 0)',
                                borderSkipped: 'bottom'
                            }
                        },
                        responsive: true,
                        legend: {
                            position: 'top'
                        },
                        title: {
                            display: true,
                            text: 'CROP-WISE SNAPSHOT OF LOANS'
                        },
                        scales: {
                            xAxes: [{
                                display: true,
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Year'
                                }
                            }],
                            yAxes: [{
                                display: true,
                                scaleLabel: {
                                    display: true,
                                    labelString: 'No. of Loans'
                                }
                            }]
                        }
                    }
                });

            },
            error:function(data,status,er) {
                alert("error: "+data+" status: "+status+" er:"+er);
            }
        });
    }
function PopulateLineChart(toDate,fromDate,distCode,reportSelection) {

    // get inputs
    var BucketWiseLoanOutstandingBean = new Object();
    /*BucketWiseLoanOutstandingBean.fromDate=$("[name='fromDate']").val();
    BucketWiseLoanOutstandingBean.toDate=$("[name='toDate']").val();
    BucketWiseLoanOutstandingBean.distCode=$("[name='distCode']").val();*/


    BucketWiseLoanOutstandingBean.fromDate=fromDate;
    BucketWiseLoanOutstandingBean.toDate=toDate;
    BucketWiseLoanOutstandingBean.distCode=distCode;
    BucketWiseLoanOutstandingBean.reportSelection=reportSelection;

    $.ajax({
        url:  "/PacsApplication/JsonDashboardServlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(BucketWiseLoanOutstandingBean),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {
            BucketWiseLoanOutstandingBean = data;
           // alert(BucketWiseLoanOutstandingBean.labels[0]);
           // alert(BucketWiseLoanOutstandingBean.datasets[0].label);
            window.saket = data;
            var randomScalingFactor = function() {
                return Math.round(Math.random() * 100);
            //return 0;
            };
            var randomColorFactor = function() {
                return Math.round(Math.random() * 255);
            };
            var randomColor = function(opacity) {
                return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
            };

            var config = {
                type: 'line',
                data: BucketWiseLoanOutstandingBean,
                options: {
                    responsive: true,
                    title:{
                        display:true,
                        text:'Crop-Wise Loan Demand'
                    },
                    tooltips: {
                        mode: 'label',
                        callbacks: {
                    // beforeTitle: function() {
                    //     return '...beforeTitle';
                    // },
                    // afterTitle: function() {
                    //     return '...afterTitle';
                    // },
                    // beforeBody: function() {
                    //     return '...beforeBody';
                    // },
                    // afterBody: function() {
                    //     return '...afterBody';
                    // },
                    // beforeFooter: function() {
                    //     return '...beforeFooter';
                    // },
                    // footer: function() {
                    //     return 'Footer';
                    // },
                    // afterFooter: function() {
                    //     return '...afterFooter';
                    // },
                    }
                    },
                    hover: {
                        mode: 'dataset'
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Year'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'No. of Loans'
                            },
                            ticks: {
                                suggestedMin: -10,
                                suggestedMax: 250
                            }
                        }]
                    }
                }
            };

            $.each(config.data.datasets, function(i, dataset) {
                dataset.borderColor = randomColor(0.4);
                dataset.backgroundColor = randomColor(0.5);
                dataset.pointBorderColor = randomColor(0.7);
                dataset.pointBackgroundColor = randomColor(0.5);
                dataset.pointBorderWidth = 1;
            })

            var ctx = document.getElementById("canvas").getContext("2d");
            window.myLine = new Chart(ctx, config);

        },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
        }
    });
}
function PopulateLineChart2(toDate,fromDate,distCode,reportSelection) {

    // get inputs
    var BucketWiseLoanOutstandingBean = new Object();
    /*BucketWiseLoanOutstandingBean.fromDate=$("[name='fromDate']").val();
    BucketWiseLoanOutstandingBean.toDate=$("[name='toDate']").val();
    BucketWiseLoanOutstandingBean.distCode=$("[name='distCode']").val();*/


    BucketWiseLoanOutstandingBean.fromDate=fromDate;
    BucketWiseLoanOutstandingBean.toDate=toDate;
    BucketWiseLoanOutstandingBean.distCode=distCode;
    BucketWiseLoanOutstandingBean.reportSelection=reportSelection;

    $.ajax({
        url:  "/PacsApplication/JsonDashboardServlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(BucketWiseLoanOutstandingBean),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {
            BucketWiseLoanOutstandingBean = data;
           // alert(BucketWiseLoanOutstandingBean.labels[0]);
           // alert(BucketWiseLoanOutstandingBean.datasets[0].label);
            window.saket = data;
            var randomScalingFactor = function() {
                return Math.round(Math.random() * 100);
            //return 0;
            };
            var randomColorFactor = function() {
                return Math.round(Math.random() * 255);
            };
            var randomColor = function(opacity) {
                return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
            };

            var config = {
                type: 'line',
                data: BucketWiseLoanOutstandingBean,
                options: {
                    responsive: true,
                    title:{
                        display:true,
                        text:'Crop-Wise Loan Recovery'
                    },
                    tooltips: {
                        mode: 'label',
                        callbacks: {
                    // beforeTitle: function() {
                    //     return '...beforeTitle';
                    // },
                    // afterTitle: function() {
                    //     return '...afterTitle';
                    // },
                    // beforeBody: function() {
                    //     return '...beforeBody';
                    // },
                    // afterBody: function() {
                    //     return '...afterBody';
                    // },
                    // beforeFooter: function() {
                    //     return '...beforeFooter';
                    // },
                    // footer: function() {
                    //     return 'Footer';
                    // },
                    // afterFooter: function() {
                    //     return '...afterFooter';
                    // },
                    }
                    },
                    hover: {
                        mode: 'dataset'
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Year'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'No. of Loans'
                            },
                            ticks: {
                                suggestedMin: -10,
                                suggestedMax: 250
                            }
                        }]
                    }
                }
            };

            $.each(config.data.datasets, function(i, dataset) {
                dataset.borderColor = randomColor(0.4);
                dataset.backgroundColor = randomColor(0.5);
                dataset.pointBorderColor = randomColor(0.7);
                dataset.pointBackgroundColor = randomColor(0.5);
                dataset.pointBorderWidth = 1;
            })

            var ctx = document.getElementById("canvas").getContext("2d");
            window.myLine = new Chart(ctx, config);

        },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
        }
    });
}

function PopulateBarChart3(toDate,fromDate,distCode,reportSelection) {

    // get inputs
    var BucketWiseLoanOutstandingBean = new Object();
    /*BucketWiseLoanOutstandingBean.fromDate=$("[name='fromDate']").val();
    BucketWiseLoanOutstandingBean.toDate=$("[name='toDate']").val();
    BucketWiseLoanOutstandingBean.distCode=$("[name='distCode']").val();*/


    BucketWiseLoanOutstandingBean.fromDate=fromDate;
    BucketWiseLoanOutstandingBean.toDate=toDate;
    BucketWiseLoanOutstandingBean.distCode=distCode;
    BucketWiseLoanOutstandingBean.reportSelection=reportSelection;


    $.ajax({
        url: "/PacsApplication/JsonDashboardServlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(BucketWiseLoanOutstandingBean),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {


            BucketWiseLoanOutstandingBean = data;
            //alert(barChartData.labels[0]);
            //alert(barChartData.datasets[0].label);
            window.saket = data;

           // $('#canvas').remove();
            //$('#canvas-div').append(' <canvas id="canvas" onclick="javascript:return doClick(event);"></canvas>');



            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: BucketWiseLoanOutstandingBean,
                options: {
                    // Elements options apply to all of the options unless overridden in a dataset
                    // In this case, we are setting the border of each bar to be 2px wide and green
                    elements: {
                        rectangle: {
                            borderWidth: 2,
                            borderColor: 'rgb(0, 255, 0)',
                            borderSkipped: 'bottom'
                        }
                    },
                    responsive: true,
                    legend: {
                        position: 'top'
                    },
                    title: {
                        display: true,
                        text: 'LOAN DISBURSED VS. LOAN RECOVERED'
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Crop Name'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'No. of loans'
                            }
                        }]
                    }
                }
            });


        },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
        }
    });
}
function PopulatePieChart(toDate,fromDate,distCode,reportSelection){

    var BucketWiseLoanOutstandingBean = new Object();



    BucketWiseLoanOutstandingBean.fromDate=fromDate;
    BucketWiseLoanOutstandingBean.toDate=toDate;
    BucketWiseLoanOutstandingBean.distCode=distCode;
    BucketWiseLoanOutstandingBean.reportSelection=reportSelection;

    $.ajax({
        url: "/PacsApplication/JsonDashboardServlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(BucketWiseLoanOutstandingBean),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {
            BucketWiseLoanOutstandingBean = data;

            var pielabels = BucketWiseLoanOutstandingBean.labels;
            var piedata1 = BucketWiseLoanOutstandingBean.datasets[0].data;
            var piedata2 = BucketWiseLoanOutstandingBean.datasets[1].data;
            var pieColor =new Array(piedata1.length);


            window.saket = data;
            var randomScalingFactor = function() {
                return Math.round(Math.random() * 100);
            };
            var randomColorFactor = function() {
                return Math.round(Math.random() * 255);
            };
            var randomColor = function(opacity) {
                return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
            };

            for(var i=0; i < pieColor.length; i++){
                pieColor[i]=randomColor(0.7);
            }
            var config1 = {
                type: 'pie',
                data: {
                    datasets: [{
                            data: piedata1,
                            backgroundColor: pieColor
                        }],
                    labels: pielabels
                },

                options: {
                    responsive: true,
                    title: {
                        display: true,
                        text: 'DCCB LOAN RECOVERED'
                    }
                }
            };
            var config2 = {
                type: 'pie',
                data: {
                    datasets: [{
                            data: piedata2,
                            backgroundColor: pieColor
                        }],
                    labels: pielabels
                },
                options: {
                    responsive: true,
                    title: {
                        display: true,
                        text: 'DCCB LOAN OUTSTANDING'
                    }
                }
            };

            var ctx1 = document.getElementById("canvas").getContext("2d");
            var ctx2 = document.getElementById("canvas1").getContext("2d");
            window.myPie1 = new Chart(ctx1, config1);
            window.myPie2 = new Chart(ctx2, config2);
        },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
        }
    });
}

