图表示例

饼图右侧图例 - 图1

uCharts2.0以下版本是不支持改变图例位置的,朋友们可以暂时用view自己做个图例,这样就解决了这个问题,不要仅仅局限于插件本身功能。

标准数据格式

  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:false,
  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. });

完整代码示例

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