Hi dharmendr sir,
I have to make google line chart populated dynamically from my database
My database name is testrecord have two table.
Table1 name test have two column.
Ipaddress name
12.44.21.09 asd
12.44.21.10 ghh
12.44.21.11 etee
12.44.21.12 qwr
12.44.21.13 wet
And so on
Table2 name is record include
Ipaddress pingtime ipstatus millisecond
12.44.21.09 10/11/2021 10:24:10 up 0
12.44.21.10 10/11/2021 10:24:10 up 26
12.44.21.11 10/11/2021 10:24:10 up 1
12.44.21.12 10/11/2021 10:24:10 down 0
12.44.21.13 10/11/2021 10:24:10 up 0
12.44.21.09 10/11/2021 11:34:10 up 300
12.44.21.10 10/11/2021 11:34:10 up 5000
I have to draw linechart against each ipaddress. I have to take pingtime on x-axis and millisecond on y-axis, but the condition should be if millisecond is zero(0) and ipstatus is up then line shouldn’t show it down.
I have to take ipaddress from table1 and pingtime , ipstatus and millisecond from table2, because in table2 there are multiple record against each ipaddress.
i have to show last 7 day record on chart.
Please help me to update my code according to above condition.
My code is attach below.
javascript
<script type="text/javascript" src="https://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() {
$.ajax({
type: "POST",
url: "graph.aspx/GetIpAddress",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
for (var i = 0; i < r.d.length; i++) {
MakeAjaxCall(r.d[i], $("#chart" + (i + 1))[0]);
}
},
failure: function (r) {
alert(r.responsetext);
},
error: function (r) {
alert(r.responsetext);
}
});
}
function MakeAjaxCall(ipAddress, dvElement) {
$.ajax({
type: "POST",
url: "graph.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', 'ipstatus');
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 maxValue = Math.max.apply(Math, r.d.map(function (x) { return x[1]; }));
var tick = [];
for (var i = 0; i <= (maxValue / 25) + 1; i++) {
tick.push(i * 25);
}
var chart = new google.visualization.LineChart(dvElement);
var options = {
title: 'ipaddress : ' + ipAddress,
width: 400,
height: 400,
bar: { groupWidth: "95%" },
legend: { position: "none" },
isStacked: true,
vAxis: {
viewWindow: {
min: 0,
max: maxValue
},
ticks: tick,
title: 'IPSTATUS',
textStyle:
{
fontStyle: "bold",
italic: true,
fontSize: 12,
color: '#0000FF'
},
titleTextStyle:
{
fontStyle: "normal",
fontWeight: 'bold',
italic: true,
fontSize: 12,
color: '#0000FF'
}
},
hAxis: {
title: 'Time',
titleTextStyle:
{
fontStyle: "normal",
fontWeight: 'bold',
italic: true,
color: '#0000FF'
}
}
};
chart.draw(data, options);
},
failure: function (r) {
alert(r.responseText);
},
error: function (r) {
alert(r.responseText);
}
});
}
</script>
c#
namespace NodesDetails
{
public partial class graph : System.Web.UI.Page
{
[WebMethod]
public static List<object> GetChartData(string ipAddress)
{
string query = string.Format("SELECT [pingtime],[ipstatus] FROM [dbo].[record] 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["ipstatus"] });
}
}
con.Close();
return chartData;
}
}
}
[WebMethod]
public static List<string> GetIpAddress()
{
string query = "SELECT DISTINCT ipaddress FROM [dbo].[record]";
string constr = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
List<string> ips = new List<string>();
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())
{
ips.Add(sdr["ipaddress"].ToString());
}
}
con.Close();
return ips;
}
}
}
}
}