chart.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'chart',
  9. props: {
  10. bindData: {
  11. type: Object,
  12. default: null
  13. }
  14. },
  15. data() {
  16. return {
  17. aa:0
  18. }
  19. },
  20. onLoad() {
  21. },
  22. methods: {
  23. // changeOption() {
  24. // const data = this.option.series[0].data
  25. // // 随机更新示例数据
  26. // data.forEach((item, index) => {
  27. // data.splice(index, 1, Math.random() * 40)
  28. // })
  29. // },
  30. onViewClick(options) {
  31. console.log(options)
  32. }
  33. }
  34. }
  35. </script>
  36. <script module="echarts" lang="renderjs">
  37. let myChart
  38. export default {
  39. mounted() {
  40. if (typeof window.echarts === 'function') {
  41. this.initEcharts()
  42. } else {
  43. // 动态引入较大类库避免影响页面展示
  44. const script = document.createElement('script')
  45. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  46. script.src = 'static/echarts.min.js'
  47. script.onload = this.initEcharts.bind(this)
  48. document.head.appendChild(script)
  49. }
  50. },
  51. methods: {
  52. initEcharts() {
  53. var dataname = ['消防水系统', '报警主机', '电气火灾', '其他', '监控视频']
  54. var datavaule = [this.bindData.fire_water_count, this.bindData.alarm_host_count, this.bindData.electrical_fire_count, this.bindData.other_count, this.bindData.video_monitoring_count]
  55. var aa = [this.bindData.fire_water_count, this.bindData.alarm_host_count, this.bindData.electrical_fire_count, this.bindData.other_count, this.bindData.video_monitoring_count]
  56. aa=aa.sort().pop();
  57. var datamax = [aa, aa, aa, aa, aa];
  58. var color = ['#3C8BF0', '#06CDF8', '#0ECB70', '#6744EF', '#FFD803'];
  59. var indicator = []
  60. for (var i = 0; i < dataname.length; i++) {
  61. indicator.push({
  62. name: dataname[i],
  63. max: datamax[i],
  64. color: color[i]
  65. })
  66. }
  67. function contains(arrays, obj) {
  68. var i = arrays.length;
  69. while (i--) {
  70. if (arrays[i] === obj) {
  71. return i;
  72. }
  73. }
  74. return false;
  75. }
  76. myChart = echarts.init(document.getElementById('echarts'))
  77. // 观测更新的数据在 view 层可以直接访问到
  78. myChart.setOption({
  79. tooltip: {
  80. formatter: function() {
  81. var html = '';
  82. for (var i = 0; i < datavaule.length; i++) {
  83. html += dataname[i] + ' : ' + datavaule[i] + '\n'
  84. }
  85. return html
  86. }
  87. },
  88. radar: {
  89. center: ["50%", "50%"],
  90. radius: "72%",
  91. splitArea: {
  92. areaStyle: {
  93. color: [
  94. '#6494fe', '#d1dfff',
  95. '#6494fe', '#d1dfff',
  96. ]
  97. }
  98. },
  99. axisLabel: {
  100. show: false,
  101. },
  102. axisLine: {
  103. show: true,
  104. lineStyle: {
  105. color: "rgba(255,255,255,.5)",
  106. }
  107. },
  108. splitLine: {
  109. lineStyle: {
  110. color: '#fff', // 分隔线颜色
  111. width: 1 // 分隔线线宽
  112. }
  113. },
  114. name: {
  115. formatter: function(value) {
  116. var i = contains(dataname, value);
  117. var percent = datavaule[i];
  118. return '{a|' + value + '}\n ' + '{b|' + percent + '}'
  119. },
  120. textStyle: {
  121. rich: {
  122. a: {
  123. // color: '#333',
  124. fontSize: 13,
  125. align: 'center',
  126. padding: [-5, 0, 0, 0]
  127. },
  128. b: {
  129. // color: 'red',
  130. fontSize: 13,
  131. align: 'center',
  132. padding: [-18, 0, 0, -7]
  133. }
  134. },
  135. },
  136. },
  137. indicator: indicator
  138. },
  139. series: [{
  140. type: "radar",
  141. symbol: "circle",
  142. symbolSize: 7,
  143. areaStyle: {
  144. normal: {
  145. color: 'rgba(255,2,2, 0.5)',
  146. },
  147. },
  148. itemStyle: {
  149. color: '#fff',
  150. borderColor: '#ff0202',
  151. borderWidth: 1,
  152. },
  153. lineStyle: {
  154. normal: {
  155. color: "#ff0202",
  156. width: 1
  157. }
  158. },
  159. data: [datavaule]
  160. }]
  161. })
  162. },
  163. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  164. // 监听 service 层数据变更
  165. myChart.setOption(newValue)
  166. },
  167. onClick(event, ownerInstance) {
  168. // 调用 service 层的方法
  169. ownerInstance.callMethod('onViewClick', {
  170. test: 'test'
  171. })
  172. }
  173. }
  174. }
  175. </script>
  176. <style>
  177. .content {
  178. display: flex;
  179. flex-direction: column;
  180. align-items: center;
  181. justify-content: center;
  182. }
  183. .echarts {
  184. width: 100%;
  185. height: 500rpx;
  186. margin-top: 70rpx;
  187. /* background:pink */
  188. }
  189. </style>