1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| <template> <div> <div> <el-select v-model="echartsType" @change="changeEcharts"> <el-option v-for="(item, index) in optVal" :label="item.lable" :value="item.value" :key="index" ></el-option> </el-select> <div id="meetingRoomTrend" style="width: 500px; height: 500px"></div> </div> </div> </template>
<script> export default { name: "changeEcharts", data() { return { echartsType: "bar", optVal: [ { lable: "柱状图", value: "bar" }, { lable: "折线图", value: "line" }, { lable: "饼状图", value: "pie" }, ], myChart: "", optionsBar: "", optionsLine: "", optionsPie: "", userName: ["李白", "阿离", "孙尚香", "虞姬", "马可"], userScore: [100, 120, 99, 89, 55, 97], }; }, methods: { // echarts所有的配置项都在options,改变这个就相当于改变了echarts; getCharts() { this.myChart = this.$echarts.init( document.getElementById("meetingRoomTrend") ); this.optionsBar = { //配置标题 title: { text: "学员成绩表鸭", },
//配置x轴 xAxis: { type: "category", //category(类目名称),value(数值) data: this.userName, }, // 配置提示语 tooltip: { show: true, trigger: "axis", //触发提示类型 item (默认) axis (中间有条细线) axisPointer: { // 触发动画效果(阴影,xy轴交叉坐标) 必须trigger为axis才有动画效果 type: "shadow", }, }, // 配置y轴 yAxis: { type: "value", }, // 配置系列列表(这个是数组对象) series: [ { type: "bar", data: this.userScore, }, ], }; this.optionsLine = { //配置标题 title: { text: "学员成绩表鸭", },
//配置x轴 xAxis: { type: "category", //category(类目名称),value(数值) data: this.userName, }, // 配置提示语 tooltip: { show: true, trigger: "axis", //触发提示类型 item (默认) axis (中间有条细线) axisPointer: { // 触发动画效果(阴影,xy轴交叉坐标) 必须trigger为axis才有动画效果 type: "shadow", }, }, // 配置y轴 yAxis: { type: "value", }, // 配置系列列表(这个是数组对象) series: [ { type: "line", data: this.userScore, }, ], }; this.optionsPie = { title: { text: "Referer of a Website", subtext: "Fake Data", left: "center", }, tooltip: { trigger: "item", // confine: true, // formatter: "{a} <br/>{b} : {c} ({d}%)", }, legend: { orient: "vertical", left: "left", }, series: [ { name: "Access From", type: "pie", radius: "50%", data: [ { value: 1048, name: "Search Engine" }, { value: 735, name: "Direct" }, { value: 580, name: "Email" }, { value: 484, name: "Union Ads" }, { value: 300, name: "Video Ads" }, ], emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: "rgba(0, 0, 0, 0.5)", }, }, }, ], };
// this.myChart.setOption(options); }, changeEcharts() { /** * clear类似于v-show * dispose类似于v-if */ if (![null, "", undefined].includes(this.myChart)) { this.myChart.dispose(); ////解决echarts dom已经加载的报错 // this.myChart.clear(); } if (this.echartsType == "bar") { this.getCharts(); this.myChart.setOption(this.optionsBar); } else if (this.echartsType == "line") { this.getCharts(); this.myChart.setOption(this.optionsLine); } else if (this.echartsType == "pie") { this.getCharts(); this.myChart.setOption(this.optionsPie); } }, }, mounted() { this.getCharts(); this.myChart.setOption(this.optionsBar); }, }; </script>
<style scoped> </style>
|