1.在組件中創建該模塊
<template> <div id = "testChart"></div> </template>
2.導入echarts
前提是:已經在項目中配置過echarts
在<script></script>中導入echarts
<script> import {echartInit} from "../../../utils/echartUtils" </script>
3.初始化該模塊
export default { name: 'Test', //vue該組件名稱Test.vue mounted() { this.testChart = echartInit('testChart'); //初始化該echarts表 /*this.testChart.setOption(this.option); */ // 如果是寫死的數據,可以在這兒setOption()看效果 }, }
4.將data中的option數據返回
在返回的數據(請求的數據)成功后加入setOption();
如果是寫死的數據,可以在mounted中直接加入setOption()看結果;
如下為動態數據獲取
export default{ data() { return { option: { "grid": { "height": "67%", "right": "10%", "top": "8%", "width": "83%" }, "legend": { "data": ['新增','完成','未完成'], bottom: '5%' }, "series": [ { name: '新增', type: 'line', /*areaStyle: {},*/ smooth: false, data: [] }, { name: '完成', type: 'line', /*areaStyle: {},*/ //折線下顯示填充色 smooth: false, data: [] //可以寫固定的數據 }, { name: '未完成', type: 'line', smooth: false, // 折線,false不平滑的折線,true平滑的曲線 data: [] //可以寫固定的數據 }, ], "toolbox": { "emphasis": { "iconStyle": { "textAlign": "right", "textPosition": "left" } }, "orient": "vertical", "right": "2%", "show": true, "textStyle": { "align": "left" } }, "tooltip": { "axisPointer": { "type": "shadow" }, "trigger": "axis" }, "xAxis": { "axisLine": { "lineStyle": { "color": "rgb(0, 138, 205)" } }, "boundaryGap": true, "data": [], //可以寫固定的數據 "splitLine": { "show": false }, "splitNumber": 1, "type": "category" }, "yAxis": { "min": 0, "splitNumber": 8, "type": "value" } }, testChart: {} } }, }
5.通過getData()向后台獲取數據並返回,將獲取的數據返回setOption()
this.testChart.setOption(this.option);
