安装
局部注册
全局注册
import { VChart, VLine, VArea, VBar, VPie, VPoint, VScale, VAxis, VGuide, VTooltip, VLegend, VGuide } from 'vux'
export default {
components: {
VChart,
VLine,
VArea,
VBar,
VPie,
VPoint,
VScale,
VAxis,
VGuide,
VTooltip,
VLegend,
VGuide
}
}
// 在入口文件全局引入
import Vue from 'vue'
import { VChart, VLine, VArea, VBar, VPie, VPoint, VScale, VAxis, VGuide, VTooltip, VLegend, VGuide } from 'vux'
Vue.component('v-chart', VChart)
Vue.component('v-line', VLine)
Vue.component('v-area', VArea)
Vue.component('v-bar', VBar)
Vue.component('v-pie', VPie)
Vue.component('v-point', VPoint)
Vue.component('v-scale', VScale)
Vue.component('v-axis', VAxis)
Vue.component('v-guide', VGuide)
Vue.component('v-tooltip', VTooltip)
Vue.component('v-legend', VLegend)
Vue.component('v-guide', VGuide)
基本折线图
<template>
<div>
<v-chart :data="data" prevent-default>
<v-scale x :tick-count="3" />
<v-tooltip :show-item-marker="false" show-x-value />
<v-line />
</v-chart>
</div>
</template>
<script>
import { VChart, VTooltip, VLine, VScale } from 'vux'
export default {
components: {
VChart,
VTooltip,
VLine,
VScale
},
data () {
return {
data: [
{ date: '2017-06-05', value: 116 },
{ date: '2017-06-06', value: 129 },
{ date: '2017-06-07', value: 135 },
{ date: '2017-06-08', value: 86 },
{ date: '2017-06-09', value: 73 },
{ date: '2017-06-10', value: 85 },
{ date: '2017-06-11', value: 73 },
{ date: '2017-06-12', value: 68 },
{ date: '2017-06-13', value: 92 },
{ date: '2017-06-14', value: 130 },
{ date: '2017-06-15', value: 245 },
{ date: '2017-06-16', value: 139 },
{ date: '2017-06-17', value: 115 },
{ date: '2017-06-18', value: 111 },
{ date: '2017-06-19', value: 309 },
{ date: '2017-06-20', value: 206 },
{ date: '2017-06-21', value: 137 },
{ date: '2017-06-22', value: 128 },
{ date: '2017-06-23', value: 85 },
{ date: '2017-06-24', value: 94 },
{ date: '2017-06-25', value: 71 },
{ date: '2017-06-26', value: 106 },
{ date: '2017-06-27', value: 84 },
{ date: '2017-06-28', value: 93 },
{ date: '2017-06-29', value: 85 },
{ date: '2017-06-30', value: 73 },
{ date: '2017-07-01', value: 83 },
{ date: '2017-07-02', value: 125 },
{ date: '2017-07-03', value: 107 },
{ date: '2017-07-04', value: 82 },
{ date: '2017-07-05', value: 44 },
{ date: '2017-07-06', value: 72 },
{ date: '2017-07-07', value: 106 },
{ date: '2017-07-08', value: 107 },
{ date: '2017-07-09', value: 66 },
{ date: '2017-07-10', value: 91 },
{ date: '2017-07-11', value: 92 },
{ date: '2017-07-12', value: 113 },
{ date: '2017-07-13', value: 107 },
{ date: '2017-07-14', value: 131 },
{ date: '2017-07-15', value: 111 },
{ date: '2017-07-16', value: 64 },
{ date: '2017-07-17', value: 69 },
{ date: '2017-07-18', value: 88 },
{ date: '2017-07-19', value: 77 },
{ date: '2017-07-20', value: 83 },
{ date: '2017-07-21', value: 111 },
{ date: '2017-07-22', value: 57 },
{ date: '2017-07-23', value: 55 },
{ date: '2017-07-24', value: 60 }
]
}
}
}
</script>
Show code
折线图:平滑曲线
<template>
<div>
<v-chart :data="data">
<v-scale x type="timeCat" mask="MM/DD" :tick-count="3" />
<v-scale y :min="0" alias="日均温度" :tick-count="5" />
<v-point
:style="{
stroke: '#fff',
lineWidth: 1
}"
shape="smooth" />
<v-line shape="smooth" />
</v-chart>
</div>
</template>
<script>
import { VChart, VLine, VPoint, VScale, VTooltip } from 'vux'
export default {
components: {
VChart,
VPoint,
VLine,
VScale,
VTooltip
},
data () {
return {
data: [
{ time: '2016-08-08 00:00:00', tem: 10 },
{ time: '2016-08-08 00:10:00', tem: 22 },
{ time: '2016-08-08 00:30:00', tem: 20 },
{ time: '2016-08-09 00:35:00', tem: 26 },
{ time: '2016-08-09 01:00:00', tem: 20 },
{ time: '2016-08-09 01:20:00', tem: 26 },
{ time: '2016-08-10 01:40:00', tem: 28 },
{ time: '2016-08-10 02:00:00', tem: 20 },
{ time: '2016-08-10 02:20:00', tem: 18 }
]
}
}
}
</script>
<style lang='css'>
</style>
Show code
动态数据:实时折线
<template>
<div>
<v-chart :data="data" ref="demo">
<v-scale x type="timeCat" :tick-count="3" mask="hh:mm:ss" />
<v-scale y :min="8" :tick-count="5" />
<v-tooltip :show-item-marker="false" show-x-value />
<v-guide type="html" :options="guide1" />
<v-guide type="html" :options="guide2" />
<v-line />
</v-chart>
</div>
</template>
<script>
import { VChart, VTooltip, VLine, VScale, VGuide } from 'vux'
const data = []
// 添加数据,模拟数据,可以指定当前时间的偏移的秒
function getRecord (offset) {
offset = offset || 0
return {
time: new Date().getTime() + offset * 1000,
value: Math.random() + 10
}
}
data.push(getRecord(-2))
data.push(getRecord(-1))
data.push(getRecord())
export default {
components: {
VChart,
VTooltip,
VLine,
VScale,
VGuide
},
mounted () {
this.timer = setInterval(() => {
data.push(getRecord())
}, 2000)
},
beforeDestroy () {
clearInterval(this.timer)
},
data () {
return {
guide1: {
position (xScale, yScales) {
const xValues = xScale.values
const yValues = yScales[0].values
const xMax = xValues[xValues.length - 1]
const yMax = yValues[yValues.length - 1]
return [ xMax, yMax ]
},
html: '<div style="border-radius: 100%;border: none;width: 12px;height: 12px;background-color: rgb(24, 144, 255);transition: top 0.35s cubic-bezier(0.23, 1, 0.32, 1);"></div>'
},
guide2: {
position (xScale, yScales) {
const xValues = xScale.values
const yValues = yScales[0].values
const xMax = xValues[xValues.length - 1]
const yMax = yValues[yValues.length - 1]
return [ xMax, yMax ]
},
html: '<div style="border-radius: 100%;border: none;width: 20px;height: 20px;background-color: rgba(24, 144, 255, 0.5);transition: top 0.35s cubic-bezier(0.23, 1, 0.32, 1);"></div>'
},
data
}
}
}
</script>
Show code
折线图:渐变
<template>
<div>
<v-chart :data="data">
<v-scale x :tick-count="5" :max="2020" />
<v-line shape="smooth" :colors="gradient"/>
<v-guide type="tag" :options="tag" />
<v-area shape="smooth" :colors="gradient"/>
</v-chart>
</div>
</template>
<script>
import { VChart, VGuide, VLine, VArea, VScale } from 'vux'
export default {
components: {
VChart,
VGuide,
VArea,
VLine,
VScale
},
data () {
return {
gradient: [
[0, '#F2C587'],
[0.5, '#ED7973'],
[1, '#8659AF']
],
tag: {
position: [ 2017, 30.12 ],
content: '30.12',
direct: 'tl',
offsetY: -5,
background: {
fill: '#8659AF'
},
pointStyle: {
fill: '#8659AF'
}
},
data: [
{ year: 2000, age: 27.2 },
{ year: 2001, age: 27.5 },
{ year: 2002, age: 27.8 },
{ year: 2003, age: 28 },
{ year: 2004, age: 28.2 },
{ year: 2005, age: 28.4 },
{ year: 2006, age: 28.8 },
{ year: 2007, age: 28.8 },
{ year: 2008, age: 28.8 },
{ year: 2009, age: 28.8 },
{ year: 2010, age: 28.9 },
{ year: 2011, age: 29 },
{ year: 2012, age: 29.3 },
{ year: 2013, age: 29.4 },
{ year: 2014, age: 29.5 },
{ year: 2015, age: 29.7 },
{ year: 2016, age: 30 },
{ year: 2017, age: 30.12 }
]
}
}
}
</script>
Show code
折线图:对比
<template>
<div>
<v-chart :data="data">
<v-line series-field="type" />
</v-chart>
</div>
</template>
<script>
import { VChart, VLine, VAxis, VTooltip } from 'vux'
/*
* [
* {"date":"2010-01-10","type":"能源","value":99.9},
* {"date":"2010-01-10","type":"金属","value":96.6}
* ]
*/
import data from './line_color.json'
export default {
components: {
VChart,
VLine,
VAxis,
VTooltip
},
data () {
return {
data
}
}
}
</script>
Show code
折线图:显示点
<template>
<div>
<v-chart :data="data">
<v-scale y :min="0" /> <!-- 默认 y 轴数值从数据的最小值到最大值,此处重置为从 0 开始 -->
<v-point :styles="{
stroke: '#fff',
lineWidth: 1
}"/>
<v-tooltip :show-item-marker="false" :on-show="onShow" /> <!-- onShow 为 F2 属性而非事件-->
<v-line />
</v-chart>
</div>
</template>
<script>
import { VChart, VPoint, VTooltip, VLine, VScale } from 'vux'
export default {
components: {
VChart,
VPoint,
VTooltip,
VLine,
VScale
},
data () {
return {
onShow (ev) {
const { items } = ev
items[0].name = null
items[0].value = '$ ' + items[0].value
},
data: [
{ day: '周一', value: 300 },
{ day: '周二', value: 400 },
{ day: '周三', value: 350 },
{ day: '周四', value: 500 },
{ day: '周五', value: 490 },
{ day: '周六', value: 600 },
{ day: '周日', value: 900 }
]
}
}
}
</script>
<style lang='css'>
</style>
Show code
层叠面积图
<template>
<div>
<v-chart :data="data">
<v-scale x field="date" type="timeCat" mask="MM-DD" />
<v-scale y field="value" :tick-count="4" :max="300" />
<v-tooltip show-crosshairs show-value-in-legend />
<v-area series-field="city" shape="smooth" adjust="stack" />
<v-line series-field="city" shape="smooth" adjust="stack" />
</v-chart>
</div>
</template>
<script>
import { VChart, VLine, VArea, VTooltip, VLegend, VScale } from 'vux'
export default {
components: {
VChart,
VLine,
VArea,
VTooltip,
VLegend,
VScale
},
data () {
return {
data: [
{ value: 63.4, city: 'New York', date: '2011-10-01' },
{ value: 62.7, city: 'Alaska', date: '2011-10-01' },
{ value: 72.2, city: 'Austin', date: '2011-10-01' },
{ value: 58, city: 'New York', date: '2011-10-02' },
{ value: 59.9, city: 'Alaska', date: '2011-10-02' },
{ value: 67.7, city: 'Austin', date: '2011-10-02' },
{ value: 53.3, city: 'New York', date: '2011-10-03' },
{ value: 59.1, city: 'Alaska', date: '2011-10-03' },
{ value: 69.4, city: 'Austin', date: '2011-10-03' },
{ value: 55.7, city: 'New York', date: '2011-10-04' },
{ value: 58.8, city: 'Alaska', date: '2011-10-04' },
{ value: 68, city: 'Austin', date: '2011-10-04' },
{ value: 64.2, city: 'New York', date: '2011-10-05' },
{ value: 58.7, city: 'Alaska', date: '2011-10-05' },
{ value: 72.4, city: 'Austin', date: '2011-10-05' },
{ value: 58.8, city: 'New York', date: '2011-10-06' },
{ value: 57, city: 'Alaska', date: '2011-10-06' },
{ value: 77, city: 'Austin', date: '2011-10-06' },
{ value: 57.9, city: 'New York', date: '2011-10-07' },
{ value: 56.7, city: 'Alaska', date: '2011-10-07' },
{ value: 82.3, city: 'Austin', date: '2011-10-07' },
{ value: 61.8, city: 'New York', date: '2011-10-08' },
{ value: 56.8, city: 'Alaska', date: '2011-10-08' },
{ value: 78.9, city: 'Austin', date: '2011-10-08' },
{ value: 69.3, city: 'New York', date: '2011-10-09' },
{ value: 56.7, city: 'Alaska', date: '2011-10-09' },
{ value: 68.8, city: 'Austin', date: '2011-10-09' },
{ value: 71.2, city: 'New York', date: '2011-10-10' },
{ value: 60.1, city: 'Alaska', date: '2011-10-10' },
{ value: 68.7, city: 'Austin', date: '2011-10-10' },
{ value: 68.7, city: 'New York', date: '2011-10-11' },
{ value: 61.1, city: 'Alaska', date: '2011-10-11' },
{ value: 70.3, city: 'Austin', date: '2011-10-11' },
{ value: 61.8, city: 'New York', date: '2011-10-12' },
{ value: 61.5, city: 'Alaska', date: '2011-10-12' },
{ value: 75.3, city: 'Austin', date: '2011-10-12' },
{ value: 63, city: 'New York', date: '2011-10-13' },
{ value: 64.3, city: 'Alaska', date: '2011-10-13' },
{ value: 76.6, city: 'Austin', date: '2011-10-13' },
{ value: 66.9, city: 'New York', date: '2011-10-14' },
{ value: 67.1, city: 'Alaska', date: '2011-10-14' },
{ value: 66.6, city: 'Austin', date: '2011-10-14' },
{ value: 61.7, city: 'New York', date: '2011-10-15' },
{ value: 64.6, city: 'Alaska', date: '2011-10-15' },
{ value: 68, city: 'Austin', date: '2011-10-15' },
{ value: 61.8, city: 'New York', date: '2011-10-16' },
{ value: 61.6, city: 'Alaska', date: '2011-10-16' },
{ value: 70.6, city: 'Austin', date: '2011-10-16' },
{ value: 62.8, city: 'New York', date: '2011-10-17' },
{ value: 61.1, city: 'Alaska', date: '2011-10-17' },
{ value: 71.1, city: 'Austin', date: '2011-10-17' },
{ value: 60.8, city: 'New York', date: '2011-10-18' },
{ value: 59.2, city: 'Alaska', date: '2011-10-18' },
{ value: 70, city: 'Austin', date: '2011-10-18' },
{ value: 62.1, city: 'New York', date: '2011-10-19' },
{ value: 58.9, city: 'Alaska', date: '2011-10-19' },
{ value: 61.6, city: 'Austin', date: '2011-10-19' },
{ value: 65.1, city: 'New York', date: '2011-10-20' },
{ value: 57.2, city: 'Alaska', date: '2011-10-20' },
{ value: 57.4, city: 'Austin', date: '2011-10-20' },
{ value: 55.6, city: 'New York', date: '2011-10-21' },
{ value: 56.4, city: 'Alaska', date: '2011-10-21' },
{ value: 64.3, city: 'Austin', date: '2011-10-21' },
{ value: 54.4, city: 'New York', date: '2011-10-22' },
{ value: 60.7, city: 'Alaska', date: '2011-10-22' },
{ value: 72.4, city: 'Austin', date: '2011-10-22' },
{ value: 54.4, city: 'New York', date: '2011-10-23' },
{ value: 65.1, city: 'Alaska', date: '2011-10-23' },
{ value: 72.4, city: 'Austin', date: '2011-10-23' },
{ value: 54.8, city: 'New York', date: '2011-10-24' },
{ value: 60.9, city: 'Alaska', date: '2011-10-24' },
{ value: 72.5, city: 'Austin', date: '2011-10-24' },
{ value: 57.9, city: 'New York', date: '2011-10-25' },
{ value: 56.1, city: 'Alaska', date: '2011-10-25' },
{ value: 72.7, city: 'Austin', date: '2011-10-25' },
{ value: 54.6, city: 'New York', date: '2011-10-26' },
{ value: 54.6, city: 'Alaska', date: '2011-10-26' },
{ value: 73.4, city: 'Austin', date: '2011-10-26' },
{ value: 54.4, city: 'New York', date: '2011-10-27' },
{ value: 56.1, city: 'Alaska', date: '2011-10-27' },
{ value: 70.7, city: 'Austin', date: '2011-10-27' },
{ value: 42.5, city: 'New York', date: '2011-10-28' },
{ value: 58.1, city: 'Alaska', date: '2011-10-28' },
{ value: 56.8, city: 'Austin', date: '2011-10-28' },
{ value: 40.9, city: 'New York', date: '2011-10-29' },
{ value: 57.5, city: 'Alaska', date: '2011-10-29' },
{ value: 51, city: 'Austin', date: '2011-10-29' },
{ value: 38.6, city: 'New York', date: '2011-10-30' },
{ value: 57.7, city: 'Alaska', date: '2011-10-30' },
{ value: 54.9, city: 'Austin', date: '2011-10-30' },
{ value: 44.2, city: 'New York', date: '2011-10-31' },
{ value: 55.1, city: 'Alaska', date: '2011-10-31' },
{ value: 58.8, city: 'Austin', date: '2011-10-31' },
{ value: 49.6, city: 'New York', date: '2011-11-01' },
{ value: 57.9, city: 'Alaska', date: '2011-11-01' },
{ value: 62.6, city: 'Austin', date: '2011-11-01' },
{ value: 47.2, city: 'New York', date: '2011-11-02' },
{ value: 64.6, city: 'Alaska', date: '2011-11-02' },
{ value: 71, city: 'Austin', date: '2011-11-02' }
]
}
}
}
</script>
Show code
层叠柱状图
<template>
<div>
<v-chart ref="demo" :data="data">
<v-scale x field="月份" />
<v-scale y field="月均降雨量" />
<v-bar series-field="name" adjust="stack" />
<v-tooltip show-value-in-legend />
</v-chart>
<x-button type="primary" plain @click.native="$refs.demo.rerender()">rerender</x-button>
</div>
</template>
<script>
import { VChart, VLine, VArea, VTooltip, VLegend, VBar, XButton, VScale } from 'vux'
export default {
components: {
VChart,
VLine,
VArea,
VTooltip,
VLegend,
VBar,
XButton,
VScale
},
data () {
return {
data: [
{ name: 'London', 月份: 'Jan.', 月均降雨量: 18.9 },
{ name: 'London', 月份: 'Feb.', 月均降雨量: 28.8 },
{ name: 'London', 月份: 'Mar.', 月均降雨量: 39.3 },
{ name: 'London', 月份: 'Apr.', 月均降雨量: 81.4 },
{ name: 'London', 月份: 'May.', 月均降雨量: 47 },
{ name: 'London', 月份: 'Jun.', 月均降雨量: 20.3 },
{ name: 'London', 月份: 'Jul.', 月均降雨量: 24 },
{ name: 'London', 月份: 'Aug.', 月均降雨量: 35.6 },
{ name: 'Berlin', 月份: 'Jan.', 月均降雨量: 12.4 },
{ name: 'Berlin', 月份: 'Feb.', 月均降雨量: 23.2 },
{ name: 'Berlin', 月份: 'Mar.', 月均降雨量: 34.5 },
{ name: 'Berlin', 月份: 'Apr.', 月均降雨量: 99.7 },
{ name: 'Berlin', 月份: 'May.', 月均降雨量: 52.6 },
{ name: 'Berlin', 月份: 'Jun.', 月均降雨量: 35.5 },
{ name: 'Berlin', 月份: 'Jul.', 月均降雨量: 37.4 },
{ name: 'Berlin', 月份: 'Aug.', 月均降雨量: 42.4 }
]
}
}
}
</script>
Show code
分组柱状图
<template>
<div>
<v-chart
ref="demo"
:data="data">
<v-scale x field="月份" />
<v-scale y field="月均降雨量" />
<v-bar series-field="name" :adjust="{
type: 'dodge',
marginRatio: 0.05 // 设置分组间柱子的间距
}" />
<v-tooltip show-value-in-legend />
</v-chart>
<x-button type="primary" plain @click.native="$refs.demo.rerender()">rerender</x-button>
</div>
</template>
<script>
import { VChart, VLine, VArea, VTooltip, VLegend, VBar, XButton, VScale } from 'vux'
export default {
components: {
VChart,
VLine,
VArea,
VTooltip,
VLegend,
VBar,
XButton,
VScale
},
data () {
return {
data: [
{ name: 'London', 月份: 'Jan.', 月均降雨量: 18.9 },
{ name: 'London', 月份: 'Feb.', 月均降雨量: 28.8 },
{ name: 'London', 月份: 'Mar.', 月均降雨量: 39.3 },
{ name: 'London', 月份: 'Apr.', 月均降雨量: 81.4 },
{ name: 'London', 月份: 'May.', 月均降雨量: 47 },
{ name: 'London', 月份: 'Jun.', 月均降雨量: 20.3 },
{ name: 'London', 月份: 'Jul.', 月均降雨量: 24 },
{ name: 'London', 月份: 'Aug.', 月均降雨量: 35.6 },
{ name: 'Berlin', 月份: 'Jan.', 月均降雨量: 12.4 },
{ name: 'Berlin', 月份: 'Feb.', 月均降雨量: 23.2 },
{ name: 'Berlin', 月份: 'Mar.', 月均降雨量: 34.5 },
{ name: 'Berlin', 月份: 'Apr.', 月均降雨量: 99.7 },
{ name: 'Berlin', 月份: 'May.', 月均降雨量: 52.6 },
{ name: 'Berlin', 月份: 'Jun.', 月均降雨量: 35.5 },
{ name: 'Berlin', 月份: 'Jul.', 月均降雨量: 37.4 },
{ name: 'Berlin', 月份: 'Aug.', 月均降雨量: 42.4 }
]
}
}
}
</script>
Show code
百分比柱状图