西藏巴青项目

mpvue-echarts.vue 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view class="container">
  3. <view class="canvasView">
  4. <view class="canvas-bar">
  5. <view class="title">饼图示例</view>
  6. <button class="update-btn" type="primary" size="mini" @click="updatePie">修改饼状图数据</button>
  7. </view>
  8. <mpvueEcharts :onInit="pieInit" canvasId="pie" ref="pieChart" />
  9. </view>
  10. <view class="canvasView">
  11. <view class="title">折线图示例</view>
  12. <mpvueEcharts :onInit="lineInit" canvasId="line" ref="lineChart" />
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import * as echarts from '../components/echarts.esm.min.js';
  18. import mpvueEcharts from '../components/echarts.vue';
  19. const cityList = [{
  20. value: 55,
  21. name: '北京'
  22. }, {
  23. value: 38,
  24. name: '上海'
  25. }, {
  26. value: 20,
  27. name: '广州'
  28. }];
  29. let pieOption = {
  30. animation: false,
  31. backgroundColor: '#F8F8F8',
  32. color: ['#37A2DA', '#32C5E9', '#67E0E3', '#91F2DE', '#FFDB5C', '#FF9F7F'],
  33. series: [{
  34. label: {
  35. normal: {
  36. fontSize: 14
  37. }
  38. },
  39. type: 'pie',
  40. center: ['50%', '50%'],
  41. radius: [0, '60%'],
  42. data: [],
  43. itemStyle: {
  44. emphasis: {
  45. shadowBlur: 10,
  46. shadowOffsetX: 0,
  47. shadowColor: 'rgba(0, 2, 2, 0.3)'
  48. }
  49. }
  50. }]
  51. };
  52. let lineOption = {
  53. animation: false,
  54. color: ['#37A2DA', '#9FE6B8'],
  55. grid: {
  56. x: 35,
  57. x2: 10,
  58. y: 30,
  59. y2: 25
  60. },
  61. calculable: false,
  62. xAxis: [{
  63. type: 'category',
  64. data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  65. }],
  66. yAxis: [{
  67. type: 'value',
  68. splitArea: {
  69. show: true
  70. }
  71. }],
  72. series: [{
  73. name: '蒸发量',
  74. type: 'line',
  75. data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
  76. }, {
  77. name: '降水量',
  78. type: 'line',
  79. data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
  80. }]
  81. };
  82. export default {
  83. data() {
  84. return {
  85. echarts: echarts,
  86. updateStatus: false
  87. }
  88. },
  89. onLoad() {
  90. pieOption.series[0].data = cityList.slice(0);
  91. },
  92. methods: {
  93. goBrowser() {
  94. // #ifdef APP-PLUS
  95. plus.runtime.openURL('https://github.com/F-loat/mpvue-echarts');
  96. // #endif
  97. // #ifdef MP-WEIXIN
  98. uni.showModal({
  99. content: '请复制链接在浏览器里打开',
  100. showCancel: false
  101. })
  102. // #endif
  103. },
  104. updatePie() {
  105. // 参考 mpvue-charts 的懒加载示例
  106. // https://github.com/F-loat/mpvue-echarts/blob/master/examples/lazyLoad.vue
  107. if (this.updateStatus) {
  108. return;
  109. }
  110. pieOption.series[0].data.push({
  111. value: 20,
  112. name: '武汉'
  113. });
  114. pieOption.series[0].data.push({
  115. value: 10,
  116. name: '杭州'
  117. });
  118. this.$refs.pieChart.init();
  119. this.updateStatus = true;
  120. },
  121. pieInit(canvas, width, height) {
  122. let pieChart = echarts.init(canvas, null, {
  123. width: width,
  124. height: height
  125. })
  126. canvas.setChart(pieChart)
  127. pieChart.setOption(pieOption)
  128. return pieChart
  129. },
  130. lineInit(canvas, width, height) {
  131. let lineChart = echarts.init(canvas, null, {
  132. width: width,
  133. height: height
  134. })
  135. canvas.setChart(lineChart)
  136. lineChart.setOption(lineOption)
  137. return lineChart
  138. }
  139. },
  140. components: {
  141. mpvueEcharts
  142. }
  143. }
  144. </script>
  145. <style>
  146. page,
  147. view {
  148. display: flex;
  149. font-size: 28upx;
  150. }
  151. page {
  152. min-height: 100%;
  153. }
  154. .page-section-title {
  155. padding: 0 30upx;
  156. }
  157. .title {
  158. margin-left: 30upx;
  159. line-height: 1.5;
  160. color: #8f8f94;
  161. }
  162. .container {
  163. padding-bottom: 30upx;
  164. box-sizing: border-box;
  165. }
  166. .container,
  167. .canvasView {
  168. flex: 1;
  169. flex-direction: column;
  170. }
  171. .navigate {
  172. color: #007AFF;
  173. }
  174. .canvas-bar {
  175. display: flex;
  176. flex-direction: row;
  177. justify-content: space-between;
  178. }
  179. .update-btn {
  180. margin-right: 30upx;
  181. }
  182. </style>