Reputation: 173
I have had success displaying and manipulating bar charts but I cannot figure out how to work the pie charts. When I use the inspect tool I can see that something is there. I am using the following code: Chart.js CDN:
Canvas container for the graph: Javascript taken from the docs:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx,{
type:"pie",
data: [3, 4],
options: {
animation:{
animateScale:true
}
}
});
Amy advise will be welcomed.
Upvotes: 0
Views: 3081
Reputation: 432
According to documentation (Chart.js - Pie & Doughnut Charts), the data
variable should have had following structure:
data: {
labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
datasets: [{
backgroundColor: [ /* backgroundColor is optional */
"#2ecc71",
"#3498db",
"#95a5a6",
"#9b59b6",
"#f1c40f",
"#e74c3c",
"#34495e"
],
data: [12, 19, 3, 17, 28, 24, 7]
}]
}
as I see it, this is the problem in your case.
Upvotes: 1
Reputation: 10075
Try this. For more see Pie chart Usage. Comment if any confusion
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
datasets: [{
backgroundColor: [
"#2ecc71",
"#3498db",
"#95a5a6",
"#9b59b6",
"#f1c40f",
"#e74c3c",
"#34495e"
],
data: [12, 19, 3, 17, 28, 24, 7]
}]
}
});
.container {
width: 80%;
margin: 15px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js"></script>
<div class="container">
<h2>Chart.js — Pie Chart Demo</h2>
<div>
<canvas id="myChart"></canvas>
</div>
</div>
Upvotes: 2