i have run my google line chart and following error occure on my webpage:
error: Data column(s) for axis #0 cannot be of type string.
please someone can me help me.
my code is here:
Javascript:
<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() {
var options = {
title: 'ipaddress',
width: 600,
height: 400,
bar: { groupWidth: "95%" },
legend: { position: "none" },
isStacked: true
};
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.LineChart($("#chart")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
<div id="chart" style="width: 900px; height: 500px;">
</div>
C#
namespace WebApplication13
{
public partial class WebForm1 : System.Web.UI.Page
{
[WebMethod]
public static List<object> GetChartData()
{
string query = "SELECT [time],[average] FROM [dbo].[graph] WHERE ipaddress='192.168.15.167'";
string constr = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"time", "average"
});
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[]
{
sdr["time"], sdr["average"]
});
}
}
con.Close();
return chartData;
}
}
}
}
}
datatable is like this:
ipaddress time average pingtime
192.168.10.5 10:15 10 25/5/2021 10:15