radar.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Radar Chart</title>
  5. <script src="../dist/Chart.bundle.js"></script>
  6. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  7. <style>
  8. canvas {
  9. -moz-user-select: none;
  10. -webkit-user-select: none;
  11. -ms-user-select: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="width:75%">
  17. <canvas id="canvas"></canvas>
  18. </div>
  19. <button id="randomizeData">Randomize Data</button>
  20. <button id="addDataset">Add Dataset</button>
  21. <button id="removeDataset">Remove Dataset</button>
  22. <button id="addData">Add Data</button>
  23. <button id="removeData">Remove Data</button>
  24. <script>
  25. var randomScalingFactor = function() {
  26. return Math.round(Math.random() * 100);
  27. };
  28. var randomColorFactor = function() {
  29. return Math.round(Math.random() * 255);
  30. };
  31. var randomColor = function(opacity) {
  32. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  33. };
  34. var config = {
  35. type: 'radar',
  36. data: {
  37. labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
  38. datasets: [{
  39. label: "My First dataset",
  40. backgroundColor: "rgba(220,220,220,0.2)",
  41. pointBackgroundColor: "rgba(220,220,220,1)",
  42. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  43. }, {
  44. label: 'Hidden dataset',
  45. hidden: true,
  46. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  47. }, {
  48. label: "My Second dataset",
  49. backgroundColor: "rgba(151,187,205,0.2)",
  50. pointBackgroundColor: "rgba(151,187,205,1)",
  51. hoverPointBackgroundColor: "#fff",
  52. pointHighlightStroke: "rgba(151,187,205,1)",
  53. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  54. },]
  55. },
  56. options: {
  57. legend: {
  58. position: 'top',
  59. },
  60. title: {
  61. display: true,
  62. text: 'Chart.js Radar Chart'
  63. },
  64. scale: {
  65. reverse: false,
  66. ticks: {
  67. beginAtZero: true
  68. }
  69. }
  70. }
  71. };
  72. window.onload = function() {
  73. window.myRadar = new Chart(document.getElementById("canvas"), config);
  74. };
  75. $('#randomizeData').click(function() {
  76. $.each(config.data.datasets, function(i, dataset) {
  77. dataset.data = dataset.data.map(function() {
  78. return randomScalingFactor();
  79. });
  80. });
  81. window.myRadar.update();
  82. });
  83. $('#addDataset').click(function() {
  84. var newDataset = {
  85. label: 'Dataset ' + config.data.datasets.length,
  86. borderColor: randomColor(0.4),
  87. backgroundColor: randomColor(0.5),
  88. pointBorderColor: randomColor(0.7),
  89. pointBackgroundColor: randomColor(0.5),
  90. pointBorderWidth: 1,
  91. data: [],
  92. };
  93. for (var index = 0; index < config.data.labels.length; ++index) {
  94. newDataset.data.push(randomScalingFactor());
  95. }
  96. config.data.datasets.push(newDataset);
  97. window.myRadar.update();
  98. });
  99. $('#addData').click(function() {
  100. if (config.data.datasets.length > 0) {
  101. config.data.labels.push('dataset #' + config.data.labels.length);
  102. $.each(config.data.datasets, function (i, dataset) {
  103. dataset.data.push(randomScalingFactor());
  104. });
  105. window.myRadar.update();
  106. }
  107. });
  108. $('#removeDataset').click(function() {
  109. config.data.datasets.splice(0, 1);
  110. window.myRadar.update();
  111. });
  112. $('#removeData').click(function() {
  113. config.data.labels.pop(); // remove the label first
  114. $.each(config.data.datasets, function(i, dataset) {
  115. dataset.data.pop();
  116. });
  117. window.myRadar.update();
  118. });
  119. </script>
  120. </body>
  121. </html>