Bind json array data to chart.js - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Bind json array data to chart.js

Demo Code

ResultView the demo in separate window

<html lang="en">
   <head> 
      <title>Chart.js - 2 Pie Charts in Same Canvas</title> 
   </head> 
   <body translate="no"> 
      <div id="canvas-holder" style="width:40%"> 
         <canvas id="chart-area"></canvas> 
      </div> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> 
      <script>
      window.chartColors = {//www  . j a v a2 s .  co  m
   red: 'rgb(255, 99, 132)',
   orange: 'rgb(255, 159, 64)',
   yellow: 'rgb(255, 205, 86)',
   green: 'rgb(75, 192, 192)',
   blue: 'rgb(54, 162, 235)',
   purple: 'rgb(153, 102, 255)',
   grey: 'rgb(231,233,237)'
};
var randomScalingFactor = function() {
  return Math.round(Math.random() * 100);
};
var data1 = [
  randomScalingFactor(),
  randomScalingFactor(),
];
var data2 = [
  randomScalingFactor(),
  randomScalingFactor(),
];
var ctx = document.getElementById("chart-area").getContext("2d");
var myPie = new Chart(ctx, {
  type: 'pie',
  data: {
        labels: ["FTE", "FTC"],
    datasets: [{
      label: 'Dataset 1',
      data: data1,
      backgroundColor: [
        "#FF6283",
        "#36A2EB"
      ],
      hoverBackgroundColor: [
        "#FF6283",
        "#36A2EB"
      ],
      borderWidth: 5,
    }, {
      label: 'Dataset 2',
      data: data2,
      backgroundColor: [
        "#FF6283",
        "#36A2EB"
      ],
      hoverBackgroundColor: [
        "#FF6283",
        "#36A2EB"
      ],
      borderWidth: 5,
    }],
  },
  options: {
    title: {
      display: true,
      text: 'Employee Overview',
      fontStyle: 'bold',
      fontSize: 20
    }
  }
});
      </script>  
   </body>
</html>

Related Tutorials