question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loading data from server and displaying

See original GitHub issue

Expected Behavior

In the example for Reactive Data in the docs, when implemented, you have to click the “Randomize” button to trigger the fillData function and get content displayed in the graph.

Actual Behavior

I’m looking for a way (as I serve the content to display from an API) to display the data as soon as the component gets a response from my API.

I tried to do the usual Vue 2.0 nextTick to call the fillData function when the component is mounted

mounted () {
      this.$nextTick(function () {
        this.fillData;
      })
    },

Below is the code of my component (slightly adapted to match my API call), of course it works perfectly when I click the “Randomize” button but I don’t want to have to click this button for each and every graph in my “Dashboard” component (several graphs in this component).

<template>
    <div>
      <line-chart :chart-data="datacollection"></line-chart>
      <button @click="fillData()">Randomize</button>
    </div>
</template>

<script>
  import LineChart from './LineChart.js'

  export default {
    components: {
      LineChart
    },
    data () {
      return {
        datacollection: null
      }
    },
    mounted () {
      this.$nextTick(function () {
        this.fillData;
      })
    },
    methods: {
      fillData () {
        this.$http.get('/api/data/line/scenarios_by_day')
            .then(response => {
            this.datacollection = response.data;
        });
      },
    }
  }
</script>

Thank you in advance for your help !

Environment

  • vue.js version: <version here>
  • vue-chart.js version: <version here>
  • npm version: <version here>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
yometecommented, Mar 27, 2017

Hi @cdubant

I recently had to implement something like this and I was kinda stuck. Here’s what I did (with help from Vuex);

First thing I did was store what I needed from the API call in Vuex so I could pretty much access it anywhere. Then I wrote a computed object that returns the the data in Vuex and uses it to build the chart; something like this

      datasetsfull () {
        return {
          labels: store.state.chartdays,
          datasets: [
            {
              label: 'Time Signed In',
              backgroundColor: '#40CF97',
              data: store.state.charttimes
            }
          ]
        }
      },
      chartdays () {
        return store.state.chartdays
      },
      charttimes () {
        return store.state.charttimes
      }
    }

And then in my template I simply used the datasetsfull function; something like this; <reports-charts :chart-data="datasetsfull" :height="400" :options="{responsive: true, maintainAspectRatio: false}"></reports-charts>

I hope that was useful to you 😄

2reactions
aperturelesscommented, Mar 25, 2017

Well, you don’t need to use Vue’s $nextTick() function, because vue does not render the chart. The chart gets renderes by chart.js on a canvas.

You can simply make your api call in the mounted() hook.

However your response.data need to have a valid chart.js datatructure.

But keep in mind the limitations to the watcher. There are also some issues related to this: #44 #34

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetching data from the server - Learn web development | MDN
The basic model of page loading on the Web is that your browser makes one or more HTTP requests to the server for...
Read more >
Loading and Displaying Server Content - amp.dev
When our site loads, we want to reach out to that server, download the latest product data, and display that data in a...
Read more >
How to display a spinner when data is loading from server by ...
It takes a small amount of time when loading data from the server. I want to display a spinner when the fetch api...
Read more >
How to Load Data Into Tables With the Fetch API in JavaScript
In today's video I'll be showing you how to load data into an HTML table using the Fetch API/requests in JavaScript.
Read more >
2: How to load in data from a server using AJAX - YouTube
How to load in data from a server using AJAX - Learn AJAX programming. In this lesson we will learn how to get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found