I can't display my ChartJS pie chart with dynamic data, I googled a lot and I couldn't find a solution, so I'm here for your help.
window.onload = function() {
$.ajax({
url: 'https://jsonplaceholder.typicode.com/todos/1',
dataType: "json",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function(data) {
// var dataResults = data.d.results;
var tempData = [{
EnterpriseProjectTypeName: 'first project'
},
{
EnterpriseProjectTypeName: 'first project'
},
{
EnterpriseProjectTypeName: 'first project'
},
{
EnterpriseProjectTypeName: 'second project'
},
{
EnterpriseProjectTypeName: 'third project'
},
{
EnterpriseProjectTypeName: 'test'
}
];
var itermeidiaryObject = {};
$.each(tempData, function(key, value) {
var epn = value.EnterpriseProjectTypeName;
var som = 0;
if (epn != null) {
itermeidiaryObject[epn] = ++itermeidiaryObject[epn] || 1;
}
var somme = som;
});
var finalObject = Object.keys(itermeidiaryObject).map(function(key) {
return {
label: key,
y: itermeidiaryObject[key]
}
});
var ctx = document.getElementById('myChart').getContext('2d');
var lables=tempData
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: finalObject,
datasets: [{
data: finalObject,
}]
},
options: {
responsive: false,
scales: {
xAxes: [{
ticks: {
maxRotation: 90,
minRotation: 80
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
});
}
And this is the html part
<canvas id="myChart"></canvas>
</div><script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
Can any one help me please?
Do you want a pie chart or a bar chart ? Edited things a little so that it makes a pie chart, although you can just go back to the bar chart with a few edits regarding mostly the way labels were handled since it looks like they need to be single values in an array. Probably a better way, but this should help. This is for the pie chart. You had a rogue div in the HTML also.
window.onload = function() { $.ajax({ url: 'https://jsonplaceholder.typicode.com/todos/1', dataType: "json", method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function(data) { // var dataResults = data.d.results; var tempData = [{ EnterpriseProjectTypeName: 'first project' }, { EnterpriseProjectTypeName: 'first project' }, { EnterpriseProjectTypeName: 'first project' }, { EnterpriseProjectTypeName: 'second project' }, { EnterpriseProjectTypeName: 'third project' }, { EnterpriseProjectTypeName: 'test' } ]; var itermeidiaryObject = {}; $.each(tempData, function(key, value) { var epn = value.EnterpriseProjectTypeName; var som = 0; if (epn != null) { itermeidiaryObject[epn] = ++itermeidiaryObject[epn] || 1; } var somme = som; }); var finalObject = Object.keys(itermeidiaryObject).map(function(key) { return { label: key, y: itermeidiaryObject[key] } }); var pievalues = finalObject.map(function(value, index) { return value.y; }); var labels = finalObject.map(function(value, index) { return value.label; }); var colorscheme = colors.slice(0, labels.length); console.log(labels); console.log(pievalues); console.log(finalObject); var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'pie', data: { labels: labels, datasets: [{ data: pievalues, backgroundColor: colorscheme }] }, options: { responsive: false, } }); } }); } var colors = ["#0074D9", "#FF4136", "#2ECC40", "#FF851B", "#7FDBFF", "#B10DC9", "#FFDC00", "#001f3f", "#39CCCC", "#01FF70", "#85144b", "#F012BE", "#3D9970", "#111111", "#AAAAAA"];
<canvas id="myChart"></canvas> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
I am presuming that the data you provided in the handler success is pretty much what you get back as JSON ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.