index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div
  3. class="el-progress"
  4. :class="[
  5. 'el-progress--' + type,
  6. status ? 'is-' + status : '',
  7. {
  8. 'el-progress--without-text': !showText,
  9. 'el-progress--text-inside': textInside,
  10. },
  11. ]"
  12. role="progressbar"
  13. :aria-valuenow="percentage"
  14. aria-valuemin="0"
  15. aria-valuemax="100"
  16. >
  17. <div class="el-progress-bar" v-if="type === 'line'">
  18. <div
  19. class="el-progress-bar__outer"
  20. :style="{ height: strokeWidth + 'px' }"
  21. >
  22. <div class="el-progress-bar__inner" :style="barStyle">
  23. <div class="el-progress-bar__innerText" v-if="showText && textInside">
  24. {{ content }}
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. <div
  30. class="el-progress-circle"
  31. :style="{ height: width + 'px', width: width + 'px' }"
  32. v-else
  33. >
  34. <svg viewBox="0 0 100 100">
  35. <path
  36. class="el-progress-circle__track"
  37. :d="trackPath"
  38. stroke="#e5e9f2"
  39. :stroke-width="relativeStrokeWidth"
  40. fill="none"
  41. :style="trailPathStyle"
  42. ></path>
  43. <path
  44. class="el-progress-circle__path"
  45. :d="trackPath"
  46. :stroke="stroke"
  47. fill="none"
  48. :stroke-linecap="strokeLinecap"
  49. :stroke-width="percentage ? relativeStrokeWidth : 0"
  50. :style="circlePathStyle"
  51. ></path>
  52. </svg>
  53. </div>
  54. <div
  55. class="el-progress__text"
  56. v-if="showText && !textInside"
  57. :style="{ fontSize: progressTextSize + 'px' }"
  58. >
  59. <template v-if="!status">{{ content }}</template>
  60. <i v-else :class="iconClass"></i>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. export default {
  66. name: 'SimpleProgress',
  67. props: {
  68. type: {
  69. type: String,
  70. default: 'line',
  71. validator: (val) => ['line', 'circle', 'dashboard'].indexOf(val) > -1,
  72. },
  73. // percentage: {
  74. // type: Number,
  75. // default: 0,
  76. // required: true,
  77. // validator: (val) => val >= 0 && val <= 100,
  78. // },
  79. status: {
  80. type: String,
  81. validator: (val) => ['success', 'exception', 'warning'].indexOf(val) > -1,
  82. },
  83. strokeWidth: {
  84. type: Number,
  85. default: 6,
  86. },
  87. strokeLinecap: {
  88. type: String,
  89. default: 'round',
  90. },
  91. textInside: {
  92. type: Boolean,
  93. default: false,
  94. },
  95. width: {
  96. type: Number,
  97. default: 126,
  98. },
  99. showText: {
  100. type: Boolean,
  101. default: true,
  102. },
  103. color: {
  104. type: [String, Array, Function],
  105. default: '',
  106. },
  107. format: Function,
  108. },
  109. data() {
  110. return {
  111. percentage: 0,
  112. }
  113. },
  114. watch: {
  115. percentage(val) {
  116. if (val < 0) this.percentage = 0
  117. if (val > 100) this.percentage = 100
  118. },
  119. },
  120. computed: {
  121. barStyle() {
  122. const style = {}
  123. style.width = this.percentage + '%'
  124. style.backgroundColor = this.getCurrentColor(this.percentage)
  125. return style
  126. },
  127. relativeStrokeWidth() {
  128. return ((this.strokeWidth / this.width) * 100).toFixed(1)
  129. },
  130. radius() {
  131. if (this.type === 'circle' || this.type === 'dashboard') {
  132. return parseInt(50 - parseFloat(this.relativeStrokeWidth) / 2, 10)
  133. } else {
  134. return 0
  135. }
  136. },
  137. trackPath() {
  138. const radius = this.radius
  139. const isDashboard = this.type === 'dashboard'
  140. return `
  141. M 50 50
  142. m 0 ${isDashboard ? '' : '-'}${radius}
  143. a ${radius} ${radius} 0 1 1 0 ${isDashboard ? '-' : ''}${radius * 2}
  144. a ${radius} ${radius} 0 1 1 0 ${isDashboard ? '' : '-'}${radius * 2}
  145. `
  146. },
  147. perimeter() {
  148. return 2 * Math.PI * this.radius
  149. },
  150. rate() {
  151. return this.type === 'dashboard' ? 0.75 : 1
  152. },
  153. strokeDashoffset() {
  154. const offset = (-1 * this.perimeter * (1 - this.rate)) / 2
  155. return `${offset}px`
  156. },
  157. trailPathStyle() {
  158. return {
  159. strokeDasharray: `${this.perimeter * this.rate}px, ${this.perimeter}px`,
  160. strokeDashoffset: this.strokeDashoffset,
  161. }
  162. },
  163. circlePathStyle() {
  164. return {
  165. strokeDasharray: `${
  166. this.perimeter * this.rate * (this.percentage / 100)
  167. }px, ${this.perimeter}px`,
  168. strokeDashoffset: this.strokeDashoffset,
  169. transition: 'stroke-dasharray 0.6s ease 0s, stroke 0.6s ease',
  170. }
  171. },
  172. stroke() {
  173. let ret
  174. if (this.color) {
  175. ret = this.getCurrentColor(this.percentage)
  176. } else {
  177. switch (this.status) {
  178. case 'success':
  179. ret = '#13ce66'
  180. break
  181. case 'exception':
  182. ret = '#ff4949'
  183. break
  184. case 'warning':
  185. ret = '#e6a23c'
  186. break
  187. default:
  188. ret = '#20a0ff'
  189. }
  190. }
  191. return ret
  192. },
  193. iconClass() {
  194. if (this.status === 'warning') {
  195. return 'el-icon-warning'
  196. }
  197. if (this.type === 'line') {
  198. return this.status === 'success'
  199. ? 'el-icon-circle-check'
  200. : 'el-icon-circle-close'
  201. } else {
  202. return this.status === 'success' ? 'el-icon-check' : 'el-icon-close'
  203. }
  204. },
  205. progressTextSize() {
  206. return this.type === 'line'
  207. ? 12 + this.strokeWidth * 0.4
  208. : this.width * 0.111111 + 2
  209. },
  210. content() {
  211. if (typeof this.format === 'function') {
  212. return this.format(this.percentage) || ''
  213. } else {
  214. return `${this.percentage}%`
  215. }
  216. },
  217. },
  218. methods: {
  219. getCurrentColor(percentage) {
  220. if (typeof this.color === 'function') {
  221. return this.color(percentage)
  222. } else if (typeof this.color === 'string') {
  223. return this.color
  224. } else {
  225. return this.getLevelColor(percentage)
  226. }
  227. },
  228. getLevelColor(percentage) {
  229. const colorArray = this.getColorArray().sort(
  230. (a, b) => a.percentage - b.percentage
  231. )
  232. for (let i = 0; i < colorArray.length; i++) {
  233. if (colorArray[i].percentage > percentage) {
  234. return colorArray[i].color
  235. }
  236. }
  237. return colorArray[colorArray.length - 1].color
  238. },
  239. getColorArray() {
  240. const color = this.color
  241. const span = 100 / color.length
  242. return color.map((seriesColor, index) => {
  243. if (typeof seriesColor === 'string') {
  244. return {
  245. color: seriesColor,
  246. percentage: (index + 1) * span,
  247. }
  248. }
  249. return seriesColor
  250. })
  251. },
  252. },
  253. }
  254. </script>