微信小程序如何使用e-charts插件

在e-charts官網已經發布了微信小程序的使用demo,根據配置項修改值達到自己想要的效果。

http://echarts.baidu.com/index.html

在github上下載dome,只需複製ec-canvas這個文件夾到微信小程序下,創建頁面引入插件,然後後根據api更改值。

 

以上三種柱狀圖又不同e-charts插件製作而成,

其中第一種和第二中是e-charts官方出的微信小程序demo更改而來。

其中幾個比較重要的api:

grid:直角座標系內繪圖網格,單個 grid 內最多可以放置上下兩個 X 軸,左右兩個 Y 軸。可以在網格上繪製折線圖柱狀圖散點圖(氣泡圖)

function initChart(canvas, width, height) {

  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  option = {
    color: ['#37a2da', '#32c5e9', '#67e0e3'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 座標軸指示器,座標軸觸發有效
        type: 'shadow'        // 默認爲直線,可選爲:'line' | 'shadow'
      }
    },
    legend: {  //圖標
      data: ['有功(kWh)']
    },
    grid: {
      left: 20,
      right: 40,
      bottom: 30,
      top: 40,
      containLabel: true
    },
    xAxis: [
      {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    yAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        textStyle:{
          height:10
        },
        data: ['1', '2', '3', '4', '5', '6', '7', 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
        axisLine: {
          lineStyle: {
            color: '#999'
          },
          lineHeight: 30
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    series: [
      {
        name: '有功(kWh)',
        type: 'bar',
        label: {
          normal: {
            show: false,//控制Y軸的數據顯示
            position: 'right'
          }
        },
        data: [1, 2, 88.1, 89.2, 87.9, 88.2, 88.1, 77.9, 87.9, 100.2, 98.4, 97.9, 88.9, 85.9, 81.1, 82, 81.5, 86.1, 90.1, 92.6, 82.4, 76.4, 73.1, 89],
        itemStyle: {
          // emphasis: {
          //   color: '#37a2da'
          // }
        }
      }
    ]
  };

  chart.setOption(option);
  return chart;
}
onReady() {
    console.log("===============")
    console.log(chart)
    wx.request({
      url: 'https://local:8080', //請更改域名
      data: { //此處填寫請求數據
      },
      method: 'GET',
      header: { 'content-type': 'application/json' },
      success: function (res) {
//請求成功的時候返回數據
        console.log(res)
        option.series[0].data = res.data.result.Pw;
        chart.setOption(option);
      },
    })
  },

在微信小程序的page外面定義函數,創建圖形,可以在生命週期函數裏更改option.series的值,使用chart.setOption(option)從新渲染數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章