[关闭]
@zhouyy 2018-04-25T08:21:58.000000Z 字数 2878 阅读 474

在vue项目中使用echarts

vue


cankao:
https://blog.csdn.net/mr_wuch/article/details/70225364

安装echarts依赖

  1. npm install echarts -S

或者使用国内的淘宝镜像:

  1. //安装
  2. npm install -g cnpm --registry=https://registry.npm.taobao.org
  3. //使用
  4. cnpm install echarts -S

创建图表

全局引入

  1. main.js
  2. // 引入echarts
  3. import echarts from 'echarts'
  4. Vue.prototype.$echarts = echarts

Hello.vue

  1. <div id="myChart" :style="{width: '300px', height: '300px'}"></div>
  2. export default {
  3. name: 'hello',
  4. data () {
  5. return {
  6. msg: 'Welcome to Your Vue.js App'
  7. }
  8. },
  9. mounted(){
  10. this.drawLine();
  11. },
  12. methods: {
  13. drawLine(){
  14. // 基于准备好的dom,初始化echarts实例
  15. let myChart = this.$echarts.init(document.getElementById('myChart'))
  16. // 绘制图表
  17. myChart.setOption({
  18. title: { text: '在Vue中使用echarts' },
  19. tooltip: {},
  20. xAxis: {
  21. data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
  22. },
  23. yAxis: {},
  24. series: [{
  25. name: '销量',
  26. type: 'bar',
  27. data: [5, 20, 36, 10, 10, 20]
  28. }]
  29. });
  30. }
  31. }
  32. }

注意: 这里echarts初始化应在钩子函数mounted()中,这个钩子函数是在el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用

SPA按需引入

  1. <template>
  2. <q-page >
  3. <!-- content -->
  4. <div id="myChart" :style="{width: '800px', height: '500px'}"></div>
  5. </q-page>
  6. </template>
  7. <script>
  8. let echarts = require('echarts/lib/echarts')
  9. // 引入柱状图组件
  10. require('echarts/lib/chart/radar')
  11. require('echarts/lib/component/legend')
  12. var dataBJ = [
  13. [97,89,100,99,100]
  14. ];
  15. var dataSH = [
  16. [99,100,96,100,100]
  17. ];
  18. var lineStyle = {
  19. normal: {
  20. width: 1,
  21. opacity: 0.5
  22. }
  23. };
  24. export default {
  25. // name: 'PageName',
  26. mounted() {
  27. this.drawLine();
  28. },
  29. methods: {
  30. drawLine() {
  31. // 基于准备好的dom,初始化echarts实例
  32. let myChart = echarts.init(document.getElementById('myChart'))
  33. //自适应
  34. window.onresize = myChart.resize
  35. // 绘制图表
  36. myChart.setOption({
  37. backgroundColor: '#000',
  38. legend: {
  39. bottom: 5,
  40. data: ['企业指数', '均值'],
  41. itemGap: 20,
  42. textStyle: {
  43. color: '#fff',
  44. fontSize: 14
  45. }
  46. },
  47. radar: {
  48. indicator: [
  49. {name: '企业质量指数',min:80, max: 100},
  50. {name: '食品溯源指标', min:80, max: 100},
  51. {name: '食品安全指标',min:80, max: 100},
  52. {name: '厨房规范指标', min:80, max: 100},
  53. {name: '餐储油脂处理指标',min:80, max: 100}
  54. ],
  55. shape: 'circle',
  56. splitNumber: 3,
  57. name: {
  58. textStyle: {
  59. color: 'rgb(238, 197, 102)'
  60. }
  61. },
  62. splitLine: {
  63. lineStyle: {
  64. color: [
  65. 'rgba(238, 197, 102, 0.1)', 'rgba(238, 197, 102, 0.2)',
  66. 'rgba(238, 197, 102, 0.4)', 'rgba(238, 197, 102, 0.6)',
  67. 'rgba(238, 197, 102, 0.8)', 'rgba(238, 197, 102, 1)'
  68. ].reverse()
  69. }
  70. },
  71. splitArea: {
  72. areaStyle: {
  73. color: ['rgba(114, 172, 209, 0.2)']
  74. }
  75. },
  76. axisLine: {
  77. lineStyle: {
  78. color: 'rgba(238, 197, 102, 0.5)'
  79. }
  80. }
  81. },
  82. series: [
  83. {
  84. name: '企业指数',
  85. type: 'radar',
  86. lineStyle: lineStyle,
  87. data: dataBJ,
  88. symbol: 'rect',
  89. label: {
  90. normal: {
  91. show: true,
  92. formatter:function(params) {
  93. return params.value;
  94. }
  95. }
  96. },
  97. itemStyle: {
  98. normal: {
  99. color: '#F9713C'
  100. }
  101. },
  102. areaStyle: {
  103. normal: {
  104. opacity: 0.3
  105. }
  106. }
  107. },
  108. {
  109. name: '均值',
  110. type: 'radar',
  111. lineStyle: {
  112. normal: {
  113. type: 'dashed'
  114. }
  115. },
  116. data: dataSH,
  117. symbol: 'none',
  118. itemStyle: {
  119. normal: {
  120. color: '#B3E4A1'
  121. }
  122. },
  123. areaStyle: {
  124. normal: {
  125. opacity: 0.01
  126. }
  127. }
  128. }
  129. ]
  130. });
  131. }
  132. }
  133. }
  134. </script>
  135. <style>
  136. </style>

自适应问题

echart图表一旦绘制成功,内容就不会再变化。所以对于echart图表,其"响应式"应该是可以随着窗口调整不断被重新绘制,不是简单的调整宽高。(见:http://www.cnblogs.com/qjqcs/p/6279674.html
ECharts没有绑定resize事件,显示区域大小发生改变内部并不知道,使用方可以根据自己的需求绑定关心的事件,主动调用resize达到自适应的效果,常见如window.onresize = myChart.resize。
(见:https://www.cnblogs.com/yigexiaojiangshi/p/7249094.html

注意:
如果这个页面有多个图表:

  1. //多图表自适应
  2. window.addEventListener("resize", function () {
  3. myEnergyChart.resize();
  4. myEnergyChart2.resize();
  5. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注