123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <template>
- <view class="qiun-columns">
- <!--#ifdef H5 -->
- <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
- <view class="qiun-title-dot-light">页面地址</view>
- </view>
- <view class="qiun-bg-white qiun-padding">
- <text>pages/basic/line/line-scroll</text>
- </view>
- <!--#endif-->
- <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
- <view class="qiun-title-dot-light">折线图带滚动条</view>
- </view>
- <view class="qiun-charts" >
- <!--#ifdef MP-ALIPAY -->
- <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>
- <!--#endif-->
- <!--#ifndef MP-ALIPAY -->
- <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" disable-scroll=true @touchstart="touchLineA" @touchmove="moveLineA" @touchend="touchEndLineA"></canvas>
- <!--#endif-->
- </view>
- <!--#ifdef H5 -->
- <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
- <view class="qiun-title-dot-light">标准数据格式</view>
- </view>
- <view class="qiun-bg-white qiun-padding">
- <textarea class="qiun-textarea" auto-height="true" maxlength="-1" v-model="textarea"/>
- </view>
- <view class="qiun-text-tips">Tips:修改后点击更新图表</view>
- <button class="qiun-button" @tap="changeData()">更新图表</button>
- <!--#endif-->
- </view>
- </template>
- <script>
- import uCharts from '@/components/u-charts/u-charts.js';
- import { isJSON } from '@/common/checker.js';
- var _self;
- var canvaLineA=null;
-
- export default {
- data() {
- return {
- cWidth:'',
- cHeight:'',
- pixelRatio:1,
- textarea:''
- }
- },
- onLoad() {
- _self = this;
- //#ifdef MP-ALIPAY
- uni.getSystemInfo({
- success: function (res) {
- if(res.pixelRatio>1){
- //正常这里给2就行,如果pixelRatio=3性能会降低一点
- //_self.pixelRatio =res.pixelRatio;
- _self.pixelRatio =2;
- }
- }
- });
- //#endif
- this.cWidth=uni.upx2px(750);
- this.cHeight=uni.upx2px(500);
- this.getServerData();
- },
- methods: {
- getServerData(){
- uni.request({
- url: 'https://www.ucharts.cn/data.json',
- data:{
- },
- success: function(res) {
- console.log(res.data.data)
- let LineA={categories:[],series:[]};
- //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
- LineA.categories=res.data.data.LineA.categories;
- LineA.series=res.data.data.LineA.series;
- _self.textarea = JSON.stringify(res.data.data.LineA);
- _self.showLineA("canvasLineA",LineA);
- },
- fail: () => {
- _self.tips="网络错误,小程序端请检查合法域名";
- },
- });
- },
- showLineA(canvasId,chartData){
- canvaLineA=new uCharts({
- $this:_self,
- canvasId: canvasId,
- type: 'line',
- fontSize:11,
- padding:[15,15,0,15],
- legend:{
- show:true,
- padding:5,
- lineHeight:11,
- margin:5,
- },
- dataLabel:true,
- dataPointShape:true,
- dataPointShapeType:'hollow',
- background:'#FFFFFF',
- pixelRatio:_self.pixelRatio,
- categories: chartData.categories,
- series: chartData.series,
- animation: true,
- enableScroll: true,//开启图表拖拽功能
- xAxis: {
- disableGrid:false,
- type:'grid',
- gridType:'dash',
- itemCount:4,
- scrollShow:true,
- scrollAlign:'left'
- },
- yAxis: {
- //disabled:true
- gridType:'dash',
- splitNumber:8,
- min:10,
- max:180,
- format:(val)=>{return val.toFixed(0)+'元'}//如不写此方法,Y轴刻度默认保留两位小数
- },
- width: _self.cWidth*_self.pixelRatio,
- height: _self.cHeight*_self.pixelRatio,
- extra: {
- line:{
- type: 'straight'
- }
- },
- });
-
- },
- touchLineA(e){
- canvaLineA.scrollStart(e);
- },
- moveLineA(e) {
- canvaLineA.scroll(e);
- },
- touchEndLineA(e) {
- canvaLineA.scrollEnd(e);
- //下面是toolTip事件,如果滚动后不需要显示,可不填写
- canvaLineA.touchLegend(e);
- canvaLineA.showToolTip(e, {
- format: function (item, category) {
- return category + ' ' + item.name + ':' + item.data
- }
- });
- },
- changeData(){
- if(isJSON(_self.textarea)){
- let newdata=JSON.parse(_self.textarea);
- canvaLineA.updateData({
- series: newdata.series,
- categories: newdata.categories,
- scrollPosition:'right',
- animation:false
- });
- }else{
- uni.showToast({
- title:'数据格式错误',
- image:'../../../static/images/alert-warning.png'
- })
- }
- }
- }
- }
- </script>
- <style>
- /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
- .qiun-charts {
- width: 750upx;
- height: 500upx;
- background-color: #FFFFFF;
- }
-
- .charts {
- width: 750upx;
- height: 500upx;
- background-color: #FFFFFF;
- }
- </style>
|