chart.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. export default {
  8. data() {
  9. return {
  10. option: {
  11. title: {
  12. left: 'center'
  13. },
  14. tooltip: {
  15. trigger: 'item'
  16. },
  17. legend: {
  18. // orient: 'vertical',
  19. left: 'center',
  20. },
  21. color:[ '#3C8BF0', '#dd514c'],
  22. series: [
  23. {
  24. name: '访问来源',
  25. type: 'pie',
  26. radius: ['35%', '60%'],
  27. center: ['50%', '50%'],
  28. data: [
  29. {value: 1048, name: '电力监测'},
  30. {value: 735, name: '视频监测'},
  31. ],
  32. emphasis: {
  33. itemStyle: {
  34. shadowBlur: 10,
  35. shadowOffsetX: 0,
  36. shadowColor: 'rgba(0, 0, 0, 0.5)'
  37. }
  38. }
  39. }
  40. ]
  41. }
  42. }
  43. },
  44. onLoad() {
  45. },
  46. methods: {
  47. changeOption() {
  48. const data = this.option.series[0].data
  49. // 随机更新示例数据
  50. data.forEach((item, index) => {
  51. data.splice(index, 1, Math.random() * 40)
  52. })
  53. },
  54. onViewClick(options) {
  55. console.log(options)
  56. }
  57. }
  58. }
  59. </script>
  60. <script module="echarts" lang="renderjs">
  61. let myChart
  62. export default {
  63. mounted() {
  64. if (typeof window.echarts === 'function') {
  65. this.initEcharts()
  66. } else {
  67. // 动态引入较大类库避免影响页面展示
  68. const script = document.createElement('script')
  69. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  70. script.src = 'static/echarts.js'
  71. script.onload = this.initEcharts.bind(this)
  72. document.head.appendChild(script)
  73. }
  74. },
  75. methods: {
  76. initEcharts() {
  77. myChart = echarts.init(document.getElementById('echarts'))
  78. // 观测更新的数据在 view 层可以直接访问到
  79. myChart.setOption(this.option)
  80. },
  81. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  82. // 监听 service 层数据变更
  83. myChart.setOption(newValue)
  84. },
  85. onClick(event, ownerInstance) {
  86. // 调用 service 层的方法
  87. ownerInstance.callMethod('onViewClick', {
  88. test: 'test'
  89. })
  90. }
  91. }
  92. }
  93. </script>
  94. <style>
  95. .content {
  96. display: flex;
  97. flex-direction: column;
  98. align-items: center;
  99. justify-content: center;
  100. }
  101. .echarts {
  102. width: 100%;
  103. height: 500rpx;
  104. margin-top:70rpx;
  105. }
  106. </style>