2
I have an MVC application and would like to clarify some questions on how to implement a chart with database data. I am using jQuery to get the values from the database, but did not understand how to take these values and generate a chart using Highcharts, I read a lot on the internet, but it was not clear how to get and put the values on the x and y axis.
For the values of X and Y axes, I am using the following:
Dictionary <decimal, decimal> dataResult = new Dictionary <decimal, decimal> ();
To query the database and assemble 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 switched 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 the search of the data:
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;
}
Could you be more specific? You want to understand how to use hightcharts or you want to fix something specific in your code?
– Erlon Charles
Dude, I think it would be so much easier for you to look at the demo and do it the same: http://www.highcharts.com/demo/
– Edgar Muniz Berlinck
I would like to know how to use hightcharts to generate the chart, but it seems that I am calling the chart before the function get the data.
– rysahara
no, he’s just assembling the chart options before the call and after it and adds the series and so yes calls the chart
– Paulo Lima