图表示例

基本柱状图 - 图1

标准数据格式

  1. "Column": {
  2. "categories": ["2012", "2013", "2014", "2015", "2016", "2017"],
  3. "series": [{
  4. "name": "成交量1",
  5. "data": [15, {"value": 20,"color": "#f04864"}, 45, 37, 43, 34]
  6. }, {
  7. "name": "成交量2",
  8. "data": [30, {"value": 40,"color": "#facc14"}, 25, 14, 34, 18]
  9. }]
  10. }

调用方法

  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:'group',
  24. width: _self.cWidth*_self.pixelRatio*0.45/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="canvasColumn" id="canvasColumn" 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 Column={categories:[],series:[]};
  41. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  42. Column.categories=res.data.data.Column.categories;
  43. Column.series=res.data.data.Column.series;
  44. _self.showColumn("canvasColumn",Column);
  45. },
  46. fail: () => {
  47. _self.tips="网络错误,小程序端请检查合法域名";
  48. },
  49. });
  50. },
  51. showColumn(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:'group',
  75. width: _self.cWidth*_self.pixelRatio*0.45/chartData.categories.length
  76. }
  77. }
  78. });
  79. },
  80. touchColumn(e){
  81. canvaColumn.showToolTip(e, {
  82. format: function (item, category) {
  83. if(typeof item.data === 'object'){
  84. return category + ' ' + item.name + ':' + item.data.value
  85. }else{
  86. return category + ' ' + item.name + ':' + item.data
  87. }
  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>