rotate.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view class="qiun-columns">
  3. <!--#ifdef H5 -->
  4. <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
  5. <view class="qiun-title-dot-light">页面地址</view>
  6. </view>
  7. <view class="qiun-bg-white qiun-padding">
  8. <text>pages/basic/line/rotate</text>
  9. </view>
  10. <!--#endif-->
  11. <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
  12. <view class="qiun-title-dot-light">横屏模式带滚动条</view>
  13. </view>
  14. <view class="qiun-charts-rotate" >
  15. <!--#ifdef MP-ALIPAY -->
  16. <canvas canvas-id="canvasLineB" id="canvasLineB" class="charts-rotate" :width="cWidth2*pixelRatio" :height="cHeight2*pixelRatio" :style="{'width':cWidth2+'px','height':cHeight2+'px'}" @touchstart="touchLineB" @touchmove="moveLineB" @touchend="touchEndLineB"></canvas>
  17. <!--#endif-->
  18. <!--#ifndef MP-ALIPAY -->
  19. <canvas canvas-id="canvasLineB" id="canvasLineB" class="charts-rotate" @touchstart="touchLineB" @touchmove="moveLineB" @touchend="touchEndLineB"></canvas>
  20. <!--#endif-->
  21. </view>
  22. <!--#ifdef H5 -->
  23. <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
  24. <view class="qiun-title-dot-light">标准数据格式</view>
  25. </view>
  26. <view class="qiun-bg-white qiun-padding">
  27. <textarea class="qiun-textarea" auto-height="true" maxlength="-1" v-model="textarea"/>
  28. </view>
  29. <view class="qiun-text-tips">Tips:修改后点击更新图表</view>
  30. <button class="qiun-button" @tap="changeData()">更新图表</button>
  31. <!--#endif-->
  32. </view>
  33. </template>
  34. <script>
  35. import uCharts from '@/components/u-charts/u-charts.js';
  36. import { isJSON } from '@/common/checker.js';
  37. var _self;
  38. var canvaLineB=null;
  39. export default {
  40. data() {
  41. return {
  42. cWidth2:'',//横屏图表
  43. cHeight2:'',//横屏图表
  44. pixelRatio:1,
  45. textarea:''
  46. }
  47. },
  48. onLoad() {
  49. _self = this;
  50. //#ifdef MP-ALIPAY
  51. uni.getSystemInfo({
  52. success: function (res) {
  53. if(res.pixelRatio>1){
  54. //正常这里给2就行,如果pixelRatio=3性能会降低一点
  55. //_self.pixelRatio =res.pixelRatio;
  56. _self.pixelRatio =2;
  57. }
  58. }
  59. });
  60. //#endif
  61. this.cWidth2=uni.upx2px(700);
  62. this.cHeight2=uni.upx2px(1100);
  63. this.getServerData();
  64. },
  65. methods: {
  66. getServerData(){
  67. uni.request({
  68. url: 'https://www.ucharts.cn/data.json',
  69. data:{
  70. },
  71. success: function(res) {
  72. console.log(res.data.data)
  73. let LineB={categories:[],series:[]};
  74. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  75. LineB.categories=res.data.data.LineB.categories;
  76. LineB.series=res.data.data.LineB.series;
  77. _self.textarea = JSON.stringify(res.data.data.LineB);
  78. _self.showLineB("canvasLineB",LineB);
  79. },
  80. fail: () => {
  81. _self.tips="网络错误,小程序端请检查合法域名";
  82. },
  83. });
  84. },
  85. showLineB(canvasId,chartData){
  86. canvaLineB=new uCharts({
  87. $this:_self,
  88. canvasId: canvasId,
  89. type: 'line',
  90. fontSize:11,
  91. legend:true,
  92. background:'#FFFFFF',
  93. pixelRatio:_self.pixelRatio,
  94. rotate:true,//开启图表横屏
  95. // #ifdef MP-ALIPAY || MP-BAIDU
  96. rotateLock:true,//百度及支付宝需要锁定旋转
  97. // #endif
  98. categories: chartData.categories,
  99. enableScroll: true,//开启图表拖拽功能
  100. animation: false,
  101. series: chartData.series,
  102. xAxis: {
  103. disableGrid:false,
  104. type:'grid',
  105. gridType:'dash',
  106. itemCount:4,//可不填写,配合enableScroll图表拖拽功能使用,x轴单屏显示数据的数量,默认为5个
  107. scrollShow:true,//新增是否显示滚动条,默认false
  108. scrollAlign:'left',
  109. //scrollBackgroundColor:'#F7F7FF',//可不填写,配合enableScroll图表拖拽功能使用,X轴滚动条背景颜色,默认为 #EFEBEF
  110. //scrollColor:'#DEE7F7',//可不填写,配合enableScroll图表拖拽功能使用,X轴滚动条颜色,默认为 #A6A6A6
  111. },
  112. yAxis: {
  113. //disabled:true
  114. },
  115. width: _self.cWidth2*_self.pixelRatio,
  116. height: _self.cHeight2*_self.pixelRatio,
  117. dataLabel: true,
  118. dataPointShape: true,
  119. extra: {
  120. line:{
  121. type: 'curve'
  122. }
  123. },
  124. });
  125. },
  126. touchLineB(e){
  127. canvaLineB.scrollStart(e);
  128. },
  129. moveLineB(e) {
  130. canvaLineB.scroll(e);
  131. },
  132. touchEndLineB(e) {
  133. canvaLineB.scrollEnd(e);
  134. //下面是toolTip事件,如果滚动后不需要显示,可不填写
  135. canvaLineB.showToolTip(e, {
  136. format: function (item, category) {
  137. return category + ' ' + item.name + ':' + item.data
  138. }
  139. });
  140. },
  141. changeData(){
  142. if(isJSON(_self.textarea)){
  143. let newdata=JSON.parse(_self.textarea);
  144. canvaLineB.updateData({
  145. series: newdata.series,
  146. categories: newdata.categories
  147. });
  148. }else{
  149. uni.showToast({
  150. title:'数据格式错误',
  151. image:'../../../static/images/alert-warning.png'
  152. })
  153. }
  154. }
  155. }
  156. }
  157. </script>
  158. <style>
  159. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  160. .qiun-charts-rotate {
  161. width: 700upx;
  162. height: 1100upx;
  163. background-color: #FFFFFF;
  164. padding: 25upx;
  165. }
  166. .charts-rotate {
  167. width: 700upx;
  168. height: 1100upx;
  169. background-color: #FFFFFF;
  170. }
  171. </style>