canvas.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="content">
  3. <!-- 从页面整洁度及代码可阅读性上来看,不再推荐使用原生canvas写法来调用uCharts -->
  4. <qiun-title-bar title="原生canvas用法示例"/>
  5. <canvas canvasId="canvasLine" id="canvasLine" class="charts" @tap="tap($event,'canvasLine')"/>
  6. <!-- #ifdef MP-WEIXIN -->
  7. <!-- 只有微信小程序有canvas2d模式 -->
  8. <qiun-title-bar title="启用type='2d'用法示例"/>
  9. <!-- 开启canvas2d模式后@tap的获取的点击坐标会不准确,开发者需按组件源码自行处理event的坐标为正确的坐标,并传入uCharts的实例中方可正确调用showTooltip方法 -->
  10. <canvas canvasId="canvasColumn" id="canvasColumn" type="2d" class="charts" @tap="tap($event,'canvasColumn')"/>
  11. <!-- #endif -->
  12. </view>
  13. </template>
  14. <script>
  15. import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
  16. var uChartsInstance = {};
  17. //下面是演示数据,您的项目不需要引用,数据需要您从服务器自行获取
  18. import demodata from '@/mockdata/demodata.json';
  19. export default {
  20. data() {
  21. return {
  22. cWidth:350,
  23. cHeight:300,
  24. pixelRatio:1,
  25. };
  26. },
  27. onLoad() {
  28. //#ifdef MP-ALIPAY || MP-WEIXIN
  29. this.pixelRatio=uni.getSystemInfoSync().pixelRatio;
  30. //#endif
  31. this.cWidth=uni.upx2px(750);
  32. this.cHeight=uni.upx2px(500);
  33. this.getServerData();
  34. },
  35. onReady() {
  36. this.getServerData()
  37. },
  38. methods: {
  39. //模拟从服务器获取数据
  40. getServerData() {
  41. setTimeout(() => {
  42. // 需要自行拼接chartsData
  43. let chartsData=JSON.parse(JSON.stringify(demodata.Line))
  44. let type2dData=JSON.parse(JSON.stringify(demodata.Column))
  45. this.drawCharts("canvasLine",chartsData);
  46. // #ifdef MP-WEIXIN
  47. this.draw2dCharts("canvasColumn",type2dData);
  48. // #endif
  49. }, 500);
  50. },
  51. drawCharts(id,data){
  52. const ctx = uni.createCanvasContext(id, this);
  53. uChartsInstance[id]=new uCharts({
  54. //$this:this, //v2.0版本不需要再传canvasId和$this了,v1.0因为传$this会导致实例混乱
  55. //canvasId: canvasId, //v2.0版本不需要再传canvasId和$this了,v1.0因为传$this会导致实例混乱
  56. type: 'line',
  57. context:ctx,//v2.0版本必须要传context
  58. fontSize:11,
  59. padding:[15,15,0,15],
  60. dataLabel:true,
  61. categories: data.categories,
  62. series: data.series,
  63. animation: true,
  64. width: this.cWidth,
  65. height: this.cHeight,
  66. extra: {
  67. line:{
  68. type: 'straight'
  69. }
  70. }
  71. });
  72. },
  73. draw2dCharts(id,data){
  74. const query = uni.createSelectorQuery().in(this);
  75. query.select('#' + id).fields({ node: true, size: true }).exec(res => {
  76. if (res[0]) {
  77. const canvas = res[0].node
  78. //v2.0版本必须要获取context并传入opts(option)中
  79. const ctx = canvas.getContext('2d')
  80. canvas.width = res[0].width * this.pixelRatio
  81. canvas.height = res[0].height * this.pixelRatio
  82. uChartsInstance[id]=new uCharts({
  83. //canvasId: canvasId, v2.0版本不需要再传canvasId和$this了,v1.0因为传$this会导致实例混乱
  84. type: 'column',
  85. padding:[15,5,0,15],
  86. dataLabel: true,
  87. width: this.cWidth*this.pixelRatio,
  88. height: this.cHeight*this.pixelRatio,
  89. pixelRatio:this.pixelRatio,
  90. canvas2d:true,//开启canvas2d
  91. context:ctx,//v2.0版本必须要传context
  92. fontSize:11,
  93. animation: true,
  94. categories: data.categories,
  95. series: data.series,
  96. xAxis: {
  97. disableGrid:true,
  98. },
  99. yAxis: {
  100. data:[{
  101. position:'right',
  102. axisLine:false,
  103. //注意,v2.0版本的format变成了formatter
  104. formatter:(val)=>{return val.toFixed(0)+'元'},
  105. }]
  106. },
  107. extra: {
  108. column: {
  109. type:'group',
  110. width: 20
  111. }
  112. }
  113. });
  114. }else{
  115. console.error("[uCharts]:未获取到context")
  116. }
  117. })
  118. },
  119. tap(e,id){
  120. uChartsInstance[id].showToolTip(e)
  121. }
  122. }
  123. }
  124. </script>
  125. <style>
  126. .content {
  127. display: flex;
  128. flex-direction: column;
  129. flex: 1;
  130. padding-bottom: 40px;
  131. }
  132. .charts{
  133. width: 750rpx;
  134. height: 500rpx;
  135. }
  136. </style>