index.vue 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <div class="component">
  3. <h6 class="title">{{data.title}}</h6>
  4. <div :id="'dashboard' + index" ref="box"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import G2 from '@antv/g2'
  9. export default {
  10. name: '',
  11. props: ['data', 'index'],
  12. data () {
  13. return {
  14. }
  15. },
  16. components: {
  17. G2,
  18. },
  19. mounted () {
  20. this.$nextTick(function () {
  21. this.init()
  22. })
  23. },
  24. methods: {
  25. returnValue (val) {
  26. return String(val).substring(0, 2).split('').join('.') - 0
  27. },
  28. init () {
  29. var _that = this
  30. var Shape = G2.Shape
  31. var aValue = _that.returnValue(_that.data.value)
  32. // 自定义Shape 部分
  33. Shape.registerShape('point', 'pointer', {
  34. drawShape: function drawShape (cfg, group) {
  35. var center = this.parsePoint({ // 获取极坐标系下画布中心点
  36. x: 0,
  37. y: 0
  38. })
  39. // 绘制指针
  40. group.addShape('line', {
  41. attrs: {
  42. x1: center.x,
  43. y1: center.y,
  44. x2: cfg.x,
  45. y2: cfg.y,
  46. stroke: cfg.color,
  47. lineWidth: 5,
  48. lineCap: 'round'
  49. }
  50. })
  51. return group.addShape('circle', {
  52. attrs: {
  53. x: center.x,
  54. y: center.y,
  55. r: 8,
  56. stroke: cfg.color,
  57. lineWidth: 4.5,
  58. fill: '#fff'
  59. }
  60. })
  61. }
  62. })
  63. var chart = new G2.Chart({
  64. container: 'dashboard' + _that.index,
  65. forceFit: true,
  66. width: _that.$refs.box.clientWidth,
  67. height: 400,
  68. padding: [0, 0, 30, 0]
  69. })
  70. chart.source([{ value: aValue }])
  71. chart.coord('polar', {
  72. startAngle: -9 / 8 * Math.PI,
  73. endAngle: 1 / 8 * Math.PI,
  74. radius: 0.75
  75. })
  76. chart.scale('value', {
  77. min: 0,
  78. max: 9,
  79. tickInterval: 1,
  80. nice: false
  81. })
  82. chart.axis('1', false)
  83. chart.axis('value', {
  84. zIndex: 2,
  85. line: null,
  86. label: {
  87. offset: -16,
  88. textStyle: {
  89. fontSize: 18,
  90. textAlign: 'center',
  91. textBaseline: 'middle'
  92. }
  93. },
  94. subTickCount: 4,
  95. subTickLine: {
  96. length: -8,
  97. stroke: '#fff',
  98. strokeOpacity: 1
  99. },
  100. tickLine: {
  101. length: -17,
  102. stroke: '#fff',
  103. strokeOpacity: 1
  104. },
  105. grid: null
  106. })
  107. chart.legend(false)
  108. chart.point().position('value*1').shape('pointer').color('#1890FF').active(false)
  109. // 绘制仪表盘背景
  110. chart.guide().arc({
  111. zIndex: 0,
  112. top: false,
  113. start: [0, 0.945],
  114. end: [9, 0.945],
  115. style: { // 底灰色
  116. stroke: '#CBCBCB',
  117. lineWidth: 18
  118. }
  119. })
  120. // 绘制指标
  121. chart.guide().arc({
  122. zIndex: 1,
  123. start: [0, 0.945],
  124. end: [aValue, 0.945],
  125. style: {
  126. stroke: '#1890FF',
  127. lineWidth: 18
  128. }
  129. })
  130. // 绘制指标数字
  131. chart.guide().html({
  132. position: ['50%', '100%'],
  133. html: '<div style="width: 300px;text-align: center;">' + '<p style="font-size: 20px; color: #545454;margin: 0;">' + _that.data.title + '</p>' + '<p style="font-size: 36px;color: #545454;margin: 0;">' + aValue * 10 + '%</p>' + '</div>'
  134. })
  135. chart.render()
  136. },
  137. }
  138. }
  139. </script>
  140. <!-- Add "scoped" attribute to limit CSS to this component only -->
  141. <style lang="scss" scoped>
  142. @import "page.scss";
  143. </style>