vue教程6-图表
引入
cnpm install echarts --save
#在vue项目目录下安装echarts
静态折线图

linechart.js
import echarts from 'echarts'
export const option = {
// backgroundColor: 'rgba(57,64,86,0.02)',
// 标题
title: {
text: '在线人数曲线图',
x: 'center',
textStyle: {
fontWeight: 'normal',
fontSize: 20,
color: '#7a8ff3'
}
},
// 鼠标悬浮提示框
tooltip: {
trigger: 'axis'
},
// 图示
legend: {
data: ['今日', '昨日', '上周'],
right: '4%'
},
// 边框栅格
grid: {
top: 100,
left: '2%',
right: '2%',
bottom: '2%',
containLabel: true
},
// X轴
xAxis: [{
type: 'category',
boundaryGap: false,
data: ['13:00', '13:05', '13:10', '13:15', '13:20', '13:25', '13:30', '13:35', '13:40', '13:45', '13:50', '13:55']
}],
// Y轴
yAxis: [{
type: 'value',
name: '人数',
axisTick: {
show: false
},
axisLabel: {
margin: 10,
textStyle: {
fontSize: 14
}
}
}],
// 图示数据
series: [{
name: '今日',
type: 'line',
smooth: true,
symbol: 'circle',
symbolSize: 5,
showSymbol: false,
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(137, 189, 27, 0.3)'
}, {
offset: 0.8,
color: 'rgba(137, 189, 27, 0)'
}], false),
shadowColor: 'rgba(0, 0, 0, 0.1)',
shadowBlur: 10
}
},
itemStyle: {
normal: {
color: 'rgb(137,189,27)',
borderColor: 'rgba(137,189,2,0.27)',
borderWidth: 12
}
},
data: []
}, {
name: '昨日',
type: 'line',
smooth: true,
symbol: 'circle',
symbolSize: 5,
showSymbol: false,
lineStyle: {
normal: {
width: 1
}
},
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(0, 136, 212, 0.3)'
}, {
offset: 0.8,
color: 'rgba(0, 136, 212, 0)'
}], false),
shadowColor: 'rgba(0, 0, 0, 0.1)',
shadowBlur: 10
}
},
itemStyle: {
normal: {
color: 'rgb(0,136,212)',
borderColor: 'rgba(0,136,212,0.2)',
borderWidth: 12
}
},
data: []
}, {
name: '上周',
type: 'line',
smooth: true,
symbol: 'circle',
symbolSize: 5,
showSymbol: false,
lineStyle: {
normal: {
width: 1
}
},
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(219, 50, 51, 0.3)'
}, {
offset: 0.8,
color: 'rgba(219, 50, 51, 0)'
}], false),
shadowColor: 'rgba(0, 0, 0, 0.1)',
shadowBlur: 10
}
},
itemStyle: {
normal: {
color: 'rgb(219,50,51)',
borderColor: 'rgba(219,50,51,0.2)',
borderWidth: 12
}
},
data: []
}]
}
chart.vue
<template>
<div class="chart-container">
<!--创建一个echarts的容器,给定高宽-->
<div id="gamechart" style="width:100%; height:100%"/>
</div>
</template> <script>
// 安装echarts后,直接引入
import echarts from 'echarts'
import { option } from './linechart' export default {
data() {
return {
chart: {},
option: option
}
},
created() {
this.fetchData()
},
// 挂载图表函数
mounted() {
this.initChart()
},
methods: {
fetchData() {
this.chart = {
today: [220, 182, 191, 134, 150, 120, 110, 125, 145, 122, 165, 122],
lastday: [120, 110, 125, 145, 122, 165, 122, 220, 182, 191, 134, 150],
lastweek: [220, 182, 125, 145, 122, 191, 134, 150, 120, 110, 165, 122]
}
this.option.series[0].data = this.chart.today
this.option.series[1].data = this.chart.lastday
this.option.series[2].data = this.chart.lastweek
},
initChart() {
// 将chart初始化的实例绑定到一个DOM
this.chart = echarts.init(document.getElementById('gamechart'))
this.chart.setOption(this.option)
}
}
}
</script> <style scoped>
.chart-container{
position: relative;
width: 100%;
height: calc(100vh - 84px);
}
</style>
从后台获取数据
- echarts的时间戳格式是13位,精确到毫秒
- 后台给出的时间戳是变化的,所以在图表上,会展示最新时间的数据

[[[1557309121000,5901],[1557309241000,5962],[1557309361000,5992],[1557309480000,5983],[1557309600000,5949],[1557309720000,6047]]
#这是后台给出的api数据
#echarts可以从后台获取包含时间戳和数据的二维数组
methods: {
fetchData() {
getGameChart(this.gameid).then(response => {
this.option.series[0].data = response.today
this.option.series[1].data = response.lastday
this.option.series[2].data = response.lastweek
this.chart.setOption(this.option)
this.chart.hideLoading()
}).catch(error => {
this.$message.error('图表请求数据失败啦!')
console.log(error)
})
},
initChart() {
// 将chart初始化的实例绑定到一个DOM
this.chart = echarts.init(document.getElementById('gamechart'))
this.chart.showLoading({
text: '正在努力的读取数据中...'
})
}
}
#在vue中请求后台api,然后将返回的数据赋值给this.option.series的几个数组
vue教程6-图表的更多相关文章
- 网页图表Highcharts实践教程之图表代码构成
网页图表Highcharts实践教程之图表代码构成 Highcharts第一个实例 下面我们来实现本书的第一个Highcharts实例. [实例1-1]下面来制作北京连续一周最高温度折线图.操作过程如 ...
- vue教程3-05 vue组件数据传递、父子组件数据获取,slot,router路由
vue教程3-05 vue组件数据传递 一.vue默认情况下,子组件也没法访问父组件数据 <!DOCTYPE html> <html lang="en"> ...
- vue教程3-04 vue.js vue-devtools 调试工具的下载安装和使用
vue教程3-04 vue.js vue-devtools vue调试工具的安装和使用 一.vue-devtools 下载与安装 1.需要 fan qiang 2.打开谷歌浏览器设置--->扩展 ...
- vue教程3-03 vue组件,定义全局、局部组件,配合模板,动态组件
vue教程3-03 vue组件,定义全局.局部组件,配合模板,动态组件 一.定义一个组件 定义一个组件: 1. 全局组件 var Aaa=Vue.extend({ template:'<h3&g ...
- vue教程3-02 vue动画
vue教程3-02 vue动画 以下代码,已经用包管理器下载好vue,animate <!DOCTYPE html> <html lang="en"> &l ...
- vue教程3-01 路由、组件、bower包管理器使用
vue教程3-01 路由.组件.包管理器 以下操作前提是 已经安装好node.js npm bower-> (前端)包管理器 下载: npm install bower -g 验证: bower ...
- vue教程2-08 自定义键盘信息、监听数据变化vm.$watch
vue教程2-08 自定义键盘信息 @keydown.up @keydown.enter @keydown.a/b/c.... 自定义键盘信息: Vue.directive('on').keyCode ...
- vue教程2-07 自定义指令
vue教程2-07 自定义指令 自定义指令: 一.属性: Vue.directive(指令名称,function(参数){ this.el -> 原生DOM元素 }); <div v-re ...
- vue教程2-07 微博评论功能
vue教程2-07 微博评论功能 <!doctype html> <html> <head> <meta charset="utf-8"& ...
- vue教程2-06 过滤器
vue教程2-06 过滤器 过滤器: vue提供过滤器: capitalize uppercase currency.... <div id="box"> {{msg| ...
随机推荐
- Spring Boot2.0之@Async实现异步调用
补充一个知识点: lombok底层原理使用的是: 字节码技术ASM修改字节码文件,生成比如类似于get() set( )方法 一定要在开发工具安装 在编译时候修改字节码文件(底层使用字节码技术),线上 ...
- Mac OS 配置Maven
步骤: 1. 下载Maven tar包 https://maven.apache.org/download.cgi?Preferred=http%3A%2F%2Fmirrors.tuna.tsingh ...
- Linux_学习_02_ 重启tomcat与查看tomcat日志
一.重启tomcat服务器 cd /home/ehlhec/tomcat_dingtalk/bin ./shutdown.sh ps -ef|grep java ./startup.sh (1) 进入 ...
- Git_学习_02_ 分支
Git鼓励大量使用分支: 1.查看分支:git branch 2.创建分支:git branch <name> 3.切换分支:git checkout <name> 4.创建+ ...
- Listen81
Nut-Cracking Chimps Demonstrate Cultural Differences One family generally dines on Chinese takeout w ...
- Linux 中安装软件报缺少共享库文件的错误
linux 安装一些如软件 或者相关的模块时,经常报出缺少一些依赖包的 libxxx.so等的共享库文件 首先找到 该共享库文件 然后查看/etc/ld.so.conf 这个文件记录了编译时使用的动态 ...
- P4311 士兵占领[最大流]
题目地址 有一个$M * N$的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了$L_ ...
- VijosP1180:选课
描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这M门课并考核通过就能获得 ...
- BI 底座——数据仓库技术(Data Warehouse)
在开始喷这个主题之前,让我们先看看数据仓库的官方定义: 数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented).集成的(Integrate).相对稳定的(Non- ...
- [codeforces161D]Distance in Tree(点分治/树形dp)
题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...