Home > Article > Web Front-end > Vue lightweight chart component

When encountering a scenario where charts need to be drawn on a web page, two libraries are generally used: D3.js and Chart.js. But in fact, you don't need such a heavyweight library at all. Sometimes you just want a simple SVG chart to meet your needs, then you can use Frappe Charts. It is a lightweight chart that provides full-featured, interactive animations, and with a simple component wrapper, you can use it with Vue.js!
Start installation
Start installing the component vue2-frappe. Here I assume you are working on an existing Vue.js project:
$ npm install --save vue2-frappe
Next step to register components:
import Vue from 'vue';
import VueFrappe from 'vue2-frappe';
import App from './App.vue';
Vue.use(VueFrappe);
new Vue({
el: '#app',
render: h => h(App)
});Start drawing charts
vue2-frappe is a layer based on Frappe Charts, encapsulating it for use with Vue.js Components, please see the Frappe Chart documentation for more usage:
<template>
<div id="app">
<h2>Chart: Benedict's Weight</h2>
<!-- id - 每一个图表必须有一个 id. -->
<!-- title - 图表上方显示的标题 -->
<!-- type - 图表的类型 线性图、饼图、条形图、等 -->
<!-- labels - x 轴上的值 -->
<!-- height- 可选,图表的高度 -->
<!-- colors - 将每一个数据集进行颜色区分 -->
<!-- lineOptions - 线形图的更多选项,请见文档-->
<!-- datasets - 数据集,对象数组 -->
<vue-frappe
id="my-chart-id"
title="Benedict's Weight From 2017-2018 (lbs)"
type="line"
:labels="['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"
:height="650"
:colors="['#008F68', '#FAE042']"
:lineOptions="{regionFill: 1}"
:datasets="[
{name: '2017', values: benedictsWeight2017},
{name: '2018', values: benedictsWeight2018}
]"
></vue-frappe>
<p>Conclusion: Benedict needs to go on a diet.</p>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
benedictsWeight2017: [480, 485, 491, 489, 485, 490, 497, 510, 512, 521, 530, 545],
benedictsWeight2018: [540, 575, 570, 555, 572, 580, 585, 587, 588, 590, 592, 590]
}
}
}
</script>Frappe Charts supports a variety of icons, such as pie charts, bar charts, proportion charts, heat maps, etc., for more advanced displays options, please see its official documentation!
Recommended tutorial: "JS Tutorial"
The above is the detailed content of Vue lightweight chart component. For more information, please follow other related articles on the PHP Chinese website!