小程序 使用echarts 數據動態變換


直接上效果 有動畫 gif有點卡 看不到出來  

要求效果: x軸底部只呈現四個時間段  但所有折線數據都要呈現  未來時間不參與繪制  年(12)月(28-31) 日(24)

以月示例 主要參考echarts文檔  小程序屬於輕容量 所以下載echarts.js 選擇定制版

html  注意地方是在json里面 先引入

<view class="content">
  <scroll-view scroll-x class="scroll">
    <view class="scroll-view">
     <view class="scroll-view-item {{timeIndex == index ? 'active' : ''}}" wx:for="{{list}}" wx:for-index="index" bindtap="timeChange" data-index='{{index}}'>{{item}}</view>
    </view>
  </scroll-view>
  <view class="mychart">
    <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
  </view>
</view>

json   eCharts文件官方下載的

{
  "usingComponents": {
    "ec-canvas": "eCharts/ec-canvas"
  },
  "navigationBarTitleText": "歷史記錄"
}

 

css

/* pages/eCharts/index.wxss */
.content{
  height: 100%;
  width: 100%;
  padding: 30rpx;
  font-size: 24rpx;
  box-sizing: border-box;
}
.scroll{
  box-sizing: border-box;
  width: 100%;
  padding: 0 20rpx;
  box-sizing: border-box;
}
.scroll-view{
  display: flex;
  box-sizing: border-box;
}
.scroll-view-item{
  padding: 0rpx 20rpx;
  border-radius: 20rpx;
  color: #A7A9A9;
  white-space: nowrap;
  margin-right: 10rpx;
}
.active{
  background: #44BBEE;
  color: #fff;
}
.mychart{
  width: 100%;
  height: 400rpx;
  box-sizing: border-box;
}
#mychart-dom-bar{
  font-size: 14px;
}

核心js

import * as echarts from 'eCharts/echarts.js'
const that = this
let datas = []
let num = 0
function name(nums,params) {
  datas = []
  for(let i=1;i<32;i++){
    datas.push(
      [nums+'-'+i, i>params?'':Math.floor(i*Math.random()*2)]
    )
  }
  num = 8
}
name(1, 31)
function time(params) { 
  datas= []
  for(let i=1;i<24;i++){
    datas.push(
      [i+':00', i>10?'':Math.floor(i+i*Math.random()*45)]
    )
  }
  num = 6
}
let chart = null;
function initChart (canvas, width, height) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  var option = {
    tooltip: {
        trigger: 'axis',
        formatter: function (params) {
          return params[0].value?params[0].name + ': ' + params[0].value: '';
        },
    },
    xAxis: {
      boundaryGap: false,
      data: datas.map(function (item) {
        let data = {
          value: item[0],
          textStyle: {
          }
        }
        return data;
      }),
      axisLabel: {
        interval: num
      },
      axisTick:{
        show: true,
        length: 0
      }
    },
    grid: {  
      left: '3%',  
      right: '4%',  
      bottom: '0',  
      top: '10%',
      containLabel: true 
    },  
    yAxis: {
      show: true,
      type: 'value',
      axisLine:{
        show: false,
      },
      axisTick:{
        show: false,
        length: 0
      },
      splitLine:{
        show: true
      }
    },
    series: {
      type: 'line',
      itemStyle : {  
        normal : {  
          color:'#44BBEE', 
            lineStyle:{  
                color:'#44BBEE'  
            },
        },
      }, 
      symbol: 'circle',
      symbolSize: 5,
      data: datas.map(function (item) {
          return item[1];
      })
    }
  }
  chart.setOption(option);
  return chart;
}
Page({
  
  /**
   * 頁面的初始數據
   */
  data: {
    list: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    ec: {
      onInit: initChart
    },
    timeIndex: ''
  },

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
    this.setData({
      timeIndex: this.data.list[0]
    })
  },
  timeChange (e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index + 1
    if(index>=8){
      name(index, 0)
    } else if(index==7){
      name(index, 6)
    } else {
      name(index, 31)
    }
    
    chart.setOption({
      xAxis: [{
        data: datas.map(function (item) {
          return item[0];
        }),
        axisLabel: {
          interval: num
        },
      }],
      series: [{
        data: datas.map(function (item) {
            return item[1];
        })
      }]
    })
    this.setData({
      timeIndex: e.currentTarget.dataset.index
    })
  },
})

說明:

1.由於interval 無法利用公式計算  只能預估 根據X軸所要顯示的時間段數量 預估對應的值   需求顯示四個 故月份 28-31 估值interval為8

2.未來時間不參與繪制 則設置時間點對應值為空 不給與0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM