123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <template>
- <view class="cmd-circle">
- <canvas :canvas-id="cid" :style="calCircleStyle"></canvas>
- </view>
- </template>
- <script>
-
- export default {
- name: "cmd-circle",
- props: {
-
- cid: {
- type: String,
- default: "defaultCanvas"
- },
-
- type: {
- type: String,
- validator: val => {
- return ['circle', 'dashboard'].includes(val);
- },
- default: 'circle'
- },
-
- percent: {
- type: Number,
- validator: val => {
- return val >= 0 && val <= 100;
- },
- default: 0
- },
-
- showInfo: {
- type: Boolean,
- default: true
- },
-
- fontColor: {
- type: String,
- default: "#595959"
- },
-
- fontSize: {
- type: Number,
- default: 14
- },
-
- status: {
- type: String,
- validator: val => {
- return ['normal', 'success', 'exception'].includes(val);
- },
- default: 'normal'
- },
-
- strokeWidth: {
- type: Number,
- default: 6
- },
-
- strokeColor: {
- type: String,
- default: ''
- },
-
- strokeBackground: {
- type: String,
- default: '#eeeeee'
- },
-
- strokeShape: {
- type: String,
- validator: val => {
- return ['round', 'square'].includes(val);
- },
- default: 'round'
- },
-
- width: {
- type: Number,
- default: 80
- },
-
- gapDegree: {
- type: Number,
- validator: val => {
- return val >= 0 && val <= 360;
- },
- default: 360
- },
-
- gapPosition: {
- type: String,
- validator: val => {
- return ['top', 'bottom', 'left', 'right'].includes(val);
- },
- default: 'top'
- }
- },
- data() {
- return {
-
- ctx: {},
-
- width2px: ""
- }
- },
- computed: {
-
- calCircleStyle() {
- return `width: ${this.width}px;
- height: ${this.width}px;`
- },
-
- calStatus() {
- let status = {}
- switch (this.status) {
- case 'normal':
- status = {
- color: "#1890ff",
- value: 1
- };
- break;
- case 'success':
- status = {
- color: "#52c41a",
- value: 2
- };
- break;
- case 'exception':
- status = {
- color: "#f5222d",
- value: 3
- };
- break;
- }
- return status
- },
-
- calGapDegree() {
- return this.gapDegree <= 0 ? 360 : this.gapDegree
- },
-
- calGapPosition() {
- let gapPosition = 0
- switch (this.gapPosition) {
- case 'bottom':
- gapPosition = 90;
- break;
- case 'left':
- gapPosition = 180;
- break;
- case 'top':
- gapPosition = 270;
- break;
- case 'right':
- gapPosition = 360;
- break;
- }
- return gapPosition
- },
- },
- watch: {
-
- percent(val) {
- this.drawStroke(val);
- }
- },
- mounted() {
-
- this.ctx = uni.createCanvasContext(this.cid, this)
-
- this.width2px = uni.upx2px(this.width)
-
- this.$nextTick(() => {
- this.drawStroke(this.percent)
- })
- },
- methods: {
-
- drawStroke(percent) {
- percent = percent >= 100 ? 100 : percent < 0 ? 0 : percent
-
- let color = this.strokeColor || this.calStatus.color
-
- if (this.showInfo) {
- switch (this.calStatus.value) {
- case 1:
- if (percent >= 100) {
-
- this.drawSuccess()
- percent = 100
- color = "#52c41a"
- } else {
-
- this.drawText(percent)
- }
- break;
- case 2:
-
- this.drawSuccess()
- percent = 100
- color = "#52c41a"
- break;
- case 3:
-
- this.drawException()
- percent = 0
- color = "#f5222d"
- break;
- default:
- break;
- }
- }
-
- let gapPosition = this.calGapPosition
- let gapDegree = this.calGapDegree
-
- if (this.type === "dashboard") {
- gapPosition = 135
- gapDegree = 270
- }
-
- this.ctx.setLineCap(this.strokeShape)
- this.ctx.setLineWidth(this.strokeWidth)
-
- this.ctx.translate(this.width2px, this.width2px)
-
- this.ctx.rotate(gapPosition * Math.PI / 180)
-
- this.ctx.beginPath()
- this.ctx.arc(0, 0, this.width2px - this.strokeWidth, 0, gapDegree * Math.PI / 180)
- this.ctx.setStrokeStyle(this.strokeBackground)
- this.ctx.stroke()
-
- this.ctx.beginPath()
- this.ctx.arc(0, 0, this.width2px - this.strokeWidth, 0, percent * gapDegree * Math.PI / 18000)
- this.ctx.setStrokeStyle(color)
- this.ctx.stroke()
-
- this.ctx.draw()
- },
-
- drawText(percent) {
- this.ctx.beginPath()
- this.ctx.setFontSize(this.fontSize)
- this.ctx.setFillStyle(this.fontColor)
- this.ctx.setTextAlign('center')
- this.ctx.fillText(`${percent}%`, this.width2px, this.width2px + this.fontSize / 2)
- this.ctx.stroke()
- },
-
- drawSuccess() {
- let x = this.width2px - this.fontSize / 2
- let y = this.width2px + this.fontSize / 2
- this.ctx.beginPath()
- this.ctx.setLineCap('round')
- this.ctx.setLineWidth(this.fontSize / 4)
- this.ctx.moveTo(this.width2px, y)
- this.ctx.lineTo(y, x)
- this.ctx.moveTo(this.width2px, y)
- this.ctx.lineTo(x, this.width2px)
- this.ctx.setStrokeStyle("#52c41a")
- this.ctx.stroke()
- },
-
- drawException() {
- let x = this.width2px - this.fontSize / 2
- let y = this.width2px + this.fontSize / 2
- this.ctx.beginPath()
- this.ctx.setLineCap('round')
- this.ctx.setLineWidth(this.fontSize / 4)
- this.ctx.moveTo(x, x)
- this.ctx.lineTo(y, y)
- this.ctx.moveTo(y, x)
- this.ctx.lineTo(x, y)
- this.ctx.setStrokeStyle("#f5222d")
- this.ctx.stroke()
- }
- }
- };
- </script>
- <style>
- .cmd-circle {
- display: inline-block;
- box-sizing: border-box;
- list-style: none;
- margin: 0;
- padding: 0;
- }
- </style>
|