In this article, we’ll see how to install Highcharts for a Vue JS app, and how to use it. Please note that it only serves as an introduction, but if it is successful, I’ll gladly write a tutorial that goes deeper in the configuration of Highcharts.
Highcharts is a very powerful Javascript library that allows us to easily create beautiful & complex charts of all types.
Getting started
We’ll create a dummy project with Vue CLI for the purpose of this tutorial, using ESLint with Airbnb’s configuration.
Install the dependencies
Highcharts has a wrapper called highcharts-vue. You can install it using npm:
|
|
It seems that the wrapper only offers an API to use Highcharts, but not Highcharts itself. So you’ll also have to install it:
|
|
Importing Highcharts locally in a component
You can use Highcharts in your components individually. It’s very easy, just import it the component. For the sake of this example, we’ll also define a dataset as in the official repo :
|
|
Then in the template:
|
|
You should now have a chart that looks like this:
Registering globally
To import Highcharts globaly, you’ll have to add the following lines to main.js. Add the following lines :
|
|
In the last line, you tell Vue to use HighchartsVue
that is imported just before, and it will be used in the templates under the name chart
. The, in the template of any component, you’ll just have to add the <chart>
tag. No need to import any package or library from that component.
|
|
Configuring the chart
There are lots of possible options to pass to the charts. This is just a preview, but please see Highcharts’ docs for more information. You can (and should) also have a look at the examples.
The options will be contained in an object that will then be passed to the :options
parameter like that: <highcharts :options="chartOptions"></highcharts>
.
Defining the data
Say we want a chart that dislays the average monthly temperatures in France. We also want to define a name for this data. We’ll do it this way:
|
|
series
is an array, and you can add as many as you wish:
|
|
Name the chart
You can also add a title and a subtitle to the chart:
|
|
Defining the axis
You can define the axis to give more meaning to your charts. Either rename the values, or simply give your axis a title like this:
|
|
Chart type
Finally you can set the chart type with the type
key. The default value is line
. Let’s change it for example to bar
:
|
|
Keep going
The possibilities to customize your charts are almost infinite. You can easily change pretty much anything: the color of the lines, the position of the legend, the responsiveness of the charts, …