Search code examples
javascriptvue.jschart.jsvue-chartjs

Vue.js: How to retrieve data from API for vue chart.js


As I have no experience with drawing charts, using vue-chart.js for my project can be not very understandable.

I receive this data from API:

reports: {

 stats: {

  2018-02: {
    users: {
      "min": 12481,
      "max": 12581,
      "length": 19,
      "average": 12531,
      "median": 12527 
     }
    },
 2018-03: {
  users: {    
    "average": 12590,   
    "length": 1,
    "max": 12590,       
    "median": 12590,
    "min": 12590
   }
  } 
 }
}

I need to draw a chart showing the amount of active users in the system on every month. So the only parameter I need is median.

So far, my chart looks like this (I took this from examples of vue-chart.js):

import { Line } from 'vue-chartjs';

export default {
extends: Line,
mounted () {
this.renderChart({
  labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  datasets: [
    {
      label: 'Data One',
      backgroundColor: '#f87979',
      data: [40, 39, 10, 40, 39, 80, 40]
    }
   ]
  }, {responsive: true, maintainAspectRatio: false})
 }
};

How can I send median values to the data parameter of the chart? I have the action loadFullReport and the getter getFullReport. Using the last one I can retrieve all the data from report object which you saw above.

Here is what I have in store:

import api from '../api';

export default {
state: {
  report: {
   isLoaded: false,
   data: {},
  },
},
actions: {
  loadFullReport({ commit }) {
  api
    .get('/reports/active', { params: { start_date: '2018-01-01', end_date: '2018-03-01' } })
    .then(({ data }) => {
      commit('SET_FULL_REPORT', data);
    });
  },
 },
mutations: {
  SET_FULL_REPORT(state, data) {
    state.report = {
      isLoaded: true,
      data,
    };
  },
},
 getters: {
   getFullReport(state) {
    return state.report;
  },
 },
};

Solution

  • I don't really understand your question. You have to transform your data, to fit the schema of chart.js

    You can however add multiple datasets. The question is what are you trying to achieve.

    import { Line } from 'vue-chartjs';
    
    export default {
    extends: Line,
    mounted () {
    this.renderChart({
      labels: ['2018-02', '2018-03'],
      datasets: [
        {
          label: 'umin',
          backgroundColor: '#f87979',
          data: [12481, 12590]
        },
        {
          label: 'umax',
          backgroundColor: '#f87979',
          data: [12581, 12590]
        },
        {
          label: 'umedian',
          backgroundColor: '#f87979',
          data: [12527, 12590]
        }
       ]
      }, {responsive: true, maintainAspectRatio: false})
     }
    };
    

    The labels: array is your X axis. The data array in your datasets object, is your Y value for the corresponding X value.

    ╔═════════╦════════════╦═════════════╗
    ║         ║ 2018-02    ║ 2018-03     ║
    ╠═════════╬════════════╬═════════════╣
    ║ umin    ║ 12481      ║ 12590       ║
    ║ umax    ║ 12581      ║ 12590       ║
    ║ umedian ║ 12527      ║ 12590       ║
    ╚═════════╩════════════╩═════════════╝