5
Answers

Dynamically generate pie chart in asp.net for students marks

Photo of Paul Rajs

Paul Rajs

7y
2.8k
1
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 
 

Answers (5)

1
Photo of Raja T
329 5.4k 113.1k 7y
Hi Paul, Please follow below steps
 
1. you need required dataset/datatable from database.
2. there are more third party js exists in google for charts.
3. I have prepared to chartjs. please check more details about the chartjs...
https://www.sitepoint.com/introduction-chart-js-2-0-six-examples/
4. you can identify returns the data from database that results only showing in chart. 
 
 
 
Accepted
1
Photo of Raja T
329 5.4k 113.1k 7y
Hi Paul, Most welcome Pauly... Happy coding :)
1
Photo of Paul Rajs
NA 641 155.7k 7y
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
1
Photo of Raja T
329 5.4k 113.1k 7y
how to you get the data from database using web method /web api..
 
if you can use easy web method /web api get data and bind the data in AJAX. Because im recently working on that using Chart JS library...
 
 
1
Photo of Paul Rajs
NA 641 155.7k 7y
thanks raj , you are right , but i want to bind the records from database 
 
will you suggest me anyother option