Echarts使用

1.在繪製圖表之前,我們需要爲Echarts準備一個具備寬高的DOM容器

  <!-- 2. 爲ECharts準備一個具備大小(寬高)的Dom -->
      <div id="main" style="width: 750px;height:400px;"></div>

 2.我們在官網eacharts

下載echarts的文件

3.我們在我們的要使用的文件的地方進行引入

/ 1. 導入 echarts
import echarts from 'echarts'
import _ from 'lodash'

4.初步繪製一個簡單的折線圖

 // 需要合併的數據
      options: {
        title: {
          text: '用戶來源'  //折線圖的標題
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3' 顏色
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true //折線圖的y軸
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }

4.我是在vue中進行使用的,所以我就一我寫的項目爲例

  created() {},
  // 此時,頁面上的元素,已經被渲染完畢了!
  async mounted() {
    // 3. 基於準備好的dom,初始化echarts實例
    var myChart = echarts.init(document.getElementById('main'))

    const { data: res } = await this.$http.get(請求數據的地址)
    if (res.meta.status !== 200) {
      return this.$message.error('獲取折線圖數據失敗!')
    }

    // 4. 準備數據和配置項
    const result = _.merge(res.data, this.options) _.merge是引入的loadsh中的方法

    // 5. 展示數據
    myChart.setOption(result)
  },

 

發佈了84 篇原創文章 · 獲贊 0 · 訪問量 1812
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章