Guest User

Untitled

a guest
Feb 20th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.46 KB | None | 0 0
  1. <!-- Uses Bootstrap 4 -->
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.js"></script>
  3. <div class="card mb-3">
  4.   <div class="card-header">Test Status Chart</div>
  5.   <div class="card-body" style="background: #212529">
  6.     <canvas id="myBarChart1" width="170%" height="40%"></canvas>
  7.   </div>
  8. </div>
  9. <script>
  10.     var ctx1 = document.getElementById("myBarChart1")
  11.  
  12. var getMax = function(chart) {
  13.     datasets = chart.data.datasets;
  14.     max = 0;
  15.     for(var i=0; i<datasets.length; i++) {
  16.         dataset=datasets[i]
  17.         if(myBarChart.data.datasets[i].hidden) {
  18.             continue;
  19.         }
  20.         dataset.data.forEach(function(d) {
  21.             if(typeof(d)=="number" && d>max) {
  22.                 max = d
  23.             }
  24.         })
  25.     }
  26.     return max;
  27. }
  28.  
  29. var myBarChart = new Chart(ctx1, {
  30.   type: 'bar',
  31.   data: {
  32.     labels: ['2/17','2/18','2/19','2/20','2/21','2/22'],
  33.     datasets: [
  34.     {
  35.       label: "Bar 1",
  36.       backgroundColor: "rgba(255,0,0,0.6)",
  37.       borderColor: "rgba(255,0,0,1)",
  38.       data: [2500,1000,1500,3500,2000,500]
  39.     },
  40.     {
  41.       label: "Bar 2",
  42.       backgroundColor: "rgba(2,117,216,0.6)",
  43.       borderColor: "rgba(2,117,216,1)",
  44.       data: [50,0,100,1250,500,0]
  45.     },
  46.     {
  47.       label: "Bar 3",
  48.       backgroundColor: "rgba(255,255,51,0.6)",
  49.       borderColor: "rgba(255,255,51,0.6)",
  50.       data: [0,0,150,250,550,200]
  51.     },
  52.     {
  53.       label: "Bar 4",
  54.       backgroundColor: "rgba(0,255,102,0.6)",
  55.       borderColor: "rgba(0,255,102,1)",
  56.       data: [2450,1000,1250,2000,100,0]
  57.     },
  58.     ],
  59.   },
  60.   options: {
  61.     responsive: true,
  62.     scales: {
  63.       xAxes: [{
  64.         time: {
  65.           unit: 'day'
  66.         },
  67.         gridLines: {
  68.           display: false,
  69.           color: "white"
  70.         },
  71.         ticks: {
  72.           maxTicksLimit: 6,
  73.           fontColor: "white"
  74.         }
  75.       }],
  76.       yAxes: [{
  77.         ticks: {
  78.           maxTicksLimit: 15,
  79.           fontColor: "white",
  80.           padding: 10,
  81.           suggestedMax: 4000
  82.         },
  83.         gridLines: {
  84.           display: true,
  85.           color: "white"
  86.         }
  87.       }],
  88.     },
  89.     legend: {
  90.       display: true,
  91.       labels: {
  92.         fontColor: "white",
  93.       },
  94.       onClick: function(e, legendItem) {
  95.         di=legendItem.datasetIndex
  96.         myBarChart.data.datasets[di].hidden = !myBarChart.data.datasets[di].hidden;
  97.         myBarChart.options.scales.yAxes[0].ticks.suggestedMax=getMax(myBarChart)+100;
  98.         myBarChart.update()
  99.       }
  100.     }
  101.   }
  102. })
  103. </script>
Advertisement
Add Comment
Please, Sign In to add comment