instead of serializing list item , create one web method and send a ajax call through your script function
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DashboardObjectiveChart.ascx.cs" Inherits="ActusAppraisalSystem.User.DashboardControls.DashboardObjectiveChart" %>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
#legend {
float: right;
margin-top: 100px;
margin-right: 50px;
}
</style>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
pieHole: 0.5,
colors: ['#F3660E', '#14C902', '#AF7BCD'],
legend: 'none'
};
$.ajax({
type: "POST",
url: "Home.aspx/GetChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart")[0]);
chart.draw(data, options);
var total = 0;
for (var i = 0; i < data.getNumberOfRows() ; i++) {
total += data.getValue(i, 1);
}
var legend = document.getElementById("legend");
var legItem = [];
var colors = ['#F3660E', '#14C902', '#AF7BCD'];
for (var i = 0; i < data.getNumberOfRows() ; i++) {
var label = data.getValue(i, 0);
var value = data.getValue(i, 1);
var percent = Number(100 * value / total).toFixed(1);
legItem[i] = document.createElement('li');
legItem[i].id = 'legend_' + data.getValue(i, 0);
legItem[i].innerHTML = '<div class="legendMarker"> <span class="legendClass" id="' + label + '" style="color:' + colors[i] + ';">' + label + ': ' + '(' + value + ')</span></div>';
legend.appendChild(legItem[i]);
}
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
<script type="text/javascript">
$(document).ready(function () {
$('.legendClass').live('click', function () {
alert("I am " + this.id + " Here");
});
});
</script>
<div class="Dashboard_FrameWrapper" style="height: 500px; overflow: auto;">
<div class="Dashboard_BlockWrapper_FrameHeader">
<div class="dashboard_grid_Header">
Employee Objectives
</div>
</div>
<div class="dashboard_grid">
<div id="chart" style="height: 500px; float: left">
</div>
<div id="legend" style="float: right"></div>
</div>
</div>
public static List<object> GetChartData()
{
string query = "usp_GetObjectivesDetailsForGoogleChart";
string constr = ApConfig.ConString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Objective_Status", "Percentage"
});
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["Objective_Status"].ToString(), sdr["Percentage"]
});
}
}
con.Close();
return chartData;
}
}
}