溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ECharts如何調(diào)用接口獲取后端數(shù)據(jù)

發(fā)布時(shí)間:2022-11-14 09:51:10 來(lái)源:億速云 閱讀:324 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了ECharts如何調(diào)用接口獲取后端數(shù)據(jù)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇ECharts如何調(diào)用接口獲取后端數(shù)據(jù)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

方法一:在mounted中使用定時(shí)器調(diào)用eacharts方法(定時(shí)器可以獲取到data中的數(shù)據(jù))

mounted () {
    setTimeout(() => {
      this.axisOption() // 樹(shù)狀
      this.pieOption()//餅圖
    }, 2000)
  },
//或者
mounted () {
    setTimeout(async () => {
      const res = await Getwx()
      this.Monthlist = res.Data.Monthlist
      this.Visitpvlist = res.Data.Visitpvlist
      this.drawLine();//柱狀圖
}, 0);
},

方法二:在調(diào)用數(shù)據(jù)的時(shí)候調(diào)用圖表(一個(gè)頁(yè)面的所有數(shù)據(jù)都在這一個(gè)接口中)

created () {
    this.GetProjectAll ()
  },
 
 methods: {
// 獲取數(shù)據(jù)
    async GetProjectAll () {
      const res = await GetProjectAll({ projectid: this.formdata.type })
      this.tableData = res.data.jrgrsl.data
      this.pieData = res.data.clbp.data
      this.$nextTick(() => {
        this.axisOption() // 樹(shù)狀
        this.pieOption()//餅圖
      })
    },
  }

方法三:使用chartes中的dataset數(shù)據(jù)集

<script>
import * as echarts from 'echarts'
import { getStatistics } from '@/api/home'
export default {
  data () {
    return {
      mainData: [],//折線圖數(shù)據(jù)
    }
  },
  mounted () {
     this.chartSetting();
  },
  created () {
    this.CeData()
  },
  methods: {
    // 返回?cái)?shù)據(jù)
    async CeData () {
      const { data } = await getStatistics()
      this.mainData = data.mainData
    },
    // 折現(xiàn)圖
    chartSetting () {
      // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
      this.mainChart = echarts.init(document.getElementById('main'))
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        dataset: [ // 數(shù)據(jù)
          {  source: this.mainData // 表數(shù)據(jù) },
          { transform: {
              type: 'sort'
            }
          }
        ],
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            axisLabel: { // 底部文字設(shè)置
              interval: 0, // 控制是否全部顯示
              fontSize: 12
            },
            axisLine: { // 底部橫線
              show: false
            },
            axisTick: { // 刻度線
              show: false
            }
          }
        ],
        yAxis: [
          { type: 'value' }
        ],
        series: [
          {
            name: '項(xiàng)目',
            type: 'line',
            stack: 'Total',
            smooth: true,
            lineStyle: {  width: 1,   color: '#2e3192' },
            showSymbol: false,
            label: {  show: true,  position: 'top' },
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {  offset: 0,  color: '#62a0f8' },
                {  offset: 1, color: '#b5d5ff' }
              ])
            },
            markPoint: { // 最大值和最小值標(biāo)注
              data: [
                { type: 'max', name: '最大值' }
              ]
            },
            emphasis: {  focus: 'series' }
          }
        ]
      }
      // 繪制圖表
      this.mainChart.setOption(option)
      const that = this
      window.addEventListener('resize', function () {
        that.mainChart.resize()
      })
    },
  }
}
</script>

方法四:在對(duì)應(yīng)圖表中,用ajax請(qǐng)求

 drawLine2 () {
      var chartDom = document.getElementById('main2');
      var myChart = echarts.init(chartDom);
      var option;
 
      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        grid: {
          left: "11%",
          width: "80%",
          height: "60%"
        },
        legend: [{
          data: ['單位: 秒'],
          top: "10",
          left: "10",
          itemWidth: 8,
          itemHeight: 8,
          icon: "rect",
          textStyle: {
            color: "#fff"
          }
        }, {
          data: ['增速%'],
          top: "10",
          right: "5%",
          itemWidth: 8,
          itemHeight: 8,
          icon: "rect",
          textStyle: {
            color: "#fff"
          }
        }],
        xAxis: [
          {
            type: 'category',
            data: [],
            axisPointer: {
              type: 'shadow'
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0,
              textStyle: {
                color: '#b8b8ba',
              },
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            min: 0,
            max: 10000,
            interval: 2000,
            axisLabel: {
              formatter: function (value) {
                return value + ""
              },
              textStyle: {
                color: '#b8b8ba',
              },
            },
            axisLine: {
              show: true
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                width: 0.5
              }
            },
            symbol: "triangle"
          },
          {
            type: 'value',
            min: 0.4,
            max: 0.9,
            interval: 0.1,
            axisLabel: {
              formatter: '{value}',
              textStyle: {
                color: '#b8b8ba',
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                width: 0.5
              }
            },
          }
        ],
        series: [
          {
            name: '單位: 秒',
            type: 'bar',
            data: [],
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#01c7f4' },
                { offset: 1, color: '#003fe2' }
              ]),
              borderRadius: 8
            },
            barWidth: 10
          },
          {
            name: '增速%',
            type: 'line',
            areaStyle: {},
            yAxisIndex: 1,
            data: [],
            itemStyle: {
              color: "#77ff3b",
            },
            lineStyle: {
              width: 1
            },
            symbolSize: 7,
            areaStyle: {
              opacity: 0.4,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 1, color: '#040d0c' },
                { offset: 0, color: '#5cd62c' }
              ])
            },
          }
        ]
      };
      const zoomSize = 6;
      myChart.on('click', function (params) {
        console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
        myChart.dispatchAction({
          type: 'dataZoom',
          startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
          endValue:
            dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
        });
      });
      option && myChart.setOption(option);
      $.ajax({
        type: "get",
        // async: false, //同步執(zhí)行
        url: "api/WxStatistics/GetStatisticsData",
        data: {},
        success: function (result) {
          myChart.setOption({
            xAxis: { data: result.Data.Monthlist },
            series: [
              {
                data: result.Data.Staytimeuvlist,
              },
              {
                data: [0.6, 0.65, 0.65, 0.68, 0.58, 0.61, 0.58, 0.6, 0.61, 0.65, 0.63, 0.55],
              }
            ]
          })
        },
        error: function (errorMsg) {
          alert("不好意思,圖表請(qǐng)求數(shù)據(jù)失敗啦!");
          myChart.hideLoading();
        }
      })
      window.addEventListener("resize", function () {
        myChart.resize();
      });
    },

注意

如果一個(gè)圖表需要展示不同數(shù)據(jù)時(shí),每獲取一次數(shù)據(jù),圖表都會(huì)重新渲染一次(例如下拉框中選取數(shù)據(jù)后,圖表切換對(duì)應(yīng)數(shù)據(jù))。
可能會(huì)出現(xiàn)There is a chart instance already initialized on the dom.這樣的警告,意思是dom上已經(jīng)初始化了一個(gè)圖表實(shí)例。
解決辦法:可以在每次渲染前先銷(xiāo)毀這個(gè)實(shí)例,然后再重新渲染。

var myChart //先注冊(cè)全局變量
 
 axisOption () {
      //在方法內(nèi)判斷,然后銷(xiāo)毀實(shí)例,然后再初始化
      if (myChart != null && myChart != "" && myChart != undefined) {
        myChart.dispose();//銷(xiāo)毀
      }
      // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
      myChart = echarts.init(document.getElementById('axisMain'))
      const option = { }
      // 繪制圖表
      myChart.setOption(option)
      window.addEventListener('resize', function () {
        myChart.resize()
      })
    },

關(guān)于“ECharts如何調(diào)用接口獲取后端數(shù)據(jù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“ECharts如何調(diào)用接口獲取后端數(shù)據(jù)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI