chart.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. alert(1)
  52. console.log('子组件bindData.fire_water_count')
  53. console.log(this.bindData.fire_water_count)
  54. var dataname = ['消防水系统', '报警主机', '电气火灾', '其他', '监控视频']
  55. 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]
  56. 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]
  57. aa.sort(function (a, b) {
  58. return a-b;
  59. });
  60. aa=aa.pop();
  61. var datamax = [aa, aa, aa, aa, aa];
  62. var color = ['#3C8BF0', '#06CDF8', '#0ECB70', '#6744EF', '#FFD803'];
  63. var indicator = []
  64. for (var i = 0; i < dataname.length; i++) {
  65. indicator.push({
  66. name: dataname[i],
  67. max: datamax[i],
  68. color: color[i]
  69. })
  70. }
  71. function contains(arrays, obj) {
  72. var i = arrays.length;
  73. while (i--) {
  74. if (arrays[i] === obj) {
  75. return i;
  76. }
  77. }
  78. return false;
  79. }
  80. myChart = echarts.init(document.getElementById('echarts'))
  81. // 观测更新的数据在 view 层可以直接访问到
  82. myChart.setOption({
  83. tooltip: {
  84. formatter: function() {
  85. var html = '';
  86. for (var i = 0; i < datavaule.length; i++) {
  87. html += dataname[i] + ' : ' + datavaule[i] + '\n'
  88. }
  89. return html
  90. }
  91. },
  92. radar: {
  93. center: ["50%", "50%"],
  94. radius: "72%",
  95. splitArea: {
  96. areaStyle: {
  97. color: [
  98. '#6494fe', '#d1dfff',
  99. '#6494fe', '#d1dfff',
  100. ]
  101. }
  102. },
  103. axisLabel: {
  104. show: false,
  105. },
  106. axisLine: {
  107. show: true,
  108. lineStyle: {
  109. color: "rgba(255,255,255,.5)",
  110. }
  111. },
  112. splitLine: {
  113. lineStyle: {
  114. color: '#fff', // 分隔线颜色
  115. width: 1 // 分隔线线宽
  116. }
  117. },
  118. name: {
  119. formatter: function(value) {
  120. var i = contains(dataname, value);
  121. var percent = datavaule[i];
  122. return '{a|' + value + '}\n ' + '{b|' + percent + '}'
  123. },
  124. textStyle: {
  125. rich: {
  126. a: {
  127. // color: '#333',
  128. fontSize: 13,
  129. align: 'center',
  130. padding: [-5, 0, 0, 0]
  131. },
  132. b: {
  133. // color: 'red',
  134. fontSize: 13,
  135. align: 'center',
  136. padding: [-18, 0, 0, -7]
  137. }
  138. },
  139. },
  140. },
  141. indicator: indicator
  142. },
  143. series: [{
  144. type: "radar",
  145. symbol: "circle",
  146. symbolSize: 7,
  147. areaStyle: {
  148. normal: {
  149. color: 'rgba(255,2,2, 0.5)',
  150. },
  151. },
  152. itemStyle: {
  153. color: '#fff',
  154. borderColor: '#ff0202',
  155. borderWidth: 1,
  156. },
  157. lineStyle: {
  158. normal: {
  159. color: "#ff0202",
  160. width: 1
  161. }
  162. },
  163. data: [datavaule]
  164. }]
  165. })
  166. },
  167. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  168. // 监听 service 层数据变更
  169. myChart.setOption(newValue)
  170. },
  171. onClick(event, ownerInstance) {
  172. // 调用 service 层的方法
  173. ownerInstance.callMethod('onViewClick', {
  174. test: 'test'
  175. })
  176. }
  177. }
  178. }
  179. </script>
  180. <style>
  181. .content {
  182. display: flex;
  183. flex-direction: column;
  184. align-items: center;
  185. justify-content: center;
  186. }
  187. .echarts {
  188. width: 100%;
  189. height: 500rpx;
  190. margin-top: 70rpx;
  191. /* background:pink */
  192. }
  193. </style>