google.charts.load('current', { packages: ['corechart', 'bar'] });
google.charts.setOnLoadCallback(drawBasic);

var _selectedchart = sessionStorage.getItem("CurrentChartSelection");

if (_selectedchart == "Itemwise")
{
    ItemHeader();
}
//google.charts.load('current', { packages: ['corechart', 'bar'] });
//google.charts.setOnLoadCallback(drawBasic);
function ItemHeader() 
{
  
    
    $.ajax({
        type: "POST",
        url: "ConsumerBehaviourAnalysis.aspx/Web_ItemWise_Qty_Count",
       /* data: "{DB:'" + DB + "'}",*/
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            //alert(response.d);
            alert("Error..1");

        },
        error: function (response) {


            alert("Error..2");
            alert(response);
        }
    });
}

function OnSuccess(response) {
    //  var jsonResult = JSON.stringify(response.d)
     alert(jsonResult);
    var jsonResult = JSON.parse(response.d);
    var jsonCount = jsonResult.length;

    console.log("Test");
    for (var n = 0; n < jsonCount; n++) {
        var _Name = jsonResult[n].Name;
        var _Qty = jsonResult[n].Qty;

    }
    sessionStorage.setItem("JsonResult", JSON.stringify(jsonResult));
    // loop the jsonresult Here 
    drawBasic();

}


// Main Chat linked to the child chart 
function drawBasic() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Qty');

    var Rjsonvalue = JSON.parse(sessionStorage.getItem("JsonResult"));
    Rjsonvalue.forEach(function (item) {
        data.addRow([item.Name, item.Qty]);
    });

   
    var options = {
        title: 'ItemWise Qty',
        hAxis: {
            title: 'Name',
            //format: 'MMM yyyy',
            /*viewWindow: {
              min: [7, 30, 0],
              max: [19, 30, 0]
            }*/
        },
        vAxis: {
            title: 'Qty'
        }
    };
    
    // Calling All selectiion Methods Here  On Page Load..
    // Totoal Sales Amount Wise Total By All category In All Branches
    // google.charts.setOnLoadCallback(byBranchAll());
    // Total Qty in  All category In All Branches
    // google.charts.setOnLoadCallback(byQtyWiseCategoryALL());
    // Inventory Wise total in All  Branches..
    // google.charts.setOnLoadCallback(byInventoryTotalALL());
    // This i Table data which is topQty Categoryname 
  
    
    function selectHandler() {
        var selection = chart.getSelection();
        if (selection.length > 0)
        {
            var mydata = data.getValue(selection[0].row, 0);
           //alert(selection[0].row);  This show data row index value 
           //alert(mydata);
           sessionStorage.setItem("Branchid", Branchid);
           //google.charts.setOnLoadCallback(byBranch(Branchid));
            
        }
        else 
        {
           

        }

    }
    

    // Loading the chart 
    var chart = new google.visualization.BarChart(
    document.getElementById('chart_div'));
    google.visualization.events.addListener(chart, 'select', selectHandler);
    chart.draw(data, options);
   
}

