图表示例

圆环图基本用法 - 图1

标准数据格式

  1. "Ring": {
  2. "series": [{
  3. "name": "一班",
  4. "data": 50
  5. }, {
  6. "name": "二班",
  7. "data": 30
  8. }, {
  9. "name": "三班",
  10. "data": 20
  11. }, {
  12. "name": "四班",
  13. "data": 18
  14. }, {
  15. "name": "五班",
  16. "data": 8
  17. }]
  18. }

调用方法

  1. new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'ring',
  5. fontSize:11,
  6. legend:true,
  7. title: {
  8. name: '70%',
  9. color: '#7cb5ec',
  10. fontSize: 25*_self.pixelRatio,
  11. offsetY:-20*_self.pixelRatio,
  12. },
  13. subtitle: {
  14. name: '收益率',
  15. color: '#666666',
  16. fontSize: 15*_self.pixelRatio,
  17. offsetY:30*_self.pixelRatio,
  18. },
  19. extra: {
  20. pie: {
  21. offsetAngle: -45,
  22. ringWidth: 40*_self.pixelRatio,
  23. lableWidth:15
  24. }
  25. },
  26. background:'#FFFFFF',
  27. pixelRatio:_self.pixelRatio,
  28. series: chartData.series,
  29. animation: true,
  30. width: _self.cWidth*_self.pixelRatio,
  31. height: _self.cHeight*_self.pixelRatio,
  32. disablePieStroke: true,
  33. dataLabel: true,
  34. });

lableWidth:15为必填参数,否则报错

完整代码示例

  1. <template>
  2. <view class="qiun-columns">
  3. <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
  4. <view class="qiun-title-dot-light">环形图</view>
  5. </view>
  6. <view class="qiun-charts" >
  7. <canvas canvas-id="canvasRing" id="canvasRing" class="charts" @touchstart="touchRing"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaRing=null;
  15. export default {
  16. data() {
  17. return {
  18. cWidth:'',
  19. cHeight:'',
  20. pixelRatio:1,
  21. serverData:'',
  22. }
  23. },
  24. onLoad() {
  25. _self = this;
  26. this.cWidth=uni.upx2px(750);
  27. this.cHeight=uni.upx2px(500);
  28. this.getServerData();
  29. },
  30. methods: {
  31. getServerData(){
  32. uni.request({
  33. url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  34. data:{
  35. },
  36. success: function(res) {
  37. console.log(res.data.data)
  38. let Ring={series:[]};
  39. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  40. Ring.series=res.data.data.Ring.series;
  41. _self.showRing("canvasRing",Ring);
  42. },
  43. fail: () => {
  44. _self.tips="网络错误,小程序端请检查合法域名";
  45. },
  46. });
  47. },
  48. showRing(canvasId,chartData){
  49. canvaRing=new uCharts({
  50. $this:_self,
  51. canvasId: canvasId,
  52. type: 'ring',
  53. fontSize:11,
  54. legend:true,
  55. title: {
  56. name: '70%',
  57. color: '#7cb5ec',
  58. fontSize: 25*_self.pixelRatio,
  59. offsetY:-20*_self.pixelRatio,
  60. },
  61. subtitle: {
  62. name: '收益率',
  63. color: '#666666',
  64. fontSize: 15*_self.pixelRatio,
  65. offsetY:30*_self.pixelRatio,
  66. },
  67. extra: {
  68. pie: {
  69. offsetAngle: -45,
  70. ringWidth: 40*_self.pixelRatio,
  71. lableWidth:15
  72. }
  73. },
  74. background:'#FFFFFF',
  75. pixelRatio:_self.pixelRatio,
  76. series: chartData.series,
  77. animation: true,
  78. width: _self.cWidth*_self.pixelRatio,
  79. height: _self.cHeight*_self.pixelRatio,
  80. disablePieStroke: true,
  81. dataLabel: true,
  82. });
  83. },
  84. touchRing(e){
  85. canvaRing.showToolTip(e, {
  86. format: function (item) {
  87. return item.name + ':' + item.data
  88. }
  89. });
  90. },
  91. }
  92. }
  93. </script>
  94. <style>
  95. page{background:#F2F2F2;width: 750upx;overflow-x: hidden;}
  96. .qiun-padding{padding:2%; width:96%;}
  97. .qiun-wrap{display:flex; flex-wrap:wrap;}
  98. .qiun-rows{display:flex; flex-direction:row !important;}
  99. .qiun-columns{display:flex; flex-direction:column !important;}
  100. .qiun-common-mt{margin-top:10upx;}
  101. .qiun-bg-white{background:#FFFFFF;}
  102. .qiun-title-bar{width:96%; padding:10upx 2%; flex-wrap:nowrap;}
  103. .qiun-title-dot-light{border-left: 10upx solid #0ea391; padding-left: 10upx; font-size: 32upx;color: #000000}
  104. .qiun-charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  105. .charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  106. </style>