-2
I need to generate a pie cart chart using js and json, I have the json file with the name of the courses and number of approved courses per course, I need js to read this information and generate the graph with them "I cannot generate the example graph: read the json and process the data so that it shows in the graph automatically. I want the names of the Courses and the approved numbers that are in json to fill in the required fields and generate the charts."
var nome_course;
var aprovados;
$(document).ready(function () {
$.getJSON("./report/report_curso.json", function (json) {
let nome_curso = json["nome_curso"];
let aprovados = json["matriculados"];
});
});
Chart.defaults.global.defaultFontFamily = 'Nunito', '-apple-system,system-
ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
Chart.defaults.global.defaultFontColor = '#858796';
// Pie Chart Example
var ctx = document.getElementById("myPieChart");
var myPieChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: [nome_curso],
datasets: [{
data: [aprovados],
backgroundColor: ['#4e73df', '#1cc88a', '#36b9cc'],
hoverBackgroundColor: ['#2e59d9', '#17a673', '#2c9faf'],
hoverBorderColor: "rgba(234, 236, 244, 1)",
}],
},
options: {
maintainAspectRatio: false,
tooltips: {
backgroundColor: "rgb(255,255,255)",
bodyFontColor: "#858796",
borderColor: '#dddfeb',
borderWidth: 1,
xPadding: 15,
yPadding: 15,
displayColors: false,
caretPadding: 10,
},
legend: {
display: false
},
cutoutPercentage: 80,
},
});
Json: [{"course name":"course 1","enrolled":"673"},{"course name":"course 2","enrolled":"906"},{"course name":"course 3","enrolled":"489"}]
What would be the problem ?
– Pedro Henrique
I cannot generate the example graph: read the json and process the data so that it shows in the graph automatically. I want the names of the Courses and the approved numbers that are in json to fill in the required fields and generate the charts.
– Fagner Fernandes
You want to use the query result
getJSON
on the chart is this ?– Pedro Henrique
To read a json you need to use Javascript methods for either map() or foreach() for example, if you still know how to play with such tools I believe you are not yet prepared to play with graphics.
– LeAndrade
yes that even in the case of the chart there where is "Labels:" would be inserted the name of the courses in EX format: ["course name 1","course name 2"]. And where is "date:" the number of those enrolled in the EX format:["673","906"]
– Fagner Fernandes
I can also work with foreach(), but very little, so I came to ask for help, some light so that I can do what I need.
– Fagner Fernandes