Good afternoon, I'm trying to implement a chart that shows me the total number of reservations made in a given month !!
I already have this:
Theproblemisthatthey-axisisdisplayingtheReservationIDinsteadofacountoftotalreservations.InthisexampleIhavetworeservationsmadeinApril,thedesiredonewouldbeontheyaxishaving1and2andnotthereserveIDitself.Cansomeonehelpme?
Controller:
[HttpPost]publicJsonResultHomeChart(){List<Reserva>data=newList<Reserva>();using(HotelEntitiesdc=newHotelEntities()){data=dc.Reserva.ToList();}varchartData=newobject[data.Count+1];chartData[0]=newobject[]{"DataEntrada",
"ID_Reserva"
};
int j = 0;
foreach (var i in data)
{
j++;
chartData[j] = new object[] { i.DataEntrada.ToString("dd/MM/yyyy"), i.ID_Reserva };
}
return Json(chartData);
}
View:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><scripttype="text/javascript" src="https://www.google.com/jsapi"></script><scripttype="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
$.ajax({
type: "POST",
url: "/Charts/HomeChart",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r);
var options = {
title: "",
pointSize: 5
};
var chart = new google.visualization.LineChart($("#chart1")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
If my role is this (alternative and maybe more intuitive) ...
[HttpPost]
public JsonResult HomeChart()
{
string query = "SELECT DataEntrada,ID_Reserva, COUNT(ID_Reserva) TotalReservas From Reserva Group by DataEntrada, ID_Reserva";
var constr = ConfigurationManager.ConnectionStrings["Hotel"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"DataEntrada", "ID_Reserva"
});
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[]
{
((DateTime)sdr["DataEntrada"]).ToString("MMMM"), sdr["TotalReservas"]
});
}
}
con.Close();
}
}
return Json(chartData);
}
The Reservation ID counter does not work, nor does the id identify. In this case I am in the reservation of ID 7 and in the image this is the ID 1 -.-