Loading data from server and displaying
See original GitHub issueExpected 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:
- Created 6 years ago
- Comments:15 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 thisAnd then in my
template
I simply used thedatasetsfull
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 😄
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