Building a simple app with Nuxt and Morris for displaying charts
The article aims to describe (very shortly) how to create an application that will display a chart for data coming from an API.
First step, create the app with Nuxt
Nuxt is one of my favourite framework at the moment: if you are familiar with Vue.js, building an application with Nuxt is the next step to a simpler and more beautiful world 😀
So, let’s start by creating the application with the command npx create-nuxt-app
Important: select
axios
as a feature to install and chooseSingle Page App
for the rendering mode.
Now, you can start your application with npm run dev
and open http://localhost:3000
. You should get something like that:
Next step, add dependencies, plugins, etc
We need two other dependencies for our application:
vue-lodash
: a library that provides utility functions (will be used to parse the result of the APIvue-morris
: a wrapper of Morris for Vue.js (it is a little bit old, but very efficient)
Let’s add these required dependencies :
For a simpler use of certain dependencies, Nuxt can be enriched with plugins. Let’s do it with vue-lodash and vue-morris.
Create two files: plugins/vue-lodash.js
and plugins/vue-morris.js
and update nuxt.config.js
to declare these plugins.
Final step, add a new page with the chart
Now, we are going to create a page to display the 10 most populous countries in a bar chart.
The service used for this example is the API of the World Bank. They provide a lot of data, very interesting ! For more information about the API, check this page.
In our example, we use this URL:http://api.worldbank.org/v2/country/all/indicator/SP.POP.TOTL?date=2018&format=json&per_page=300&mrv=1
Parameters:
- SP.POP.TOTL: name of the indicator (in our case, the population)
- date: range of date (I just set the last year, I don’t want historical data)
- format: format of the result (it can be XML, JSON, …)
- per_page: number of result for each query (I set it to 300 to get all the results in a single query, no paging)
- mrv: fetches most recent values based on the number specified (I set it to 1, to get only the last recent value)
To query this API and get the most populous countries, the code will look like (in this method, we filter the values, sort according to the value
field and keep only 10 values):
Let’s add a new file: pages/population.vue
- The
<script>
part: we importBarChart
fromvue-morris
and add amounted
function that invoke the API (see above) - The
<template>
part: we use thebar-chart
tag for displaying the result of the call to the API.
Now, if you open the URL http://localhost:3000/population
, you should see something like that:
Finally, we are going to create a link to navigate from the home page to the population page: update the file pages/index.vue
, replace the two buttons by a <nuxt-link>
(you can also remove the logo).
Conclusion
As you can see, it’s very easy to create an application with Nuxt and add a simple chart framework to get rich and interactive pages.
The source code is available on github: https://github.com/bbonnin/medium-nuxt-morris. Enjoy !