curve.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/curve</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'}" @touchstart="touchLineA"></canvas>
  17. <!--#endif-->
  18. <!--#ifndef MP-ALIPAY -->
  19. <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" @touchstart="touchLineA"></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,20,0,15],
  92. legend:{
  93. show:true,
  94. padding:5,
  95. lineHeight:11,
  96. margin:0,
  97. },
  98. dataLabel:true,
  99. dataPointShape:true,
  100. background:'#FFFFFF',
  101. pixelRatio:_self.pixelRatio,
  102. categories: chartData.categories,
  103. series: chartData.series,
  104. animation: true,
  105. xAxis: {
  106. type:'grid',
  107. gridColor:'#CCCCCC',
  108. gridType:'dash',
  109. dashLength:8,
  110. boundaryGap:'justify'
  111. },
  112. yAxis: {
  113. gridType:'dash',
  114. gridColor:'#CCCCCC',
  115. dashLength:8,
  116. splitNumber:5,
  117. format:(val)=>{return val.toFixed(0)+'元'}
  118. },
  119. width: _self.cWidth*_self.pixelRatio,
  120. height: _self.cHeight*_self.pixelRatio,
  121. extra: {
  122. line:{
  123. type: 'curve'
  124. }
  125. }
  126. });
  127. //下面是默认选中索引
  128. let cindex=3;
  129. //下面是自定义文案
  130. let textList=[{text:'我是一个标题',color:null},{text:'自定义1:值1',color:'#2fc25b'},{text:'自定义2:值2',color:'#facc14'},{text:'自定义3:值3',color:'#f04864'}];
  131. //下面是event的模拟,tooltip的Y坐标值通过这个mp.changedTouches[0].y控制
  132. let tmpevent={mp:{changedTouches:[{x: 0, y: 80}]}};
  133. setTimeout(()=>{
  134. canvaLineA.showToolTip( tmpevent , {
  135. index:cindex,
  136. textList:textList
  137. });
  138. },200)
  139. },
  140. touchLineA(e) {
  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. });
  155. }else{
  156. uni.showToast({
  157. title:'数据格式错误',
  158. image:'../../../static/images/alert-warning.png'
  159. })
  160. }
  161. }
  162. }
  163. }
  164. </script>
  165. <style>
  166. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  167. .qiun-charts {
  168. width: 750upx;
  169. height: 500upx;
  170. background-color: #FFFFFF;
  171. }
  172. .charts {
  173. width: 750upx;
  174. height: 500upx;
  175. background-color: #FFFFFF;
  176. }
  177. </style>