chart.vue 4.2 KB

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