图表示例

堆叠柱状图 - 图1

标准数据格式

  1. "ColumnStack": {
  2. "categories": ["2013", "2014", "2015", "2016", "2017", "2018"],
  3. "series": [{
  4. "name": "类别一",
  5. "data": [35, 36, 31, 33, 13, 34]
  6. }, {
  7. "name": "类别二",
  8. "data": [18, 27, 21, 24, 6, 28]
  9. }, {
  10. "name": "类别三",
  11. "data": [18, 27, 21, 24, 6, 28]
  12. }]
  13. }

调用方法

  1. canvaColumn=new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'column',
  5. legend:true,
  6. fontSize:11,
  7. background:'#FFFFFF',
  8. pixelRatio:_self.pixelRatio,
  9. animation: true,
  10. categories: chartData.categories,
  11. series: chartData.series,
  12. xAxis: {
  13. disableGrid:true,
  14. },
  15. yAxis: {
  16. //disabled:true
  17. },
  18. dataLabel: true,
  19. width: _self.cWidth*_self.pixelRatio,
  20. height: _self.cHeight*_self.pixelRatio,
  21. extra: {
  22. column: {
  23. type:'stack',
  24. width: _self.cWidth*_self.pixelRatio*0.5/chartData.categories.length
  25. }
  26. }
  27. });

完整代码示例

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