0
I have a Google Charts chart that works perfectly bringing the database data through ajax, but I tried to do the same with Charts.js and I can’t. I would like to know how I put real data on the Charts.js chart by taking advantage of my already created Function get to the other chart.
// ESTA E A FUNCTION DO GRAFICO GOOGLE CHARTS QUE FUNCIONA, COM AS "CONST" TRAZENDO OS DADOS
async function drawChart15() {
const qtndVitimas = await GetQuantidadeVitimas()
const qtndAutores = await GetQuantidadeAutor()
const container = document.querySelector('#graficoGoogle16')
const data = new google.visualization.arrayToDataTable([
['Legendas', 'Abril', 'Agosto', "Setembro", "Novembro", "Dezembro"],
[qtndVitimas, qtndVitimas, qtndAutores, qtndVitimas, qtndAutores, qtndVitimas],
])
const options = {
height: 250,
width: 500
}
const chart = new google.visualization.ColumnChart(container)
chart.draw(data, options)
}
Now I will show the two Function that I created to pull the data through ajax.
// AMBAS FUNCTION ESTÃO TRAZENDO OS DADOS CORRETAMENTE
async function GetQuantidadeVitimas() {
let qtnd_vitimas = 0
const url = `/Vitima/AjaxQuantidadeVitimas`
try {
const resposta = await fetch(url);
const resultado = await resposta.json();
qtnd_vitimas = resultado
} catch (e) {
console.error(e);
}
return qtnd_vitimas;
}
async function GetQuantidadeAutor() {
let qntd_Autores = 0
const url = `/Autor/AjaxQuantidadeAutores`
try {
const resposta = await fetch(url);
const resultado = await resposta.json();
qntd_Autores = resultado
} catch (e) {
console.error(e);
}
return qntd_Autores;
}
These two functions return the number of victims and authors registered in the database. I will show the step by step of Getnumerovitimas().
//aqui pegando os dados do banco
public int GetNumeroVitimas()
{
using (IDbConnection cn = ConnectionAnaliseCriminal)
{
try
{
cn.Open();
string query = "SELECT COUNT(TP_VITIMA.COD_VITIMA)"
+ " FROM [dbo].[TP_VITIMA] "
+ " WHERE TP_VITIMA.[DT_EXCLUSAO_LOGICA] IS NULL ";
return cn.Query<int>(query).SingleOrDefault();
}
finally
{
cn.Close();
}
}
}
//Passa pela ControllerBase
protected int NumeroVitimas(int codFormulario) => new VitimaDados().GetNumeroVitimas(GetCodCrimeByCodFormulario(codFormulario));
// dps a "VitimaController onde e add a viewbag e o ajax
ViewBag.NumeroVitimas = NumeroVitimas(codFormulario);
[HttpGet]
public JsonResult AjaxQuantidadeVitimas()
{
return Json(new VitimaDados().GetNumeroVitimas());
}
Now I wanted to do the same thing with the Charts.js chart to show the actual data coming from the database but I’m not getting it. Does anyone know how to solve?
// SCRIPT PURO DO CHARTS.JS PORÉM EU QUERO INSERIR NA "DATA" OS MEUS DADOS VINDO DO BANCO PELO AJAX, ASSIM COMO FIZ NO OUTRO GRÁFICO, PORÉM NÃO ESTOU CONSEGUINDO
var ctx = document.getElementById('myChart');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: 'titulo grafico',
data: [1, 2, 3, 4, 5, 6],
borderColor: 'rgba(77,166,253,0.85)',
backgroundColor: 'red',
},
]
},
});
<html>
<head>
<title></title>
</head>
<body>
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</body>
</html>
No matter the google Harts for this question... you have to submit what return
/Vitima/AjaxQuantidadeVitimas
and/Autor/AjaxQuantidadeAutores
. And after all your backend is php or c#?– Leandro Angelo
All in c# and they return the number of registered authors and victims.
– user198468
So why the php tag on the question? Present their feedback and the action with the model in the backend
– Leandro Angelo
Okay, I added the feedback of one, more and the same procedure of the other.
– user198468
It wouldn’t just be in the method where you create Chart to previously call the collection functions?
– M. Bertolazo
@M.Bertolazo can be, more as I would do it, can give an example in the script?
– user198468
Wouldn’t that solve? date: [Getquantityavoidances(), 2, 3, 4, 5, 6]
– M. Bertolazo
@M.Bertolazo and to call my duties in Chart as would?
– user198468
your method
GetNumeroVitimas()
returns only an integer, does not have an argument to filter by month... I think the first step would be to create a consultation that brings the total number of victims grouped per month.... I already say that this is another question– Leandro Angelo
@Leandroangelo then more before doing this, I wanted to use the same more method on the chart of Charts.js as my previous question, in case as I would do?
– user198468
It won’t, because it doesn’t meet that need
– Leandro Angelo
@Leandroangelo Sorry I don’t understand. I’m beginner in the area of Backend, if you can explain me better I would appreciate it very much. What does not meet the need and what I must do?
– user198468
It’s not the back end issue... even on the google chart, what you’re displaying doesn’t make any sense. You’re just repeating numbers in different positions... What do you want to represent with this graph?
– Leandro Angelo
@Leandroangelo I want to represent the total number of victims and authors, the repeated numbers are just examples that I will change later.
– user198468