vue中使用echarts并根據(jù)選擇條件動(dòng)態(tài)展示echarts圖表
前言
以前在使用echarts的時(shí)候,想要根據(jù)選擇條件讓echarts的圖片也跟著變化,我經(jīng)常是放在vue的watch中監(jiān)聽(tīng)數(shù)據(jù)的變化來(lái)實(shí)現(xiàn)的。但是這里我發(fā)現(xiàn)了一個(gè)更加簡(jiǎn)單的方法,不需要在vue的watch中監(jiān)聽(tīng)數(shù)據(jù)也可以實(shí)現(xiàn),具體代碼和運(yùn)行結(jié)果如下所示
echarts官網(wǎng)示例
1、在vue中使用echarts圖表,官網(wǎng)示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- vue在線引入 --> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script> <!-- 引入樣式 --> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <!-- 引入組件庫(kù) --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <!-- 在線引入echarts --> <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.1/echarts.min.js"></script> </head> <body> <div id="app"> <div id="main" style="width: 500px; height: 400px;"></div> </div> <script> var app = new Vue({ el: '#app', data: { myChart: null }, methods: { // 初始化echarts initEcharts() { var chartDom = document.getElementById('main'); // 將創(chuàng)建的echarts示例放到vue的data中,這樣在這個(gè)界面就想到于全局配置了 this.myChart = echarts.init(chartDom); }, // 配置echarts的option,展示數(shù)據(jù)圖表 setEchartsOption() { // 這里可以發(fā)送axios請(qǐng)求,然后通過(guò)響應(yīng)的數(shù)據(jù)賦值給對(duì)應(yīng)的x軸和y軸即可,由于這里沒(méi)有于后端聯(lián)調(diào),所以簡(jiǎn)單請(qǐng)求判斷一下, // 請(qǐng)求后端大概也是這個(gè)過(guò)程 var option; option = { tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], axisTick: { alignWithLabel: true } } ], yAxis: [ { type: 'value' } ], series: [ { name: 'Direct', type: 'bar', barWidth: '60%', data: [10, 52, 200, 334, 390, 330, 220] } ] }; option && this.myChart.setOption(option); }, }, mounted() { // 注意調(diào)用順序,先初始化echarts才給echarts賦值 this.initEcharts() this.setEchartsOption() } }) </script> </body> </html>
2、根據(jù)條件選擇展示echarts圖表,這里沒(méi)有發(fā)送axios請(qǐng)求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- vue在線引入 --> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script> <!-- 引入樣式 --> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <!-- 引入組件庫(kù) --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <!-- 在線引入echarts --> <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.1/echarts.min.js"></script> </head> <body> <div id="app"> <el-row> <el-select v-model="value" filterable placeholder="請(qǐng)選擇" @change="setEchartsOption"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> <el-button type="primary" @click="searchButton">查詢</el-button> </el-row> <el-row> <div id="main" style="width: 500px; height: 400px;"></div> </el-row> </div> <script> var app = new Vue({ el: '#app', data: { myChart: null, options: [{ value: '星期六', label: '星期六' }, { value: '星期日', label: '星期日' }], value: '星期六', zhouliu: { xValue: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], yValue: [10, 52, 200, 334, 390, 330, 220] }, zhouri: { xValue: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], yValue: [50, 80, 40, 99, 300, 88, 160] } }, methods: { // 初始化echarts initEcharts() { var chartDom = document.getElementById('main'); // 將創(chuàng)建的echarts示例放到vue的data中,這樣在這個(gè)界面就想到于全局配置了 this.myChart = echarts.init(chartDom); }, // 配置echarts的option,展示數(shù)據(jù)圖表 setEchartsOption() { // 這里可以發(fā)送axios請(qǐng)求,然后通過(guò)響應(yīng)的數(shù)據(jù)賦值給對(duì)應(yīng)的x軸和y軸即可,由于這里沒(méi)有于后端聯(lián)調(diào),所以簡(jiǎn)單請(qǐng)求判斷一下, // 請(qǐng)求后端大概也是這個(gè)過(guò)程 var xValue = null; var yValue = null; if (this.value === '星期六') { xValue = this.zhouliu.xValue yValue = this.zhouliu.yValue } else if (this.value === '星期日') { xValue = this.zhouri.xValue yValue = this.zhouri.yValue } var option; option = { tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', data: xValue, axisTick: { alignWithLabel: true } } ], yAxis: [ { type: 'value' } ], series: [ { name: 'Direct', type: 'bar', barWidth: '60%', data: yValue } ] }; option && this.myChart.setOption(option); }, searchButton() { // 為了避免重復(fù)初始化echarts, 我們?cè)邳c(diǎn)擊按鈕的時(shí)候可以調(diào)用echarts的clear方法清楚一下,然后再重新調(diào)用函數(shù)進(jìn)行對(duì)echarts的賦值 this.myChart.clear() // 這一句有沒(méi)有都可以 this.setEchartsOption() } }, mounted() { // 注意調(diào)用順序,先初始化echarts才給echarts賦值 this.initEcharts() this.setEchartsOption() } }) </script> </body> </html>
3、結(jié)合使用axios請(qǐng)求展示圖表
mysql數(shù)據(jù)庫(kù)數(shù)據(jù)數(shù)據(jù)如下所示
后端返回的數(shù)據(jù)格式
可以根據(jù)傳入的時(shí)間獲得需要的數(shù)據(jù)
{ "code": 200, "message": "查詢成功!", "data": [ { "id": 1, "count": 24, "createTime": "2023-02-15T14:17:50" }, { "id": 2, "count": 32, "createTime": "2023-02-16T14:18:19" }, { "id": 3, "count": 40, "createTime": "2023-02-17T14:18:30" }, { "id": 4, "count": 16, "createTime": "2023-02-18T14:18:44" }, { "id": 5, "count": 88, "createTime": "2023-02-19T14:18:59" }, { "id": 6, "count": 8, "createTime": "2023-02-20T14:19:11" }, { "id": 7, "count": 160, "createTime": "2023-02-21T14:19:27" }, { "id": 8, "count": 108, "createTime": "2023-02-22T14:19:38" } ] }
前端代碼
vue+elementUI
可以直接復(fù)制使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- vue在線引入 --> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script> <!-- 引入樣式 --> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <!-- 引入組件庫(kù) --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <!-- 在線引入echarts --> <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.1/echarts.min.js"></script> <!-- axios --> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <el-row> <el-date-picker v-model="value1" type="datetimerange" start-placeholder="開(kāi)始日期" end-placeholder="結(jié)束日期" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00']"> </el-date-picker> <el-button type="primary" @click="searchButton">查詢</el-button> </el-row> <el-row> <div id="main" style="width: 500px; height: 400px;"></div> </el-row> </div> <script> var app = new Vue({ el: '#app', data: { myChart: null, value1: [ '2023-02-15 14:17:50', '2023-02-22 14:19:38' ], }, methods: { // 初始化echarts initEcharts() { var chartDom = document.getElementById('main'); // 將創(chuàng)建的echarts示例放到vue的data中,這樣在這個(gè)界面就想到于全局配置了 this.myChart = echarts.init(chartDom); }, // 配置echarts的option,展示數(shù)據(jù)圖表 async setEchartsOption() { // 這里可以發(fā)送axios請(qǐng)求,然后通過(guò)響應(yīng)的數(shù)據(jù)賦值給對(duì)應(yīng)的x軸和y軸即可,由于這里沒(méi)有于后端聯(lián)調(diào),所以簡(jiǎn)單請(qǐng)求判斷一下, const result = await axios.get(`http://localhost:8080/echarts/getechartslistbytime?startTime=${this.value1[0]}&endTime=${this.value1[1]}`) console.log(result.data.data) var xValue = null; var yValue = null; if (result.data.data) { xValue = result.data.data.map(item => { return item.createTime.split('T')[0] }) yValue = result.data.data.map(item => { return item.count }) } var option; option = { tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', data: xValue, axisTick: { alignWithLabel: true } } ], yAxis: [ { type: 'value' } ], series: [ { name: '數(shù)量', type: 'bar', barWidth: '60%', data: yValue } ] }; option && this.myChart.setOption(option); }, searchButton() { // 為了避免重復(fù)初始化echarts, 我們?cè)邳c(diǎn)擊按鈕的時(shí)候可以調(diào)用echarts的clear方法清楚一下,然后再重新調(diào)用函數(shù)進(jìn)行對(duì)echarts的賦值 this.myChart.clear() // 這一句有沒(méi)有都可以 this.setEchartsOption() } }, mounted() { // 注意調(diào)用順序,先初始化echarts才給echarts賦值 this.initEcharts() this.setEchartsOption() } }) </script> </body> </html>
其實(shí)這里也可以用折線圖,過(guò)程都差不多的
后端代碼
springboot+mysql+mybatis-plus,剛好這里有springboot結(jié)合mybatis-plus的用法
后端代碼各個(gè)文件的位置截圖如下
mysql數(shù)據(jù)庫(kù)建表語(yǔ)句和插入數(shù)據(jù)語(yǔ)句
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for echarts -- ---------------------------- DROP TABLE IF EXISTS `echarts`; CREATE TABLE `echarts` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '主鍵id', `count` int NULL DEFAULT NULL COMMENT '統(tǒng)計(jì)數(shù)量', `create_time` datetime NULL DEFAULT NULL COMMENT '創(chuàng)建時(shí)間', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of echarts -- ---------------------------- INSERT INTO `echarts` VALUES (1, 24, '2023-02-15 14:17:50'); INSERT INTO `echarts` VALUES (2, 32, '2023-02-16 14:18:19'); INSERT INTO `echarts` VALUES (3, 40, '2023-02-17 14:18:30'); INSERT INTO `echarts` VALUES (4, 16, '2023-02-18 14:18:44'); INSERT INTO `echarts` VALUES (5, 88, '2023-02-19 14:18:59'); INSERT INTO `echarts` VALUES (6, 8, '2023-02-20 14:19:11'); INSERT INTO `echarts` VALUES (7, 160, '2023-02-21 14:19:27'); INSERT INTO `echarts` VALUES (8, 108, '2023-02-22 14:19:38'); SET FOREIGN_KEY_CHECKS = 1;
application.yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/echarts-demo?characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: root mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
啟動(dòng)器類
@SpringBootApplication @MapperScan("com.example.dao") public class EchartsDemoApplication { public static void main(String[] args) { SpringApplication.run(EchartsDemoApplication.class, args); } }
Controller
@RestController @RequestMapping("echarts") @CrossOrigin // 允許跨域 public class EchartsController{ /** * 服務(wù)對(duì)象 */ @Autowired private EchartsService echartsService; @GetMapping("/getechartslistbytime") public R getEchartsListByTime(String startTime, String endTime) { return echartsService.getEchartsListByTime(startTime,endTime); } }
dao或者mapper
public interface EchartsDao extends BaseMapper<Echarts> { }
實(shí)體類,這里包括統(tǒng)一響應(yīng)格式的R對(duì)象
Echarts類
public class Echarts { //主鍵id private Integer id; //統(tǒng)計(jì)數(shù)量 private Integer count; //創(chuàng)建時(shí)間 private LocalDateTime createTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public LocalDateTime getCreateTime() { return createTime; } public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; } }
R對(duì)象
package com.example.domain; public class R { private Integer code; private String message; private Object data; public R(Integer code, String message) { this.code = code; this.message = message; } public R(Integer code, String message, Object data) { this.code = code; this.message = message; this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
Service接口
public interface EchartsService extends IService<Echarts> { R getEchartsListByTime(String startTime, String endTime); }
ServiceImpl實(shí)現(xiàn)類
@Service("echartsService") public class EchartsServiceImpl extends ServiceImpl<EchartsDao, Echarts> implements EchartsService { @Override public R getEchartsListByTime(String startTime, String endTime) { LambdaQueryWrapper<Echarts> queryWrapper = new LambdaQueryWrapper<>(); // ge 是>= queryWrapper.ge((startTime != null && startTime.length() > 0), Echarts::getCreateTime, startTime); queryWrapper.le((endTime != null && endTime.length() > 0), Echarts::getCreateTime, endTime); // 這里就相當(dāng)于SELECT id,count,create_time FROM echarts WHERE (create_time >= ? AND create_time <= ?) List<Echarts> list = this.list(queryWrapper); return new R(200, "查詢成功!", list); } }
總結(jié)
到此這篇關(guān)于vue中使用echarts并根據(jù)選擇條件動(dòng)態(tài)展示echarts圖表的文章就介紹到這了,更多相關(guān)vue使用echarts圖表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue2.0 element-ui中el-upload的before-upload方法返回false時(shí)submit(
這篇文章主要介紹了vue2.0 element-ui中el-upload的before-upload方法返回false時(shí)submit()不生效的解決方法,這里需要主要項(xiàng)目中用的element-ui是V1.4.3,感興趣的朋友參考下吧2018-08-08Vue高級(jí)用法實(shí)例教程之動(dòng)態(tài)組件
讓多個(gè)組件使用同一個(gè)掛載點(diǎn),并動(dòng)態(tài)切換,這就是動(dòng)態(tài)組件,下面這篇文章主要給大家介紹了關(guān)于Vue高級(jí)用法實(shí)例教程之動(dòng)態(tài)組件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序
本文主要介紹了vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05使用vue制作探探滑動(dòng)堆疊組件的實(shí)例代碼
探探的堆疊滑動(dòng)組件起到了關(guān)鍵的作用,下面就來(lái)看看如何用vue寫(xiě)一個(gè)探探的堆疊組件,感興趣的朋友一起看看吧2018-03-03vue3全局組件自動(dòng)注冊(cè)功能實(shí)現(xiàn)
本文主要講述vue3的全局公共組件的自動(dòng)注冊(cè)功能,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-02-02