chart3.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts3" class="echarts"></view>
  4. </view>
  5. </template>
  6. <script>
  7. //start
  8. //end
  9. export default {
  10. data() {
  11. return {
  12. option: {
  13. // legend: {
  14. // data: ['bar', 'bar2', 'bar3', 'bar4'],
  15. // color: ['#1ECC22', '#0FC6C8','#0D88F0', '#FF1313', '#FF522A' ],
  16. // },
  17. grid: { //aa位置
  18. top: '2%',
  19. right: '-5%',
  20. bottom: '1%',
  21. left: '17%',
  22. },
  23. color: ['#1ECC22', '#0FC6C8','#0D88F0', '#FF1313', '#FF522A' ],
  24. xAxis: [{
  25. show: false
  26. }],
  27. yAxis: [{ //aay轴
  28. axisTick: 'none',
  29. axisLine: 'none',
  30. axisLabel: {
  31. verticalAlign: 'bottom',
  32. align: 'bottom',
  33. padding: [0, 0, 8, 5],
  34. textStyle: {
  35. color: '#333',
  36. fontSize:'16'
  37. }
  38. },
  39. data: ['正常 1569','其他 569','预警 156','故障 0','离线 12' ],
  40. },
  41. {
  42. //左侧柱状图的Y轴
  43. axisTick: 'none',
  44. axisLine: 'none',
  45. data: [70,20,8,0,12],
  46. axisLabel: {
  47. show: true,
  48. verticalAlign: 'bottom',
  49. align: 'right',
  50. padding: [0, 45, 8, 0],
  51. textStyle: {
  52. color: '#333',
  53. fontSize:'15'
  54. },
  55. formatter: function(value) {
  56. return value + '%'
  57. }
  58. }
  59. },
  60. ],
  61. series: [{
  62. type: 'bar',
  63. data: [1357, , , , ],
  64. barWidth: 6,
  65. itemStyle: {
  66. normal: {
  67. barBorderRadius: 6,
  68. }
  69. },
  70. z: 2
  71. }, {
  72. type: 'bar',
  73. data: [ ,1260, , , ],
  74. barWidth: 6,
  75. itemStyle: {
  76. normal: {
  77. barBorderRadius: 6,
  78. }
  79. },
  80. z: 2
  81. }, {
  82. type: 'bar',
  83. data: [ , ,36, , ],
  84. barWidth: 6,
  85. itemStyle: {
  86. normal: {
  87. barBorderRadius: 6,
  88. }
  89. },
  90. z: 2
  91. },
  92. {
  93. type: 'bar',
  94. data: [ , , ,36, ],
  95. barWidth: 6,
  96. itemStyle: {
  97. normal: {
  98. barBorderRadius: 6,
  99. }
  100. },
  101. z: 2
  102. },
  103. {
  104. type: 'bar',
  105. data: [ , , , ,100],
  106. barWidth: 6,
  107. itemStyle: {
  108. normal: {
  109. barBorderRadius: 6,
  110. }
  111. },
  112. z: 2
  113. },
  114. {
  115. type: 'bar',
  116. barGap: '-100%',
  117. data: [1554, 1554, 1554, 1554, 1554],
  118. barWidth: 6,
  119. itemStyle: {
  120. normal: {
  121. color: 'rgba(0,0,0, 0.15)',
  122. barBorderRadius: 6,
  123. }
  124. },
  125. z: 0
  126. },
  127. ]
  128. }
  129. }
  130. },
  131. onLoad() {
  132. // this.option.series.reverse()
  133. },
  134. methods: {
  135. changeOption() {
  136. const data = this.option.series[0].data
  137. // 随机更新示例数据
  138. data.forEach((item, index) => {
  139. data.splice(index, 1, Math.random() * 40)
  140. })
  141. },
  142. onViewClick(options) {
  143. console.log(options)
  144. }
  145. }
  146. }
  147. </script>
  148. <script module="echarts" lang="renderjs">
  149. let myChart
  150. export default {
  151. mounted() {
  152. if (typeof window.echarts === 'function') {
  153. this.initEcharts()
  154. } else {
  155. // 动态引入较大类库避免影响页面展示
  156. const script = document.createElement('script')
  157. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  158. script.src = 'static/echarts.js'
  159. script.onload = this.initEcharts.bind(this)
  160. document.head.appendChild(script)
  161. }
  162. },
  163. methods: {
  164. initEcharts() {
  165. myChart = echarts.init(document.getElementById('echarts3'))
  166. // 观测更新的数据在 view 层可以直接访问到
  167. myChart.setOption(this.option)
  168. },
  169. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  170. // 监听 service 层数据变更
  171. myChart.setOption(newValue)
  172. },
  173. onClick(event, ownerInstance) {
  174. // 调用 service 层的方法
  175. ownerInstance.callMethod('onViewClick', {
  176. test: 'test'
  177. })
  178. }
  179. }
  180. }
  181. </script>
  182. <style>
  183. .content {
  184. display: flex;
  185. flex-direction: column;
  186. align-items: center;
  187. justify-content: center;
  188. }
  189. .echarts {
  190. width: 100%;
  191. height: 500rpx;
  192. margin-top: 70rpx;
  193. /* background:pink */
  194. }
  195. </style>