背景 :

项目中需要使用到 charts图表,技术方案定的是用 uCharts 插件。


关于uCharts :

使用uCharts :

  • 下载插件 -> 进入DClound插件市场进行下载

image.png
image.png

  • 插件的使用, 分为两个版本: v1 和 v2 -> 两个版本使用方法不同。具体见以下示例

————————————————————- Demo 分割线————————————————————————————————-

V1 版本

  1. <template>
  2. <view>
  3. <canvas canvas-id="canvasRing" id="canvasRing" class="charts"></canvas>
  4. </view>
  5. </template>
  6. <script>
  7. import uCharts from '@/components/u-charts/u-charts.js';
  8. let _self;
  9. export default {
  10. data() {
  11. return {
  12. ballSeries: {
  13. series: []
  14. },
  15. ballCount: 40,
  16. cWidth: '',
  17. cHeight: '',
  18. pixelRatio: 1,
  19. eunmList: [{
  20. key: '5',
  21. name: '进度风险',
  22. data: 10,
  23. color: '#00CF7D'
  24. },
  25. {
  26. key: '4',
  27. name: '质量风险',
  28. data: 30,
  29. color: '#64E8FF'
  30. },
  31. {
  32. key: '3',
  33. name: '安全风险',
  34. data: 8,
  35. color: '#FFBD3F'
  36. },
  37. {
  38. key: '3',
  39. name: '环境风险',
  40. data: 24,
  41. color: '#AAE044'
  42. },
  43. {
  44. key: '3',
  45. name: '履职风险',
  46. data: 12,
  47. color: '#594DFF'
  48. },
  49. {
  50. key: '3',
  51. name: '未匹配',
  52. data: 2,
  53. color: '#FF6A6A'
  54. },
  55. ],
  56. }
  57. },
  58. mounted() {
  59. _self = this;
  60. this.cWidth = uni.upx2px(750);
  61. this.cHeight = uni.upx2px(300);
  62. this.showRing("canvasRing", this.eunmList, this.ballCount);
  63. },
  64. methods: {
  65. showRing(canvasId, chartData, count) {
  66. _self.pixelRatio = 1
  67. new uCharts({
  68. $this: _self,
  69. canvasId: canvasId,
  70. canvas2d: true,
  71. type: 'ring',
  72. fontSize: 11,
  73. padding: [5, 5, 5, 5],
  74. // 图例配置
  75. legend: {
  76. show: false,
  77. position: 'right',
  78. float: 'center',
  79. itemGap: 10,
  80. padding: 5,
  81. lineHeight: 26,
  82. margin: 5,
  83. borderWidth: 1
  84. },
  85. background: '#0c9aff',
  86. pixelRatio: _self.pixelRatio,
  87. series: chartData,
  88. animation: true,
  89. width: _self.cWidth * _self.pixelRatio,
  90. height: _self.cHeight * _self.pixelRatio,
  91. disablePieStroke: true,
  92. // 区域占比提示
  93. dataLabel: false,
  94. subtitle: {
  95. name: '会议纪要总数',
  96. color: '#ffffff',
  97. fontSize: 14 * _self.pixelRatio,
  98. },
  99. title: {
  100. name: count,
  101. color: '#ffffff',
  102. fontSize: 18 * _self.pixelRatio,
  103. fontWight: 500
  104. },
  105. extra: {
  106. pie: {
  107. offsetAngle: 0,
  108. // 圆环的宽度
  109. ringWidth: 8 * _self.pixelRatio,
  110. labelWidth: 15
  111. }
  112. },
  113. });
  114. },
  115. }
  116. }
  117. </script>

运行效果:
image.png

V2 版本

  1. <template>
  2. <view>
  3. <view class="charts-box">
  4. <qiun-data-charts type="bar" canvas2d :chartData="chartData" :opts="chartOpts" background="none" canvasId="through_stack_bar" />
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. import qiunDataCharts from './qiun-data-charts/components/qiun-data-charts/qiun-data-charts.vue';
  10. let _self;
  11. export default {
  12. components: {
  13. qiunDataCharts,
  14. },
  15. data() {
  16. return {
  17. cWidth: '',
  18. cHeight: '',
  19. chartData: {
  20. categories: [
  21. "火车东站项目",
  22. "萧山机场项目",
  23. "凤起路广场项目",
  24. "希尔顿酒店项目",
  25. ],
  26. series: [{
  27. "name": "进度风险",
  28. "legendShape": "square",
  29. "data": [
  30. 35,
  31. 36,
  32. 31,
  33. 33,
  34. ]
  35. },
  36. {
  37. "name": "质量风险",
  38. "legendShape": "square",
  39. "data": [
  40. 18,
  41. 27,
  42. 1,
  43. 4,
  44. ]
  45. },
  46. {
  47. "name": "安全风险",
  48. "legendShape": "square",
  49. "data": [
  50. 8,
  51. 7,
  52. 21,
  53. 24,
  54. ]
  55. }, {
  56. "name": "履职风险",
  57. "legendShape": "square",
  58. "data": [
  59. 18,
  60. 2,
  61. 2,
  62. 24,
  63. ]
  64. }
  65. ],
  66. },
  67. chartOpts: {
  68. "type": "bar",
  69. "canvasId": "through_stack_bar",
  70. "canvas2d": true,
  71. "background": "none",
  72. "dataLabel": false, // 隐藏图形上数据
  73. "color": [
  74. "#00C083",
  75. "#3996FF",
  76. "#FFBD3F",
  77. "#90A9FF",
  78. ],
  79. "xAxis": {
  80. "disabled": true,
  81. "disableGrid": true, // 隐藏x做标轴 及 x轴分割线
  82. },
  83. "yAxis": {
  84. "disableGrid": true,
  85. "showTitle": false,
  86. },
  87. "extra": {
  88. "bar": {
  89. "type": "stack", // 堆叠柱状图
  90. "width": 20,
  91. "axisLine": false,
  92. },
  93. }
  94. }
  95. }
  96. },
  97. methods: {
  98. },
  99. mounted() {
  100. _self = this;
  101. this.cWidth = uni.upx2px(750);
  102. this.cHeight = uni.upx2px(300);
  103. },
  104. }
  105. </script>
  106. <style lang="scss">
  107. .charts-box {
  108. background: #fff;
  109. }
  110. </style>
  • 下载v2版本,应用到项目中(我这里因为只有自己使用的是v2 , 业务上需要实现堆叠条状图,v1版本不支持。所以我把v2的包放在自己的目录里面,目的是分包打包,减小主应用体积)
  • 修改包引用路径(下载到的目录是umi-modules,使用是components)
  • 在业务组件中引用 qiun-data-charts
  • 配置chartData(数据) 、opts(图表配置项)、canvasId(图标Id)

运行效果:
image.png