0

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.

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.