Quickly add charts to your Vue application with our ZingChart component
This guide assumes some basic working knowledge of Vue.
Install the zingchart-vue package via npm
$ npm install zingchart-vue
You can either include the zingchart-vue component to your project globally or locally per component
In your main app file, add the following lines of code:
import Vue from 'vue';
import zingchartVue from 'zingchart-vue';
Vue.component('zingchart', zingchartVue)
This will register the zingchart component globally throughout your application. While the easiest installation option, this will load ZingChart immediately on your user's first load of the application - regardless if a chart is on the first page or not. We reccomend this approach if ZingChart is used heavily across multiple pages.
In each component where ZingChart is being used, include the following in your component's configuration:
import zingchart;
import zingchartVue from 'zingchart-vue';
{
...
components: {
zingchart: zingchartVue,
...
}
}
Note: We recommend this approach if ZingChart is only included in a few, un-related pages across your application.
ZingChart comes bundled with your common chart types such as line, column, pie, and scatter. For additional chart types, you will need to import the additional module file.
For example, adding a depth chart to your vue component will require an additional import. Note, you must import from the modules-es6
directory in the zingchart package.
import 'zingchart/modules-es6/zingchart-depth.min.js';
}
Here is a full .vue example for loading a map:
<template>
<div style="height:200px">
<zingchart :height="'100%'" ref="myChart"
:data="{
shapes: [
{
type: 'zingchart.maps',
options: {
name: 'usa',
ignore: ['AK','HI']
}
}
]
}" >
</zingchart>
</div>
</template>
<script>
import zingchartVue from 'zingchart-vue';
import 'zingchart/modules-es6/zingchart-maps.min.js';
import 'zingchart/modules-es6/zingchart-maps-usa.min.js';
export default {
components: {
zingchart: zingchartVue,
},
}
</script>
The zingchart-vue
component can be included into template as an element. Below is a simple example of a line chart:
<zingchart :data="chartData"></zingchart>
...
new Vue({
...
data() {
return {
chartData: {
type: 'line',
series: [{
values: [4,5,3,3,4,4]
}]
}
}
}
...
})
The configuration object to pass to the chart. This can be a graphset
object (multi-chart shared configuration) or a standard single chart configuration.
<zingchart :data="myData" :series="mySeries"></zingchart>
{
data() {
return {
myData: {
type: 'line',
title: {
text: 'Hello World',
},
},
mySeries: [
{ values: [1,2,4,5,6] }
]
}
}
}
The id for the DOM element for ZingChart to attach to. If no id is specified, the id will be autogenerated in the form of zingchart-auto-#
Accepts an array of series objects, and overrides a series if it was supplied into the config object. Varries by chart type used - Refer to the ZingChart documentation for more details.
The render type of the chart. The default is svg
but you can also pass the string canvas
to render the charts in canvas.
<zingchart :data="myData" :series="mySeries"></zingchart>
{
data() {
return {
myData: {
type: 'line',
title: {
text: 'Hello World',
},
},
mySeries: [
{ values: [1,2,4,5,6] }
]
}
}
}
The width of the chart. Defaults to 100%
The height of the chart. Defaults to 480px.
The theme or 'defaults' object defined by ZingChart. More information available here: https://www.zingchart.com/docs/api/themes
All zingchart events are readily available on the component to listen to. For example, to listen for the 'complete' event when the chart is finished rendering:
<zingchart :data="myData" @complete="chartCompleted"/>
{
...
methods: {
chartCompleted(result) {
console.log(`The chart ${result.id} finished rendering`);
}
}
}
For a list of all the events that you can listen to, refer to the complete documentation on https://www.zingchart.com/docs/events
All zingchart methods are readily available on the component's instance to call. For example, to add a new plot node to the chart:
<zingchart :data="myData" ref="chart"/>
{
...
methods: {
myCustomAddNode() {
this.$refs.chart.addnode({
value: 55,
});
}
}
}
For a list of all the methods that you can call and the parameters each method can take, refer to the complete documentation on https://www.zingchart.com/docs/methods
This repository contains a sample Vue application to give you an easy way to see the component in action
$ npm run dev