Dynamically Populate Pie Chart from Database using C# in ASP.Net

Last Reply one year ago By paulrajmca

Posted one year ago
  • hi developers,
     
    Dynamically generate pie chart in asp.net for students marks.
    am creating a web application. in that i need to create a pie chart for the student after he done exams.
    i am trying with google buit i did not get exact output , so if anyone know how to Dynamically generate pie chart please help me to i am done this task
     
    thanking you
    Paul.S
    man becomes what he thinks about 
     

Posted one year ago

thanks for all of your reply friends ,

finally i have done this task well with following codes , 

1.chart.aspx  just like below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="demo_Summa" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', { packages: ['corechart'] });
</script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'test.aspx/GetData',
data: '{}',
success:
function (response) {
drawVisualization(response.d);
}

});
})

function drawVisualization(dataValues) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Column Name');
data.addColumn('number', 'Column Value');

for (var i = 0; i < dataValues.length; i++) {
data.addRow([dataValues[i].ColumnName, dataValues[i].Value]);
}

new google.visualization.PieChart(document.getElementById('visualization')).
draw(data, { title: "Test Series Chart Example" });
}


</script>
</head>
<body>
<form id="form1" runat="server">

<center>
Test Series
<br />
Student Page
<div>
<asp:Label ID="lblTotal" runat="server" ForeColor="red"></asp:Label>
</div>
</center>
<div id="visualization" style="width: 1000px; height: 400px;"></div>
</form>
</body>
</html>

 2.C# code like this 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;

public partial class demo_Summa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = Connection.DBConn();
string totalQuestions = "select COUNT(*) from tbl_testseries where subject='Chemistry' and status='A'";
SqlCommand cmdtotalQuestions = new SqlCommand(totalQuestions, con);
int counttotalQuestions = (Int32)cmdtotalQuestions.ExecuteScalar();
lblTotal.Text = "Total No of Questions = " + " " + " " + counttotalQuestions.ToString();
}
[WebMethod]
public static List<Data> GetData()
{
SqlConnection con = Connection.DBConn();

string Correct = string.Empty;
string Answer = string.Empty;

//Total No of questions
string totalQuestions = "select COUNT(*) from tbl_testseries where subject='Chemistry' and status='A'";
SqlCommand cmdtotalQuestions = new SqlCommand(totalQuestions, con);
int counttotalQuestions = (Int32)cmdtotalQuestions.ExecuteScalar();

// incorrect answer
string incorrect = "select COUNT(*) from tbl_tsAnswer where userid='101' and correct='0' and status='A'";
SqlCommand cmdincorrect = new SqlCommand(incorrect, con);
int incorrectCount = (Int32)cmdincorrect.ExecuteScalar();

//correct
string correct = "select COUNT(*) from tbl_tsAnswer where userid='101' and correct='1' and status='A'";
SqlCommand cmdcorrect = new SqlCommand(correct, con);
int correctCount = (Int32)cmdcorrect.ExecuteScalar();

//strAttendedCount
string strAttendedCount = "select COUNT(*) from tbl_tsAnswer where userid='101' and status='A'";
SqlCommand cmdAttendedCount = new SqlCommand(strAttendedCount, con);
int countAttended = (Int32)cmdAttendedCount.ExecuteScalar();

int NotAttended = counttotalQuestions - countAttended;
List<Data> dataList = new List<Data>();
dataList.Add(new Data("No of Questions Not Attended", NotAttended));

dataList.Add(new Data("", 0));
dataList.Add(new Data("Questions Answered Incorrectly", incorrectCount));

dataList.Add(new Data("Questions Answered Correctly", correctCount));

return dataList;
}
}

public class Data
{
public string ColumnName = "";
public int Value = 0;

public Data(string columnName, int value)
{
ColumnName = columnName;
Value = value;
}
}

thats all , 

Paul.S

man becomes what he thinks about