PieChart.vue 3.8 KB

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