line-scroll.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/line-scroll</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" >
  15. <!--#ifdef MP-ALIPAY -->
  16. <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio" :style="{'width':cWidth+'px','height':cHeight+'px'}" disable-scroll=true @touchstart="touchLineA" @touchmove="moveLineA" @touchend="touchEndLineA"></canvas>
  17. <!--#endif-->
  18. <!--#ifndef MP-ALIPAY -->
  19. <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" disable-scroll=true @touchstart="touchLineA" @touchmove="moveLineA" @touchend="touchEndLineA"></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 canvaLineA=null;
  39. export default {
  40. data() {
  41. return {
  42. cWidth:'',
  43. cHeight:'',
  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.cWidth=uni.upx2px(750);
  62. this.cHeight=uni.upx2px(500);
  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 LineA={categories:[],series:[]};
  74. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  75. LineA.categories=res.data.data.LineA.categories;
  76. LineA.series=res.data.data.LineA.series;
  77. _self.textarea = JSON.stringify(res.data.data.LineA);
  78. _self.showLineA("canvasLineA",LineA);
  79. },
  80. fail: () => {
  81. _self.tips="网络错误,小程序端请检查合法域名";
  82. },
  83. });
  84. },
  85. showLineA(canvasId,chartData){
  86. canvaLineA=new uCharts({
  87. $this:_self,
  88. canvasId: canvasId,
  89. type: 'line',
  90. fontSize:11,
  91. padding:[15,15,0,15],
  92. legend:{
  93. show:true,
  94. padding:5,
  95. lineHeight:11,
  96. margin:5,
  97. },
  98. dataLabel:true,
  99. dataPointShape:true,
  100. dataPointShapeType:'hollow',
  101. background:'#FFFFFF',
  102. pixelRatio:_self.pixelRatio,
  103. categories: chartData.categories,
  104. series: chartData.series,
  105. animation: true,
  106. enableScroll: true,//开启图表拖拽功能
  107. xAxis: {
  108. disableGrid:false,
  109. type:'grid',
  110. gridType:'dash',
  111. itemCount:4,
  112. scrollShow:true,
  113. scrollAlign:'left'
  114. },
  115. yAxis: {
  116. //disabled:true
  117. gridType:'dash',
  118. splitNumber:8,
  119. min:10,
  120. max:180,
  121. format:(val)=>{return val.toFixed(0)+'元'}//如不写此方法,Y轴刻度默认保留两位小数
  122. },
  123. width: _self.cWidth*_self.pixelRatio,
  124. height: _self.cHeight*_self.pixelRatio,
  125. extra: {
  126. line:{
  127. type: 'straight'
  128. }
  129. },
  130. });
  131. },
  132. touchLineA(e){
  133. canvaLineA.scrollStart(e);
  134. },
  135. moveLineA(e) {
  136. canvaLineA.scroll(e);
  137. },
  138. touchEndLineA(e) {
  139. canvaLineA.scrollEnd(e);
  140. //下面是toolTip事件,如果滚动后不需要显示,可不填写
  141. canvaLineA.touchLegend(e);
  142. canvaLineA.showToolTip(e, {
  143. format: function (item, category) {
  144. return category + ' ' + item.name + ':' + item.data
  145. }
  146. });
  147. },
  148. changeData(){
  149. if(isJSON(_self.textarea)){
  150. let newdata=JSON.parse(_self.textarea);
  151. canvaLineA.updateData({
  152. series: newdata.series,
  153. categories: newdata.categories,
  154. scrollPosition:'right',
  155. animation:false
  156. });
  157. }else{
  158. uni.showToast({
  159. title:'数据格式错误',
  160. image:'../../../static/images/alert-warning.png'
  161. })
  162. }
  163. }
  164. }
  165. }
  166. </script>
  167. <style>
  168. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  169. .qiun-charts {
  170. width: 750upx;
  171. height: 500upx;
  172. background-color: #FFFFFF;
  173. }
  174. .charts {
  175. width: 750upx;
  176. height: 500upx;
  177. background-color: #FFFFFF;
  178. }
  179. </style>