图表示例

饼图基本用法 - 图1

标准数据格式

  1. "Pie": {
  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: 'pie',
  5. fontSize:11,
  6. legend:true,
  7. background:'#FFFFFF',
  8. pixelRatio:_self.pixelRatio,
  9. series: chartData.series,
  10. animation: true,
  11. width: _self.cWidth*_self.pixelRatio,
  12. height: _self.cHeight*_self.pixelRatio,
  13. dataLabel: true,
  14. extra: {
  15. pie: {
  16. lableWidth:15
  17. }
  18. },
  19. });

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="canvasPie" id="canvasPie" class="charts" @touchstart="touchPie"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaPie=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 Pie={series:[]};
  39. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  40. Pie.series=res.data.data.Pie.series;
  41. _self.showPie("canvasPie",Pie);
  42. },
  43. fail: () => {
  44. _self.tips="网络错误,小程序端请检查合法域名";
  45. },
  46. });
  47. },
  48. showPie(canvasId,chartData){
  49. canvaPie=new uCharts({
  50. $this:_self,
  51. canvasId: canvasId,
  52. type: 'pie',
  53. fontSize:11,
  54. legend:true,
  55. background:'#FFFFFF',
  56. pixelRatio:_self.pixelRatio,
  57. series: chartData.series,
  58. animation: true,
  59. width: _self.cWidth*_self.pixelRatio,
  60. height: _self.cHeight*_self.pixelRatio,
  61. dataLabel: true,
  62. extra: {
  63. pie: {
  64. lableWidth:15
  65. }
  66. },
  67. });
  68. },
  69. touchPie(e){
  70. canvaPie.showToolTip(e, {
  71. format: function (item) {
  72. return item.name + ':' + item.data
  73. }
  74. });
  75. },
  76. }
  77. }
  78. </script>
  79. <style>
  80. page{background:#F2F2F2;width: 750upx;overflow-x: hidden;}
  81. .qiun-padding{padding:2%; width:96%;}
  82. .qiun-wrap{display:flex; flex-wrap:wrap;}
  83. .qiun-rows{display:flex; flex-direction:row !important;}
  84. .qiun-columns{display:flex; flex-direction:column !important;}
  85. .qiun-common-mt{margin-top:10upx;}
  86. .qiun-bg-white{background:#FFFFFF;}
  87. .qiun-title-bar{width:96%; padding:10upx 2%; flex-wrap:nowrap;}
  88. .qiun-title-dot-light{border-left: 10upx solid #0ea391; padding-left: 10upx; font-size: 32upx;color: #000000}
  89. .qiun-charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  90. .charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  91. </style>