The following is my SQL Server Data Table with the data I will show in the chart.

SQL server Data Table

Image 1.

Here I will show the total number of users by their city (Address). The following is my table in design mode.

Table in design mode

Image 2.

The following is the script of my table:

  1. CREATE TABLE [dbo].[Employee](
  2. [CompanyName] [varchar](50) NULL,
  3. [EmployeeCode] [int] NOT NULL,
  4. [EmployeeSupervisorCode] [int] NULL,
  5. [EmployeeName] [varchar](50) NULL,
  6. [ProjectName] [varchar](50) NULL,
  7. [JoiningDate] [datetime] NULL,
  8. [Experience] [varchar](50) NULL,
  9. [Mobile] [varchar](15) NULL,
  10. [Address] [varchar](50) NULL,
  11. [CreatedDate] [datetime] NULL,
  12. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  13. (
  14. [EmployeeCode] ASC
  15. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  16. ) ON [PRIMARY]
  17. GO
  18. SET ANSI_PADDING OFF
  19. GO
The following is the aspx:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jQueryGoogleChart.aspx.cs" Inherits="jQueryChartApplication.jQueryGoogleChart" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
  7. <script src="http://www.google.com/jsapi" type="text/javascript"></script>
  8. <script type="text/javascript">
  9. google.load('visualization', '1', { packages: ['corechart'] });
  10. </script>
  11. <script type="text/javascript">
  12. $(function () {
  13. $.ajax({
  14. type: 'POST',
  15. dataType: 'json',
  16. contentType: 'application/json',
  17. url: 'jQueryGoogleChart.aspx/GetChartData',
  18. data: '{}',
  19. success:
  20. function (response) {
  21. drawchart(response.d);
  22. },
  23. error: function () {
  24. alert("Error loading data!");
  25. }
  26. });
  27. })
  28. function drawchart(dataValues) {
  29. var data = new google.visualization.DataTable();
  30. data.addColumn('string', 'Column Name');
  31. data.addColumn('number', 'Column Value');
  32. for (var i = 0; i < dataValues.length; i++) {
  33. data.addRow([dataValues[i].EmployeeCity, dataValues[i].Total]);
  34. }
  35. new google.visualization.PieChart(document.getElementById('myChartDiv')).
  36. draw(data, { title: "Google Chart in Asp.net using jQuery" });
  37. }
  38. </script>
  39. </head>
  40. <body>
  41. <form id="form1" runat="server">
  42. <div id="myChartDiv" style="width: 700px; height: 450px;">
  43. </div>
  44. </form>
  45. </body>
  46. </html>
The following is the aspx.cs code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Web.Services;
  10. namespace jQueryChartApplication
  11. {
  12. public partial class jQueryGoogleChart : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. }
  17. [WebMethod]
  18. public static List<employeeDetails> GetChartData()
  19. {
  20. DataTable dt = new DataTable();
  21. using (SqlConnection con = new SqlConnection(@"Data Source=MyPC\SqlServer2k8;Integrated Security=true;Initial Catalog=Test"))
  22. {
  23. con.Open();
  24. SqlCommand cmd = new SqlCommand("SELECT Address as Name, COUNT(EMPLOYEECODE) AS Total FROM Employee GROUP BY Address", con);
  25. SqlDataAdapter da = new SqlDataAdapter(cmd);
  26. da.Fill(dt);
  27. con.Close();
  28. }
  29. List<employeeDetails> dataList = new List<employeeDetails>();
  30. foreach (DataRow dtrow in dt.Rows)
  31. {
  32. employeeDetails details = new employeeDetails();
  33. details.EmployeeCity = dtrow[0].ToString();
  34. details.Total = Convert.ToInt32(dtrow[1]);
  35. dataList.Add(details);
  36. }
  37. return dataList;
  38. }
  39. public class employeeDetails
  40. {
  41. public string EmployeeCity { get; set; }
  42. public int Total { get; set; }
  43. }
  44. }
  45. }
Now run the application.

google chart
Image 3.

Now hover the mouse on the chart.

pia chart
Image 4.

Showing Google Chart
Image 5.