您好,登錄后才能下訂單哦!
這篇文章主要介紹了ECharts如何調用接口獲取后端數據的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇ECharts如何調用接口獲取后端數據文章都會有所收獲,下面我們一起來看看吧。
mounted () { setTimeout(() => { this.axisOption() // 樹狀 this.pieOption()//餅圖 }, 2000) }, //或者 mounted () { setTimeout(async () => { const res = await Getwx() this.Monthlist = res.Data.Monthlist this.Visitpvlist = res.Data.Visitpvlist this.drawLine();//柱狀圖 }, 0); },
created () { this.GetProjectAll () }, methods: { // 獲取數據 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() // 樹狀 this.pieOption()//餅圖 }) }, }
<script> import * as echarts from 'echarts' import { getStatistics } from '@/api/home' export default { data () { return { mainData: [],//折線圖數據 } }, mounted () { this.chartSetting(); }, created () { this.CeData() }, methods: { // 返回數據 async CeData () { const { data } = await getStatistics() this.mainData = data.mainData }, // 折現圖 chartSetting () { // 基于準備好的dom,初始化echarts實例 this.mainChart = echarts.init(document.getElementById('main')) const option = { tooltip: { trigger: 'axis', axisPointer: { type: 'cross', label: { backgroundColor: '#6a7985' } } }, dataset: [ // 數據 { source: this.mainData // 表數據 }, { transform: { type: 'sort' } } ], xAxis: [ { type: 'category', boundaryGap: false, axisLabel: { // 底部文字設置 interval: 0, // 控制是否全部顯示 fontSize: 12 }, axisLine: { // 底部橫線 show: false }, axisTick: { // 刻度線 show: false } } ], yAxis: [ { type: 'value' } ], series: [ { name: '項目', 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: { // 最大值和最小值標注 data: [ { type: 'max', name: '最大值' } ] }, emphasis: { focus: 'series' } } ] } // 繪制圖表 this.mainChart.setOption(option) const that = this window.addEventListener('resize', function () { that.mainChart.resize() }) }, } } </script>
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, //同步執行 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("不好意思,圖表請求數據失敗啦!"); myChart.hideLoading(); } }) window.addEventListener("resize", function () { myChart.resize(); }); },
如果一個圖表需要展示不同數據時,每獲取一次數據,圖表都會重新渲染一次(例如下拉框中選取數據后,圖表切換對應數據)。
可能會出現There is a chart instance already initialized on the dom.這樣的警告,意思是dom上已經初始化了一個圖表實例。
解決辦法:可以在每次渲染前先銷毀這個實例,然后再重新渲染。
var myChart //先注冊全局變量 axisOption () { //在方法內判斷,然后銷毀實例,然后再初始化 if (myChart != null && myChart != "" && myChart != undefined) { myChart.dispose();//銷毀 } // 基于準備好的dom,初始化echarts實例 myChart = echarts.init(document.getElementById('axisMain')) const option = { } // 繪制圖表 myChart.setOption(option) window.addEventListener('resize', function () { myChart.resize() }) },
關于“ECharts如何調用接口獲取后端數據”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“ECharts如何調用接口獲取后端數據”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。