Hi rani,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http, $window) {
$scope.PopulateChart = function (chartType) {
$http.post("Default.aspx/GetChartData", { headers: { 'Content-Type': 'application/json'} })
.then(function (response) {
$scope.ChartData = {
labels: response.data.d[0],
data: response.data.d[1],
color: response.data.d[2]
};
var ctx = document.getElementById("dvCanvas").getContext('2d');
var myChart = new Chart(ctx, {
type: chartType,
showInLegend: false,
data: {
datasets: [{
data: $scope.ChartData.data,
backgroundColor: $scope.ChartData.color
}],
labels: $scope.ChartData.labels
},
options: { responsive: false, legend: { display: false} }
});
});
}
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
<label for="ChartType">
<input type="radio" name="ChartType" ng-click="PopulateChart('pie')" />Pie
<input type="radio" name="ChartType" ng-click="PopulateChart('horizontalBar')" />Bar
<input type="radio" name="ChartType" ng-click="PopulateChart('bar')" />Column
</label>
<hr />
<canvas id="dvCanvas" height="200" width="500"></canvas>
</div>
Namespaces
C#
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
VB.Net
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Code
C#
[WebMethod]
public static List<object> GetChartData()
{
List<object> chartData = new List<object>();
List<string> labels = new List<string>();
List<int> data = new List<int>();
List<string> color = new List<string>();
string query = "SELECT ShipCity,COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry = 'Brazil' GROUP BY ShipCity";
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
foreach (DataRow row in dt.Rows)
{
labels.Add(row["ShipCity"].ToString());
data.Add(Convert.ToInt32(row["Total"]));
System.Threading.Thread.Sleep(50);
color.Add(String.Format("#{0:X6}", new Random().Next(0x1000000)));
}
chartData.Add(labels.ToArray());
chartData.Add(data.ToArray());
chartData.Add(color.ToArray());
return chartData;
}
VB.Net
<WebMethod()>
Public Shared Function GetChartData() As List(Of Object)
Dim chartData As List(Of Object) = New List(Of Object)()
Dim labels As List(Of String) = New List(Of String)()
Dim data As List(Of Integer) = New List(Of Integer)()
Dim color As List(Of String) = New List(Of String)()
Dim query As String = "SELECT ShipCity,COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry = 'Brazil' GROUP BY ShipCity"
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
End Using
For Each row As DataRow In dt.Rows
labels.Add(row("ShipCity").ToString())
data.Add(Convert.ToInt32(row("Total")))
System.Threading.Thread.Sleep(50)
color.Add(String.Format("#{0:X6}", New Random().[Next](&H1000000)))
Next
chartData.Add(labels.ToArray())
chartData.Add(data.ToArray())
chartData.Add(color.ToArray())
Return chartData
End Function
Screenshot