Hi sofia,
Check this example. Now please take its reference and correct your code.
HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
MakeAjaxCall("192.168.15.167", $("#chart1")[0])
MakeAjaxCall("192.168.15.168", $("#chart2")[0])
}
function MakeAjaxCall(ipAddress, dvElement) {
$.ajax({
type: "POST",
url: "Default.aspx/GetChartData",
data: '{ipAddress:"' + ipAddress + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = new google.visualization.DataTable();
data.addColumn('date', 'pingtime');
data.addColumn('number', 'average');
for (var i = 0; i < r.d.length; i++) {
data.addRow([new Date(parseInt(r.d[i][0].substr(6))), parseInt(r.d[i][1])]);
}
var chart = new google.visualization.LineChart(dvElement);
var options = {
title: 'ipaddress : ' + ipAddress,
width: 600,
height: 400,
bar: { groupWidth: "95%" },
legend: { position: "none" },
isStacked: true
};
chart.draw(data, options);
},
failure: function (r) {
alert(r.responsetext);
},
error: function (r) {
alert(r.responsetext);
}
});
}
</script>
<div id="chart1" style="width: 900px; height: 300px;"></div>
<div id="chart2" style="width: 900px; height: 300px;"></div>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Code
C#
[WebMethod]
public static List<object> GetChartData(string ipAddress)
{
string query = string.Format("SELECT [pingtime],[average] FROM [dbo].[graph] WHERE ipaddress='{0}' AND CONVERT(DATE,pingtime,103) BETWEEN CONVERT(DATE,DATEADD(DAY, -7, GETDATE()),103) AND CONVERT(DATE,GETDATE(),103) ORDER BY CONVERT(DATETIME,pingtime,103) ASC", ipAddress);
string constr = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
List<object> chartData = new List<object>();
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[] { Convert.ToDateTime(sdr["pingtime"]), sdr["average"] });
}
}
con.Close();
return chartData;
}
}
}
VB.Net
<WebMethod>
Public Shared Function GetChartData(ByVal ipAddress As String) As List(Of Object)
Dim query As String = String.Format("SELECT [pingtime],[average] FROM [dbo].[graph] WHERE ipaddress='{0}' AND CONVERT(DATE,pingtime,103) BETWEEN CONVERT(DATE,DATEADD(DAY, -7, GETDATE()),103) AND CONVERT(DATE,GETDATE(),103) ORDER BY CONVERT(DATETIME,pingtime,103) ASC", ipAddress)
Dim constr As String = ConfigurationManager.ConnectionStrings("dbcs").ConnectionString
Dim chartData As List(Of Object) = New List(Of Object)()
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
chartData.Add(New Object() {Convert.ToDateTime(sdr("pingtime")), sdr("average")})
End While
End Using
con.Close()
Return chartData
End Using
End Using
End Function
Screenshot