chart.vue 4.2 KB

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