RingChart.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view class="ring_chart">
  3. <canvas :canvasId="canvasId" id="canvasId" :style="{ width: cWidth + 'px', height: cHeight + 'px' }" @touchstart="touchstart" />
  4. <slot />
  5. </view>
  6. </template>
  7. <script>
  8. import uCharts from '@/plugins/stan-ucharts/u-charts/u-charts.js'; //可以优化放全局 uCharts ==>使用全局
  9. const ringCharts = {},
  10. optionAs = {};
  11. export default {
  12. name: 'PieChart',
  13. props: {
  14. dataAs: {
  15. //数据
  16. type: Object,
  17. default: () => ({})
  18. },
  19. basicAs: {
  20. ////通用基础项设置
  21. type: Object,
  22. default: () => ({})
  23. },
  24. titleAs: {
  25. //只适用于适用于ring 环形、arcbar 弧线、gauge量规
  26. type: Object,
  27. default: () => ({})
  28. },
  29. legendAs: {
  30. //图例设置
  31. type: Object,
  32. default: () => ({})
  33. },
  34. extraAs: {
  35. //详情请看 http://doc.ucharts.cn/1172130
  36. type: Object,
  37. default: () => ({})
  38. },
  39. width: {
  40. //图标宽度
  41. type: Number,
  42. default: 750
  43. },
  44. height: {
  45. //图标高度
  46. type: Number,
  47. default: 500
  48. },
  49. valueKey: {
  50. type: String,
  51. default: 'series'
  52. },
  53. canvasId: {
  54. type: String,
  55. default: `ring_canvas_${Math.ceil(Math.random(5) * 10000)}`
  56. }
  57. },
  58. data() {
  59. return {};
  60. },
  61. computed: {
  62. cWidth() {
  63. return uni.upx2px(this.width);
  64. },
  65. cHeight() {
  66. return uni.upx2px(this.height);
  67. }
  68. },
  69. methods: {
  70. showCharts() {
  71. let defaultOption = {
  72. //通用基础项设置 basicAs
  73. $this: this, //this实例组件内使用图表,必须传入this实例
  74. canvasId: this.canvasId, //页面组件canvas-id,支付宝中为id
  75. type: 'ring', //图表类型,可选值为pie、line、column、area、ring、radar、arcbar、gauge、candle、bar、mix、rose、word
  76. padding: [15, 15, 0, 15], //画布填充边距,顺序为上右下左,同css,但必须4位
  77. colors: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d'], //图表配色方案,不传则使用系统默认配置
  78. rotate: false, //是否横屏展示
  79. rotateLock: true, // 锁定横屏模式,如果在支付宝和百度小程序中使用横屏模式,请赋值true,否则每次都会旋转90度。跨端使用通过uni-app的条件编译来赋值
  80. animation: true, //是否动画展示
  81. dataPointShape: true,
  82. duration: 1000, //动画展示时长单位毫秒
  83. fontSize: 12, //全局默认字体大小(可选,单位为px,默认13px)高分屏不必乘像素比,自动根据pixelRatio计算
  84. background: '#ffffff', //canvas背景颜色(如果页面背景颜色不是白色请设置为页面的背景颜色,默认#ffffff)无作用
  85. pixelRatio: 1, //像素比,默认为1,仅支付宝小程序需要大于1,其他平台必须为1
  86. width: this.cWidth, //canvas宽度,单位为px,支付宝高分屏需要乘像素比(pixelRatio)
  87. height: this.cHeight, //canvas高度,单位为px,支付宝高分屏需要乘像素比
  88. //数据列表配置项 dataAS
  89. series: this.dataAs[this.valueKey], //数据列表
  90. //图列配置 legendAs
  91. legend: {
  92. show: true, //是否显示各类别的图例标识
  93. position: 'top',
  94. float: 'left',
  95. padding: 10,
  96. margin: 0
  97. },
  98. //titleAs
  99. title: {
  100. name: ''
  101. },
  102. subtitle: {
  103. name: ''
  104. },
  105. //扩展配置 extraAs 详情请看 http://doc.ucharts.cn/1172130
  106. extra: {
  107. pie: {
  108. lableWidth: 15,
  109. ringWidth: 40, //圆环的宽度
  110. offsetAngle: 0 //圆环的角度
  111. }
  112. }
  113. };
  114. optionAs[this.canvasId] = Object.assign(defaultOption, this.basicAs, this.titleAs, this.legendAs, this.extraAs);
  115. ringCharts[this.canvasId] = new uCharts(optionAs[this.canvasId]);
  116. },
  117. touchstart(e) {
  118. ringCharts[this.canvasId].touchLegend(e, {
  119. animation: false
  120. });
  121. ringCharts[this.canvasId].showToolTip(e, {
  122. format: function(item) {
  123. if (typeof item.data === 'object') {
  124. return `${item.name}:${item.data.value}`;
  125. } else {
  126. return `${item.name}:${item.data}`;
  127. }
  128. }
  129. });
  130. },
  131. changeData(series) {
  132. ringCharts[this.canvasId].updateData({
  133. series
  134. });
  135. }
  136. }
  137. };
  138. </script>
  139. <style></style>