引入

  1. cnpm install echarts --save
  2. #在vue项目目录下安装echarts

  

静态折线图

linechart.js

  1. import echarts from 'echarts'
  2.  
  3. export const option = {
  4. // backgroundColor: 'rgba(57,64,86,0.02)',
  5. // 标题
  6. title: {
  7. text: '在线人数曲线图',
  8. x: 'center',
  9. textStyle: {
  10. fontWeight: 'normal',
  11. fontSize: 20,
  12. color: '#7a8ff3'
  13. }
  14. },
  15. // 鼠标悬浮提示框
  16. tooltip: {
  17. trigger: 'axis'
  18. },
  19. // 图示
  20. legend: {
  21. data: ['今日', '昨日', '上周'],
  22. right: '4%'
  23. },
  24. // 边框栅格
  25. grid: {
  26. top: 100,
  27. left: '2%',
  28. right: '2%',
  29. bottom: '2%',
  30. containLabel: true
  31. },
  32. // X轴
  33. xAxis: [{
  34. type: 'category',
  35. boundaryGap: false,
  36. 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']
  37. }],
  38. // Y轴
  39. yAxis: [{
  40. type: 'value',
  41. name: '人数',
  42. axisTick: {
  43. show: false
  44. },
  45. axisLabel: {
  46. margin: 10,
  47. textStyle: {
  48. fontSize: 14
  49. }
  50. }
  51. }],
  52. // 图示数据
  53. series: [{
  54. name: '今日',
  55. type: 'line',
  56. smooth: true,
  57. symbol: 'circle',
  58. symbolSize: 5,
  59. showSymbol: false,
  60. areaStyle: {
  61. normal: {
  62. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  63. offset: 0,
  64. color: 'rgba(137, 189, 27, 0.3)'
  65. }, {
  66. offset: 0.8,
  67. color: 'rgba(137, 189, 27, 0)'
  68. }], false),
  69. shadowColor: 'rgba(0, 0, 0, 0.1)',
  70. shadowBlur: 10
  71. }
  72. },
  73. itemStyle: {
  74. normal: {
  75. color: 'rgb(137,189,27)',
  76. borderColor: 'rgba(137,189,2,0.27)',
  77. borderWidth: 12
  78.  
  79. }
  80. },
  81. data: []
  82. }, {
  83. name: '昨日',
  84. type: 'line',
  85. smooth: true,
  86. symbol: 'circle',
  87. symbolSize: 5,
  88. showSymbol: false,
  89. lineStyle: {
  90. normal: {
  91. width: 1
  92. }
  93. },
  94. areaStyle: {
  95. normal: {
  96. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  97. offset: 0,
  98. color: 'rgba(0, 136, 212, 0.3)'
  99. }, {
  100. offset: 0.8,
  101. color: 'rgba(0, 136, 212, 0)'
  102. }], false),
  103. shadowColor: 'rgba(0, 0, 0, 0.1)',
  104. shadowBlur: 10
  105. }
  106. },
  107. itemStyle: {
  108. normal: {
  109. color: 'rgb(0,136,212)',
  110. borderColor: 'rgba(0,136,212,0.2)',
  111. borderWidth: 12
  112. }
  113. },
  114. data: []
  115. }, {
  116. name: '上周',
  117. type: 'line',
  118. smooth: true,
  119. symbol: 'circle',
  120. symbolSize: 5,
  121. showSymbol: false,
  122. lineStyle: {
  123. normal: {
  124. width: 1
  125. }
  126. },
  127. areaStyle: {
  128. normal: {
  129. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  130. offset: 0,
  131. color: 'rgba(219, 50, 51, 0.3)'
  132. }, {
  133. offset: 0.8,
  134. color: 'rgba(219, 50, 51, 0)'
  135. }], false),
  136. shadowColor: 'rgba(0, 0, 0, 0.1)',
  137. shadowBlur: 10
  138. }
  139. },
  140. itemStyle: {
  141. normal: {
  142. color: 'rgb(219,50,51)',
  143. borderColor: 'rgba(219,50,51,0.2)',
  144. borderWidth: 12
  145. }
  146. },
  147. data: []
  148. }]
  149. }

chart.vue

  1. <template>
  2. <div class="chart-container">
  3. <!--创建一个echarts的容器,给定高宽-->
  4. <div id="gamechart" style="width:100%; height:100%"/>
  5. </div>
  6. </template>
  7.  
  8. <script>
  9. // 安装echarts后,直接引入
  10. import echarts from 'echarts'
  11. import { option } from './linechart'
  12.  
  13. export default {
  14. data() {
  15. return {
  16. chart: {},
  17. option: option
  18. }
  19. },
  20. created() {
  21. this.fetchData()
  22. },
  23. // 挂载图表函数
  24. mounted() {
  25. this.initChart()
  26. },
  27. methods: {
  28. fetchData() {
  29. this.chart = {
  30. today: [220, 182, 191, 134, 150, 120, 110, 125, 145, 122, 165, 122],
  31. lastday: [120, 110, 125, 145, 122, 165, 122, 220, 182, 191, 134, 150],
  32. lastweek: [220, 182, 125, 145, 122, 191, 134, 150, 120, 110, 165, 122]
  33. }
  34. this.option.series[0].data = this.chart.today
  35. this.option.series[1].data = this.chart.lastday
  36. this.option.series[2].data = this.chart.lastweek
  37. },
  38. initChart() {
  39. // 将chart初始化的实例绑定到一个DOM
  40. this.chart = echarts.init(document.getElementById('gamechart'))
  41. this.chart.setOption(this.option)
  42. }
  43. }
  44. }
  45. </script>
  46.  
  47. <style scoped>
  48. .chart-container{
  49. position: relative;
  50. width: 100%;
  51. height: calc(100vh - 84px);
  52. }
  53. </style>

  

从后台获取数据

  • echarts的时间戳格式是13位,精确到毫秒
  • 后台给出的时间戳是变化的,所以在图表上,会展示最新时间的数据

  1. [[[1557309121000,5901],[1557309241000,5962],[1557309361000,5992],[1557309480000,5983],[1557309600000,5949],[1557309720000,6047]]
  2. #这是后台给出的api数据
  3. #echarts可以从后台获取包含时间戳和数据的二维数组

  

  1. methods: {
  2. fetchData() {
  3. getGameChart(this.gameid).then(response => {
  4. this.option.series[0].data = response.today
  5. this.option.series[1].data = response.lastday
  6. this.option.series[2].data = response.lastweek
  7. this.chart.setOption(this.option)
  8. this.chart.hideLoading()
  9. }).catch(error => {
  10. this.$message.error('图表请求数据失败啦!')
  11. console.log(error)
  12. })
  13. },
  14. initChart() {
  15. // 将chart初始化的实例绑定到一个DOM
  16. this.chart = echarts.init(document.getElementById('gamechart'))
  17. this.chart.showLoading({
  18. text: '正在努力的读取数据中...'
  19. })
  20. }
  21. }
  22. #在vue中请求后台api,然后将返回的数据赋值给this.option.series的几个数组

  

vue教程6-图表的更多相关文章

  1. 网页图表Highcharts实践教程之图表代码构成

    网页图表Highcharts实践教程之图表代码构成 Highcharts第一个实例 下面我们来实现本书的第一个Highcharts实例. [实例1-1]下面来制作北京连续一周最高温度折线图.操作过程如 ...

  2. vue教程3-05 vue组件数据传递、父子组件数据获取,slot,router路由

    vue教程3-05 vue组件数据传递 一.vue默认情况下,子组件也没法访问父组件数据 <!DOCTYPE html> <html lang="en"> ...

  3. vue教程3-04 vue.js vue-devtools 调试工具的下载安装和使用

    vue教程3-04 vue.js vue-devtools vue调试工具的安装和使用 一.vue-devtools 下载与安装 1.需要 fan qiang 2.打开谷歌浏览器设置--->扩展 ...

  4. vue教程3-03 vue组件,定义全局、局部组件,配合模板,动态组件

    vue教程3-03 vue组件,定义全局.局部组件,配合模板,动态组件 一.定义一个组件 定义一个组件: 1. 全局组件 var Aaa=Vue.extend({ template:'<h3&g ...

  5. vue教程3-02 vue动画

    vue教程3-02 vue动画 以下代码,已经用包管理器下载好vue,animate <!DOCTYPE html> <html lang="en"> &l ...

  6. vue教程3-01 路由、组件、bower包管理器使用

    vue教程3-01 路由.组件.包管理器 以下操作前提是 已经安装好node.js npm bower-> (前端)包管理器 下载: npm install bower -g 验证: bower ...

  7. vue教程2-08 自定义键盘信息、监听数据变化vm.$watch

    vue教程2-08 自定义键盘信息 @keydown.up @keydown.enter @keydown.a/b/c.... 自定义键盘信息: Vue.directive('on').keyCode ...

  8. vue教程2-07 自定义指令

    vue教程2-07 自定义指令 自定义指令: 一.属性: Vue.directive(指令名称,function(参数){ this.el -> 原生DOM元素 }); <div v-re ...

  9. vue教程2-07 微博评论功能

    vue教程2-07 微博评论功能 <!doctype html> <html> <head> <meta charset="utf-8"& ...

  10. vue教程2-06 过滤器

    vue教程2-06 过滤器 过滤器: vue提供过滤器: capitalize uppercase currency.... <div id="box"> {{msg| ...

随机推荐

  1. SDUT OJ 2616 简单计算

    简单计算 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 某天,XX 给YY 出了一道题,题目是: 给出n 个十进制的数,找出这n ...

  2. elementaryos必装软件

    所使用版本:elementaryos-0.4-stable-amd64.20160909.iso vmtools jdk sougouinput IntellijIEAD

  3. struts2 自定义类型转化 第三弹

    1.Struts2的类型转化,对于8种原生数据类型以及Date,String等常见类型,Struts2可以使用内建的类型转化器实现自动转化:但对于自定义的对象类型来说,就需要我们自己指定类型转化的的方 ...

  4. VC++动态链接库(DLL)编程深入浅出:Q&A(原创)

    Q1:extern “C” 是做什么用的? A1:一种情况是多个文件中,变量声明或者函数声明,需要extern “C”,这种情况在这里不做讨论. 在dll工程中,被extern "C&quo ...

  5. POJ3417Network(LCA+树上查分||树剖+线段树)

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  6. 1131 Subway Map(30 分)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  7. 1107 Social Clusters (30)(30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  8. ACM学习历程——NOJ1113 Game I(贪心 || 线段树)

    Description 尼克发明了这样一个游戏:在一个坐标轴上,有一些圆,这些圆的圆心都在x轴上,现在给定一个x轴上的点,保证该点没有在这些圆内(以及圆上),尼克可以以这个点为圆心做任意大小的圆,他想 ...

  9. 3170: [Tjoi 2013]松鼠聚会

    题目大意 给定n个点,找到一个点使这个点到其他所有点的切比雪夫距离之和最小. 题解 我们知道切比雪夫距离和曼哈顿距离的转化公式 \(1\)表示切比雪夫距离,\(2\)表示曼哈顿距离 我们有: \(x_ ...

  10. mongodb入门-关系型RDMS数据库对比及适用场景

    引言 最近工作接触到了mongodb数据库,记录下个人对其的理解和使用情况.虽然mongodb 出来的时间已经不短,但是相对mysql mssql oracle 这样传统的关系型数据库来说还是比较年轻 ...