ArcbarChart.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="arcbar_chart">
  3. <canvas :canvasId="canvasId" id="canvasId" :style="{ width: cWidth + 'px', height: cHeight + 'px' }" />
  4. <slot />
  5. </view>
  6. </template>
  7. <script>
  8. import uCharts from '@/plugins/stan-ucharts/u-charts/u-charts.js'; //可以优化放全局 uCharts ==>使用全局
  9. const arcbarCharts = {},
  10. optionAs = {};
  11. export default {
  12. name: 'ArcbarChart',
  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: 250
  43. },
  44. height: {
  45. //图标高度
  46. type: Number,
  47. default: 250
  48. },
  49. valueKey: {
  50. type: String,
  51. default: 'series'
  52. },
  53. canvasId: {
  54. type: String,
  55. default: `arcbar_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 colors = ['#facc14', '#2fc25b', '#f04864', '#8543e0', '#90ed7d', '#ff7600'].sort(() => Math.random() - 0.5);
  72. let defaultOption = {
  73. //通用基础项设置 basicAs
  74. $this: this, //this实例组件内使用图表,必须传入this实例
  75. canvasId: this.canvasId, //页面组件canvas-id,支付宝中为id
  76. type: 'arcbar', //图表类型,可选值为pie、line、column、area、ring、radar、arcbar、gauge、candle、bar、mix、rose、word
  77. padding: [15, 15, 0, 15], //画布填充边距,顺序为上右下左,同css,但必须4位
  78. colors: colors, //图表配色方案,不传则使用系统默认配置
  79. rotate: false, //是否横屏展示
  80. rotateLock: true, // 锁定横屏模式,如果在支付宝和百度小程序中使用横屏模式,请赋值true,否则每次都会旋转90度。跨端使用通过uni-app的条件编译来赋值
  81. enableScroll: false, //是否开启图表可拖拽滚动 默认false 支持line, area, column, candle图表类型(需配合绑定@touchstart, @touchmove, @touchend方法)
  82. enableMarkLine: false, //是否显示辅助线 默认false 支持line, area, column, candle, mix图表类型
  83. animation: true, //是否动画展示
  84. duration: 1000, //动画展示时长单位毫秒
  85. dataLabel: true, //是否在图表中显示数据标签内容值
  86. fontSize: 12, //全局默认字体大小(可选,单位为px,默认13px)高分屏不必乘像素比,自动根据pixelRatio计算
  87. background: '#ffffff', //canvas背景颜色(如果页面背景颜色不是白色请设置为页面的背景颜色,默认#ffffff)无作用
  88. pixelRatio: 1, //像素比,默认为1,仅支付宝小程序需要大于1,其他平台必须为1
  89. width: this.cWidth, //canvas宽度,单位为px,支付宝高分屏需要乘像素比(pixelRatio)
  90. height: this.cHeight, //canvas高度,单位为px,支付宝高分屏需要乘像素比
  91. //数据列表配置项 dataAS
  92. series: this.dataAs[this.valueKey], //数据列表
  93. //titleAs
  94. title: {
  95. name: (this.dataAs[this.valueKey][0].data * 100).toFixed(0) + '%',
  96. color: this.basicAs.colors ? this.basicAs.colors[0] : colors[0]
  97. },
  98. subtitle: {
  99. name: this.dataAs[this.valueKey][0].name
  100. },
  101. //扩展配置 extraAs 详情请看 http://doc.ucharts.cn/1172130
  102. extra: {}
  103. };
  104. let propsData = { ...this.basicAs, ...this.titleAs, ...this.legendAs, ...this.extraAs };
  105. optionAs[this.canvasId] = Object.assign(defaultOption, propsData);
  106. arcbarCharts[this.canvasId] = new uCharts(optionAs[this.canvasId]);
  107. },
  108. changeData(series) {
  109. arcbarCharts[this.canvasId].updateData({
  110. series
  111. });
  112. }
  113. }
  114. };
  115. </script>
  116. <style></style>