DashChartDemo.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div :style="{ padding: '0 0 32px 32px' }">
  3. <v-chart :forceFit="true" :height="300" :data="chartData" :scale="scale">
  4. <v-coord type="polar" :startAngle="-202.5" :endAngle="22.5" :radius="0.75"></v-coord>
  5. <v-axis
  6. dataKey="value"
  7. :zIndex="2"
  8. :line="null"
  9. :label="axisLabel"
  10. :subTickCount="4"
  11. :subTickLine="axisSubTickLine"
  12. :tickLine="axisTickLine"
  13. :grid="null"
  14. ></v-axis>
  15. <v-axis dataKey="1" :show="false"></v-axis>
  16. <v-series gemo="point" position="value*1" shape="pointer" color="#1890FF" :active="false"></v-series>
  17. <v-guide
  18. type="arc"
  19. :zIndex="0"
  20. :top="false"
  21. :start="arcGuide1Start"
  22. :end="arcGuide1End"
  23. :vStyle="arcGuide1Style"
  24. ></v-guide>
  25. <v-guide type="arc" :zIndex="1" :start="arcGuide2Start" :end="getArcGuide2End" :vStyle="arcGuide2Style"></v-guide>
  26. <v-guide type="html" :position="htmlGuidePosition" :html="getHtmlGuideHtml()"></v-guide>
  27. </v-chart>
  28. </div>
  29. </template>
  30. <script>
  31. import { registerShape } from 'viser-vue'
  32. registerShape('point', 'pointer', {
  33. draw(cfg, container) {
  34. let point = cfg.points[0]
  35. point = this.parsePoint(point)
  36. const center = this.parsePoint({
  37. x: 0,
  38. y: 0,
  39. })
  40. container.addShape('line', {
  41. attrs: {
  42. x1: center.x,
  43. y1: center.y,
  44. x2: point.x,
  45. y2: point.y + 15,
  46. stroke: cfg.color,
  47. lineWidth: 5,
  48. lineCap: 'round',
  49. },
  50. })
  51. return container.addShape('circle', {
  52. attrs: {
  53. x: center.x,
  54. y: center.y,
  55. r: 9.75,
  56. stroke: cfg.color,
  57. lineWidth: 4.5,
  58. fill: '#fff',
  59. },
  60. })
  61. },
  62. })
  63. const scale = [
  64. {
  65. dataKey: 'value',
  66. min: 0,
  67. max: 9,
  68. tickInterval: 1,
  69. nice: false,
  70. },
  71. ]
  72. const data = [{ value: 7.0 }]
  73. export default {
  74. name: 'DashChartDemo',
  75. props: {
  76. datasource: {
  77. type: Number,
  78. default: 7,
  79. },
  80. title: {
  81. type: String,
  82. default: '',
  83. },
  84. },
  85. created() {
  86. if (!this.datasource) {
  87. this.chartData = data
  88. } else {
  89. this.chartData = [{ value: this.datasource }]
  90. }
  91. this.getChartData()
  92. },
  93. watch: {
  94. datasource: function (val) {
  95. this.chartData = [{ value: val }]
  96. this.getChartData()
  97. },
  98. },
  99. methods: {
  100. getChartData() {
  101. if (this.chartData && this.chartData.length > 0) {
  102. this.abcd = this.chartData[0].value * 10
  103. } else {
  104. this.abcd = 70
  105. }
  106. },
  107. getHtmlGuideHtml() {
  108. return (
  109. '<div style="width: 300px;text-align: center;">\n' +
  110. '<p style="font-size: 14px;color: #545454;margin: 0;">' +
  111. this.title +
  112. '</p>\n' +
  113. '<p style="font-size: 36px;color: #545454;margin: 0;">' +
  114. this.abcd +
  115. '%</p>\n' +
  116. '</div>'
  117. )
  118. },
  119. getArcGuide2End() {
  120. return [this.chartData[0].value, 0.945]
  121. },
  122. },
  123. data() {
  124. return {
  125. chartData: [],
  126. height: 400,
  127. scale: scale,
  128. abcd: 70,
  129. axisLabel: {
  130. offset: -16,
  131. textStyle: {
  132. fontSize: 18,
  133. textAlign: 'center',
  134. textBaseline: 'middle',
  135. },
  136. },
  137. axisSubTickLine: {
  138. length: -8,
  139. stroke: '#fff',
  140. strokeOpacity: 1,
  141. },
  142. axisTickLine: {
  143. length: -17,
  144. stroke: '#fff',
  145. strokeOpacity: 1,
  146. },
  147. arcGuide1Start: [0, 0.945],
  148. arcGuide1End: [9, 0.945],
  149. arcGuide1Style: {
  150. stroke: '#CBCBCB',
  151. lineWidth: 18,
  152. },
  153. arcGuide2Start: [0, 0.945],
  154. arcGuide2Style: {
  155. stroke: '#1890FF',
  156. lineWidth: 18,
  157. },
  158. htmlGuidePosition: ['50%', '100%'],
  159. htmlGuideHtml: `
  160. <div style="width: 300px;text-align: center;">
  161. <p style="font-size: 14px;color: #545454;margin: 0;">${this.title}</p>
  162. <p style="font-size: 36px;color: #545454;margin: 0;">${this.abcd}%</p>
  163. </div>
  164. `,
  165. }
  166. },
  167. }
  168. </script>