Chart with data from json - Javascript Chart.js

Javascript examples for Chart.js:Chart Json Data

Description

Chart with data from json

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){/*from   w w  w  .j a  va  2 s  .c  o m*/
function getRandomColor() {
  var letters = '0123456789ABCDEF';
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}
data = '["Mobiltelefon","Smartphone","SIM-Karte","Tablet","Navigationsger\u00e4t","USB-Stick","Speicherkarte","PC","Notebook","Festplatte"]';
var chart = new Chart(document.getElementById("pie-chart"), {
  type: 'pie',
  data: {
    labels: JSON.parse(data),
    datasets: [{
      label: "Anzahl Asservate",
      backgroundColor: [getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor()],
      data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    }]
  },
  options: {
    title: {
      display: true,
      text: 'Kategorien-Verteilung der Asservate'
    }
  }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="pie-chart" width="800" height="450"></canvas>  
   </body>
</html>

Related Tutorials