chart.vue 4.5 KB

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