chart.vue 4.7 KB

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