Advertisement
Guest User

js

a guest
May 12th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { HorizontalBar } from 'vue-chartjs'
  2.  
  3. //import Chart from 'chartjs-plugin-datalabels'
  4.  
  5.  
  6. export default {
  7.     name: 'diagram',
  8.     extends: HorizontalBar,
  9.  
  10.     data: () => ({
  11.         chartData: '',
  12.         score: {},
  13.         value: [],
  14.         keys: [],
  15.         options: {
  16.             scales: {
  17.                 xAxes: [{
  18.                     ticks: {
  19.                         beginAtZero: true
  20.                         // barThickness: 150,
  21.                         //barPercentage: 0.5
  22.                         // scaleShowLabels:false,
  23.  
  24.                     }
  25.                 }],
  26.                 yAxes: [{
  27.  
  28.                     //display: false
  29.                     //beginAtZero: true,
  30.  
  31.                     // scaleShowLabels:false,
  32.                     // barPercentage: 0.5,
  33.                     //barThickness: 150,
  34.                     // mirror: true
  35.                     stacked: true,
  36.                     ticks:{
  37.                         beginAtZero: true,
  38.                         mirror:true
  39.                     }
  40.                 }]
  41.             },
  42.             scaleShowLabels: false
  43.             // responsive: true,
  44.             /*legend: {
  45.                 display: false,
  46.             },
  47.             */
  48.         },
  49.                 responsive: true,
  50.         maintainAspectRatio: false
  51.  
  52.     }),
  53.     created () {
  54.  
  55.         // Overwriting base render method with actual data.
  56.         this.$http({
  57.             method: 'get',
  58.             url: this.base_url + '/exam/api/score/' + this.$store.state.user.id + '/',
  59.  
  60.             auth: {
  61.                 username: 'l360_mobile_app',
  62.                 password: 'itsd321#'
  63.             },
  64.         }).then(res => {
  65.  
  66.             console.log(res);
  67.             if (res.status == '200') {
  68.                 console.log(res.data);
  69.                 this.score = res.data;
  70.                 for (let v  in  this.score) {
  71.                     this.value.push(this.score[v]);
  72.                 }
  73.                 for (let key  in  this.score) {
  74.                     this.keys.push(key);
  75.                 }
  76.  
  77.  
  78.                 console.log(this.keys);
  79.                 console.log(this.value);
  80.             }
  81.         });
  82.         this.fillData()
  83.  
  84.     },
  85.     mounted(){
  86.  
  87.         this.renderChart(this.chartData, this.options);
  88.        
  89.  
  90.  
  91.     },
  92.  
  93.     methods: {
  94.  
  95.         fillData ()  {
  96.             var context = this.$refs.canvas.getContext('2d');
  97.             this.chartData = new Chart(context,{
  98.  
  99.                 labels: this.keys,
  100.  
  101.                 datasets: [
  102.                     {
  103.                         fillColor: "#ffecbf",
  104.                         strokeColor: "#dacdd2",
  105.                         //backgroundColor: '#f87979',
  106.                         data: this.value,
  107.                         backgroundColor: [
  108.                             'rgba(255, 99, 132, 0.2)',
  109.                             'rgba(54, 162, 235, 0.2)',
  110.                             'rgba(255, 206, 86, 0.2)',
  111.                             'rgba(75, 192, 192, 0.2)',
  112.                             'rgba(153, 102, 255, 0.2)',
  113.                             'rgba(255, 159, 64, 0.2)'
  114.                         ],
  115.                         borderColor: [
  116.                             'rgba(255,99,132,1)',
  117.                             'rgba(54, 162, 235, 1)',
  118.                             'rgba(255, 206, 86, 1)',
  119.                             'rgba(75, 192, 192, 1)',
  120.                             'rgba(153, 102, 255, 1)',
  121.                             'rgba(255, 159, 64, 1)'
  122.                         ],
  123.  
  124.  
  125.                     }
  126.  
  127.                 ],
  128.  
  129.  
  130.  
  131.  
  132.             })
  133.         }
  134.     },
  135.     watch: {
  136.         data: function () {
  137.             this._chart.destroy();
  138.             this.renderChart(this.chartData, this.options);
  139.         },
  140.     },
  141.  
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement