在Vue中使用echarts

- 日理万妓 2021-07-25 12:46 741阅读 0赞

一,安装echarts

  1. cnpm i echarts@4.9.0 -S

" class="reference-link">watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70

(echarts通常用这种方式引入:import echarts from ‘echarts’。如果不行的话,就用这种引入方式:import * as echarts from ‘echarts’。)

二,新建echarts.vue

内容如下:

  1. <template>
  2. <div class="chart-container">
  3. <el-row>
  4. <el-col :span="12">
  5. <div id="chartColumn" style="width:500px; height:400px;"></div>
  6. </el-col>
  7. <el-col :span="12">
  8. <div id="chartBar" style="width:500px; height:400px;"></div>
  9. </el-col>
  10. <el-col :span="12">
  11. <div id="chartLine" style="width:500px; height:400px;"></div>
  12. </el-col>
  13. <el-col :span="12">
  14. <div id="chartPie" style="width:500px; height:400px;"></div>
  15. </el-col>
  16. </el-row>
  17. </div>
  18. </template>
  19. <script>
  20. import echarts from 'echarts'
  21. export default {
  22. data() {
  23. return {
  24. chartColumn: null,
  25. chartBar: null,
  26. chartLine: null,
  27. chartPie: null
  28. }
  29. },
  30. methods: {
  31. drawColumnChart() {
  32. this.chartColumn = echarts.init(document.getElementById('chartColumn'));
  33. this.chartColumn.setOption({
  34. title: { text: 'Column Chart' },
  35. tooltip: {},
  36. xAxis: {
  37. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  38. },
  39. yAxis: {},
  40. series: [{
  41. name: '销量',
  42. type: 'bar',
  43. data: [5, 20, 36, 10, 10, 20]
  44. }]
  45. });
  46. },
  47. drawBarChart() {
  48. this.chartBar = echarts.init(document.getElementById('chartBar'));
  49. this.chartBar.setOption({
  50. title: {
  51. text: 'Bar Chart',
  52. subtext: '数据来自网络'
  53. },
  54. tooltip: {
  55. trigger: 'axis',
  56. axisPointer: {
  57. type: 'shadow'
  58. }
  59. },
  60. legend: {
  61. data: ['2011年', '2012年']
  62. },
  63. grid: {
  64. left: '3%',
  65. right: '4%',
  66. bottom: '3%',
  67. containLabel: true
  68. },
  69. xAxis: {
  70. type: 'value',
  71. boundaryGap: [0, 0.01]
  72. },
  73. yAxis: {
  74. type: 'category',
  75. data: ['巴西', '印尼', '美国', '印度', '中国', '世界人口(万)']
  76. },
  77. series: [
  78. {
  79. name: '2011年',
  80. type: 'bar',
  81. data: [18203, 23489, 29034, 104970, 131744, 630230]
  82. },
  83. {
  84. name: '2012年',
  85. type: 'bar',
  86. data: [19325, 23438, 31000, 121594, 134141, 681807]
  87. }
  88. ]
  89. });
  90. },
  91. drawLineChart() {
  92. this.chartLine = echarts.init(document.getElementById('chartLine'));
  93. this.chartLine.setOption({
  94. title: {
  95. text: 'Line Chart'
  96. },
  97. tooltip: {
  98. trigger: 'axis'
  99. },
  100. legend: {
  101. data: ['邮件营销', '联盟广告', '搜索引擎']
  102. },
  103. grid: {
  104. left: '3%',
  105. right: '4%',
  106. bottom: '3%',
  107. containLabel: true
  108. },
  109. xAxis: {
  110. type: 'category',
  111. boundaryGap: false,
  112. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  113. },
  114. yAxis: {
  115. type: 'value'
  116. },
  117. series: [
  118. {
  119. name: '邮件营销',
  120. type: 'line',
  121. stack: '总量',
  122. data: [120, 132, 101, 134, 90, 230, 210]
  123. },
  124. {
  125. name: '联盟广告',
  126. type: 'line',
  127. stack: '总量',
  128. data: [220, 182, 191, 234, 290, 330, 310]
  129. },
  130. {
  131. name: '搜索引擎',
  132. type: 'line',
  133. stack: '总量',
  134. data: [820, 932, 901, 934, 1290, 1330, 1320]
  135. }
  136. ]
  137. });
  138. },
  139. drawPieChart() {
  140. this.chartPie = echarts.init(document.getElementById('chartPie'));
  141. this.chartPie.setOption({
  142. title: {
  143. text: 'Pie Chart',
  144. subtext: '纯属虚构',
  145. x: 'center'
  146. },
  147. tooltip: {
  148. trigger: 'item',
  149. formatter: "{a} <br/>{b} : {c} ({d}%)"
  150. },
  151. legend: {
  152. orient: 'vertical',
  153. left: 'left',
  154. data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
  155. },
  156. series: [
  157. {
  158. name: '访问来源',
  159. type: 'pie',
  160. radius: '55%',
  161. center: ['50%', '60%'],
  162. data: [
  163. { value: 335, name: '直接访问' },
  164. { value: 310, name: '邮件营销' },
  165. { value: 234, name: '联盟广告' },
  166. { value: 135, name: '视频广告' },
  167. { value: 1548, name: '搜索引擎' }
  168. ],
  169. itemStyle: {
  170. emphasis: {
  171. shadowBlur: 10,
  172. shadowOffsetX: 0,
  173. shadowColor: 'rgba(0, 0, 0, 0.5)'
  174. }
  175. }
  176. }
  177. ]
  178. });
  179. },
  180. drawCharts() {
  181. this.drawColumnChart()
  182. this.drawBarChart()
  183. this.drawLineChart()
  184. this.drawPieChart()
  185. },
  186. },
  187. mounted: function () {
  188. this.drawCharts()
  189. },
  190. updated: function () {
  191. this.drawCharts()
  192. }
  193. }
  194. </script>
  195. <style scoped>
  196. .chart-container {
  197. background-color:white;
  198. height: 100%;
  199. width: 100%;
  200. }
  201. .el-col {
  202. padding: 30px;
  203. }
  204. </style>

三,运行结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 1

发表评论

表情:
评论列表 (有 0 条评论,741人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue使用Echarts

    前端可视化是一个前端最基本的技能,要想做的好看,还是得借助一下百度家的echarts,那要怎么在Vue中使用echarts?这个官网没有给出实例,实例基本都是在jquery里面