启用X轴滚动条及拖拽 - 图1

坐标轴配置

  1. enableScroll: true,//开启图表拖拽功能
  2. xAxis: {
  3. type:'grid',
  4. gridType:'dash',
  5. itemCount:4,//x轴单屏显示数据的数量,默认为5个
  6. scrollShow:true,//新增是否显示滚动条,默认false
  7. scrollAlign:'left',//滚动条初始位置
  8. scrollBackgroundColor:'#F7F7FF',//默认为 #EFEBEF
  9. scrollColor:'#DEE7F7',//默认为 #A6A6A6
  10. }

canvas组件配置

  1. <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" disable-scroll=true @touchstart="touchLineA" @touchmove="moveLineA" @touchend="touchEndLineA"></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. <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" disable-scroll=true @touchstart="touchLineA" @touchmove="moveLineA" @touchend="touchEndLineA"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaLineA=null;
  15. /*下面是服务器返回的数据格式
  16. var Data={
  17. "LineA": {
  18. "categories": ["2012", "2013", "2014", "2015", "2016", "2017"],
  19. "series": [{
  20. "name": "成交量A",
  21. "data": [35, 8, 25, 37, 4, 20]
  22. }, {
  23. "name": "成交量B",
  24. "data": [70, 40, 65, 100, 44, 68]
  25. }, {
  26. "name": "成交量C",
  27. "data": [100, 80, 95, 150, 112, 132]
  28. }]
  29. }
  30. }
  31. */
  32. export default {
  33. data() {
  34. return {
  35. cWidth:'',
  36. cHeight:'',
  37. pixelRatio:1,
  38. serverData:'',
  39. }
  40. },
  41. onLoad() {
  42. _self = this;
  43. this.cWidth=uni.upx2px(750);
  44. this.cHeight=uni.upx2px(500);
  45. this.getServerData();
  46. },
  47. methods: {
  48. getServerData(){
  49. uni.request({
  50. url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  51. data:{
  52. },
  53. success: function(res) {
  54. console.log(res.data.data)
  55. //下面这个根据需要保存后台数据,我是为了模拟更新柱状图,所以存下来了
  56. _self.serverData=res.data.data;
  57. let LineA={categories:[],series:[]};
  58. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  59. LineA.categories=res.data.data.LineA.categories;
  60. LineA.series=res.data.data.LineA.series;
  61. _self.showLineA("canvasLineA",LineA);
  62. },
  63. fail: () => {
  64. _self.tips="网络错误,小程序端请检查合法域名";
  65. },
  66. });
  67. },
  68. showLineA(canvasId,chartData){
  69. canvaLineA=new uCharts({
  70. $this:_self,
  71. canvasId: canvasId,
  72. type: 'line',
  73. fontSize:11,
  74. legend:true,
  75. dataLabel:true,
  76. dataPointShape:true,
  77. background:'#FFFFFF',
  78. pixelRatio:_self.pixelRatio,
  79. categories: chartData.categories,
  80. series: chartData.series,
  81. animation: false,
  82. enableScroll: true,//开启图表拖拽功能
  83. xAxis: {
  84. disableGrid:false,
  85. type:'grid',
  86. gridType:'dash',
  87. itemCount:4,
  88. scrollShow:true,
  89. scrollAlign:'left',
  90. scrollBackgroundColor:'#F7F7FF',
  91. scrollColor:'#DEE7F7',
  92. },
  93. yAxis: {
  94. //disabled:true
  95. gridType:'dash',
  96. splitNumber:8,
  97. min:10,
  98. max:180,
  99. format:(val)=>{return val.toFixed(0)+'元'}
  100. },
  101. width: _self.cWidth*_self.pixelRatio,
  102. height: _self.cHeight*_self.pixelRatio,
  103. extra: {
  104. lineStyle: 'straight'
  105. },
  106. });
  107. },
  108. touchLineA(e){
  109. canvaLineA.scrollStart(e);
  110. },
  111. moveLineA(e) {
  112. canvaLineA.scroll(e);
  113. },
  114. touchEndLineA(e) {
  115. canvaLineA.scrollEnd(e);
  116. //下面是toolTip事件,如果滚动后不需要显示,可不填写
  117. canvaLineA.showToolTip(e, {
  118. format: function (item, category) {
  119. return category + ' ' + item.name + ':' + item.data
  120. }
  121. });
  122. },
  123. }
  124. }
  125. </script>
  126. <style>
  127. page{background:#F2F2F2;width: 750upx;overflow-x: hidden;}
  128. .qiun-padding{padding:2%; width:96%;}
  129. .qiun-wrap{display:flex; flex-wrap:wrap;}
  130. .qiun-rows{display:flex; flex-direction:row !important;}
  131. .qiun-columns{display:flex; flex-direction:column !important;}
  132. .qiun-common-mt{margin-top:10upx;}
  133. .qiun-bg-white{background:#FFFFFF;}
  134. .qiun-title-bar{width:96%; padding:10upx 2%; flex-wrap:nowrap;}
  135. .qiun-title-dot-light{border-left: 10upx solid #0ea391; padding-left: 10upx; font-size: 32upx;color: #000000}
  136. .qiun-charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  137. .charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  138. </style>