I have an MVC application and would like to clarify some doubts on how to implement a chart with the database data. I'm using jQuery to get the values from the database, but I did not understand how to grab these values and generate a graph using Highcharts, I read a lot on the internet, but it was not clear how to get and put the values on the x axis and the y axis.
For the values of X and Y axes, I'm using the following:
Dictionary <decimal, decimal> dataResult = new Dictionary <decimal, decimal> ();
To query the database and mount the following:
foreach (var item in query)
{
dataResult.Add (Convert.ToDecimal (item.valorinicial), Convert.ToDecimal (item.preco));
}
To pass the value:
return Json (dataResult, JsonRequestBehavior.AllowGet);
To generate the Chart, I moved to:
var options = {
chart: {
renderTo: "container",
},
series: [{}]
};
$.getJSON("/GraficosLev/GetDadosByGraf", { ...parametros passados.... },
function (data) {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
ignoreHiddenSeries: false
},
xAxis: {
},
series: data
});
});
For data search:
Dictionary<decimal, decimal> dataResult = new Dictionary<decimal, decimal>();
public object GetValuesByGraf(int? idRod, int? idLev, string codLev, string kmIni, string kmFim)
{
string tpLev = (from l in db.TIPO_LEVA where l.id == idLev select l.tipo_levantamento).FirstOrDefault();
//Conta o numero de datas Selecionadas
string[] codLevant = codLev.Split(',');
foreach (string i in codLevant)
{
//pega os valores do id do codigo de levantmanto para pegar os levantamentos
int idCodLev = (from l in db.LEV1
where l.cod_levantamento == i
select l.id).FirstOrDefault();
if (tpLev =="I")
{
var query = (from iri in db.IRR10
where iri.cod_levantamento_id == idCodLev
select iri);
foreach (var item in query)
{
dataResult.Add(Convert.ToDecimal(item.inicial), Convert.ToDecimal(item.iri));
}
}
}
return dataResult;
}