引用方法

import uCharts from '../../components/u-charts/u-charts.js';

模板写法

< canvas canvas-id="canvasColumn" id="canvasColumn" class="charts">

样式写法

.charts{width: 750upx; height:500upx;background-color: #FFFFFF;}

实例化方法

new uCharts({具体参数详见API参数章节})

注意事项

  • 传入uCharts的cWidthcHeight,要与样式中的width和height相等,否则会造成canvas大小与预期不符。
  • 其他问题详见下章《常见问题》

代码示例

  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. <!--#ifdef MP-ALIPAY -->
  8. <canvas canvas-id="canvasColumn" id="canvasColumn" class="charts" :style="{'width':cWidth*pixelRatio+'px','height':cHeight*pixelRatio+'px', 'transform': 'scale('+(1/pixelRatio)+')','margin-left':-cWidth*(pixelRatio-1)/2+'px','margin-top':-cHeight*(pixelRatio-1)/2+'px'}"></canvas>
  9. <!--#endif-->
  10. <!--#ifndef MP-ALIPAY -->
  11. <canvas canvas-id="canvasColumn" id="canvasColumn" class="charts"></canvas>
  12. <!--#endif-->
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import uCharts from '../../components/u-charts/u-charts.js';
  18. var _self;
  19. var canvaColumn=null;
  20. export default {
  21. data() {
  22. return {
  23. cWidth:'',
  24. cHeight:'',
  25. pixelRatio:1,
  26. serverData:''
  27. }
  28. },
  29. onLoad() {
  30. _self = this;
  31. //#ifdef MP-ALIPAY
  32. uni.getSystemInfo({
  33. success: function (res) {
  34. if(res.pixelRatio>1){
  35. //正常这里给2就行,如果pixelRatio=3性能会降低一点
  36. //_self.pixelRatio =res.pixelRatio;
  37. _self.pixelRatio =2;
  38. }
  39. }
  40. });
  41. //#endif
  42. this.cWidth=uni.upx2px(750);
  43. this.cHeight=uni.upx2px(500);
  44. this.getServerData();
  45. },
  46. methods: {
  47. getServerData(){
  48. uni.request({
  49. url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  50. data:{
  51. },
  52. success: function(res) {
  53. console.log(res.data.data)
  54. //下面这个根据需要保存后台数据,我是为了模拟更新柱状图,所以存下来了
  55. _self.serverData=res.data.data;
  56. let Column={categories:[],series:[]};
  57. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  58. Column.categories=res.data.data.Column.categories;
  59. //这里的series数据是后台做好的,如果您的数据没有和前面我注释掉的格式一样,请自行拼接数据
  60. Column.series=res.data.data.Column.series;
  61. _self.showColumn("canvasColumn",Column);
  62. },
  63. fail: () => {
  64. console.log("网络错误,小程序端请检查合法域名");
  65. },
  66. });
  67. },
  68. showColumn(canvasId,chartData){
  69. canvaColumn=new uCharts({
  70. $this:_self,
  71. canvasId: canvasId,
  72. type: 'column',
  73. legend:true,
  74. fontSize:11,
  75. background:'#FFFFFF',
  76. pixelRatio:_self.pixelRatio,
  77. animation: true,
  78. categories: chartData.categories,
  79. series: chartData.series,
  80. xAxis: {
  81. disableGrid:true,
  82. },
  83. yAxis: {
  84. },
  85. dataLabel: true,
  86. width: _self.cWidth*_self.pixelRatio,
  87. height: _self.cHeight*_self.pixelRatio,
  88. extra: {
  89. column: {
  90. width: _self.cWidth*_self.pixelRatio*0.45/chartData.categories.length
  91. }
  92. }
  93. });
  94. },
  95. changeData(){
  96. canvaColumn.updateData({
  97. series: _self.serverData.ColumnB.series,
  98. categories: _self.serverData.ColumnB.categories
  99. });
  100. }
  101. }
  102. }
  103. </script>
  104. <style>
  105. page{background:#F2F2F2;width: 750upx;overflow-x: hidden;}
  106. .qiun-padding{padding:2%; width:96%;}
  107. .qiun-wrap{display:flex; flex-wrap:wrap;}
  108. .qiun-rows{display:flex; flex-direction:row !important;}
  109. .qiun-columns{display:flex; flex-direction:column !important;}
  110. .qiun-common-mt{margin-top:10upx;}
  111. .qiun-bg-white{background:#FFFFFF;}
  112. .qiun-title-bar{width:96%; padding:10upx 2%; flex-wrap:nowrap;}
  113. .qiun-title-dot-light{border-left: 10upx solid #0ea391; padding-left: 10upx; font-size: 32upx;color: #000000}
  114. /* 通用样式 */
  115. .qiun-charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  116. .charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  117. </style>