使用echarts點(diǎn)擊按鈕從新渲染圖表并更新數(shù)據(jù)
echarts點(diǎn)擊按鈕從新渲染圖表并更新數(shù)據(jù)
效果圖


像這樣的,點(diǎn)擊一個(gè)會(huì)顯示不同的數(shù)據(jù)的試圖。
思路
很簡(jiǎn)單,就是點(diǎn)擊按鈕后從新調(diào)用一下echarts試圖的方法,然后把新的數(shù)據(jù)當(dāng)參數(shù)傳給echarts方法內(nèi),然后給到data就能渲染了。
上代碼
export default {
data() {
return {
//默認(rèn)給一個(gè)數(shù)據(jù),一進(jìn)來(lái)就能看到的。
barDatas:[730, 801, 924, 222, 1333, 411, 566, 888, 466, 877]
};
},
mounted() {
//一進(jìn)頁(yè)面就加載試圖,并把默認(rèn)的數(shù)據(jù)傳給他渲染出來(lái),這個(gè)默認(rèn)的不是寫(xiě)死的,實(shí)際工作可以一進(jìn)來(lái)直接發(fā)請(qǐng)求那數(shù)據(jù)給試圖
this.barGraph(this.barDatas);
},
methods: {
//橫向條形圖
barGraph(val) {
//初始化圖標(biāo)
var myCharts = this.$echarts.init(this.$refs["echart-right"]);
//Y軸的數(shù)據(jù),和數(shù)據(jù)值位置一一對(duì)應(yīng)
var cate = [
"0001",
"0002",
"0003",
"0004",
"0005",
"0006",
"0007",
"0008",
"0009",
"0010",
];
//數(shù)據(jù)值,順序和Y軸的名字一一對(duì)應(yīng)
var barData = val //這個(gè)地方參數(shù)傳給他渲染數(shù)據(jù)
var option = {
title: {
text: this.rightname + "合格率排行榜top10",
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
//圖表位置
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
//X軸
xAxis: {
type: "value",
axisLine: {
show: false,
},
axisTick: {
show: false,
},
//不顯示X軸刻度線和數(shù)字
splitLine: { show: false },
axisLabel: { show: false },
},
yAxis: {
type: "category",
data: cate,
//升序
inverse: true,
splitLine: { show: false },
axisLine: {
show: false,
},
axisTick: {
show: false,
},
//key和圖間距
offset: 10,
//動(dòng)畫(huà)部分
animationDuration: 300,
animationDurationUpdate: 300,
//key文字大小
nameTextStyle: {
fontSize: 5,
},
},
series: [
{
//柱狀圖自動(dòng)排序,排序自動(dòng)讓Y軸名字跟著數(shù)據(jù)動(dòng)
realtimeSort: true,
name: "數(shù)量",
type: "bar",
data: barData,
barWidth: 14,
barGap: 10,
smooth: true,
valueAnimation: true,
//Y軸數(shù)字顯示部分
label: {
normal: {
show: true,
position: "right",
valueAnimation: true,
offset: [5, -2],
textStyle: {
color: "#333",
fontSize: 13,
},
},
},
itemStyle: {
emphasis: {
barBorderRadius: 7,
},
//顏色樣式部分
normal: {
barBorderRadius: 7,
color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
{ offset: 0, color: "#3977E6" },
{ offset: 1, color: "#37BBF8" },
]),
},
},
},
],
//動(dòng)畫(huà)部分
animationDuration: 0,
animationDurationUpdate: 3000,
animationEasing: "linear",
animationEasingUpdate: "linear",
};
myCharts.setOption(option);
//圖表大小變動(dòng)從新渲染,動(dòng)態(tài)自適應(yīng)
window.addEventListener("resize", function () {
myCharts.resize();
});
},
//點(diǎn)擊高亮
// 點(diǎn)擊后渲染不同echarts試圖
acts(index) {
this.actlist = index;
if (index == 4) {
this.isshow = true;
} else {
this.isshow = false;
//我是循環(huán)寫(xiě)的按鈕,所以通過(guò)判斷點(diǎn)擊的是哪一個(gè)按鈕,來(lái)對(duì)應(yīng)賦值新的數(shù)據(jù)然后調(diào)用方法傳參從新渲染試圖,單獨(dú)寫(xiě)的按鈕直接在上面加點(diǎn)擊事件就行。
//當(dāng)然這個(gè)數(shù)據(jù)不是死的,后面給成點(diǎn)擊按鈕發(fā)請(qǐng)求接口那數(shù)據(jù)賦值。
if(index==0){
this.barDatas=[530, 301, 524, 622, 223, 344, 333, 422, 566, 677]
this.barGraph(this.barDatas)
console.log("ri");
}else if(index==1){
this.barDatas=[730, 801, 624, 222, 223, 344, 333, 322, 466, 877]
this.barGraph(this.barDatas)
console.log("zhou");
}else if(index==2){
this.barDatas=[430, 501, 524, 722, 123, 644, 433, 322, 666, 827]
this.barGraph(this.barDatas)
console.log("yue");
}else{
this.barDatas=[330, 401, 524, 622, 723, 844, 533, 322, 636, 527]
this.barGraph(this.barDatas)
console.log("nian");
}
}
}
},
};
echarts3點(diǎn)擊按鈕動(dòng)態(tài)更新數(shù)據(jù)
1.后臺(tái)代碼(模擬數(shù)據(jù))
@RequestMapping("/queryMiddleAppinfo")
@ResponseBody
public Map queryMiddleAppinfo(){
List<Integer> list1 = new ArrayList<Integer>();
list1.add((int)Math.floor(Math.random()*20+1));
list1.add((int)Math.floor(Math.random()*20+1));
list1.add((int)Math.floor(Math.random()*20+1));
list1.add((int)Math.floor(Math.random()*20+1));
list1.add((int)Math.floor(Math.random()*20+1));
list1.add((int)Math.floor(Math.random()*20+1));
list1.add((int)Math.floor(Math.random()*20+1));
List<Integer> list2 = new ArrayList<Integer>();
list2.add((int)Math.floor(Math.random()*20+1));
list2.add((int)Math.floor(Math.random()*20+1));
list2.add((int)Math.floor(Math.random()*20+1));
list2.add((int)Math.floor(Math.random()*20+1));
list2.add((int)Math.floor(Math.random()*20+1));
list2.add((int)Math.floor(Math.random()*20+1));
list2.add((int)Math.floor(Math.random()*20+1));
Map map = new HashMap();
map.put("man", list1);
map.put("women", list2);
return map;
}2.前臺(tái)界面
按鈕
<button class="layui-btn" data-type="reload">搜索</button>
存放圖標(biāo)的div
<div id="main-line" style="height: 450px;"></div>
3.echarts代碼
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main-line'));
// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
myChart.setOption({
tooltip: {
trigger: 'axis'
},
legend: {
data: ['男', '女']
},
toolbox: {
show: false,
feature: {
dataView: {show: true, readOnly: false},
magicType: {show: true, type: ['line', 'bar']},
restore: {show: true},
saveAsImage: {show: true}
}
},
calculable: true,
xAxis: [
{
type: 'category',
data: ['1930', '1940', '1950', '1960', '1970', '1980','1990']
}
],
yAxis: [
{
type: 'value'
}
],
series: [
{
name: '男',
type: 'bar',
data: [],
markPoint: {
data: [
{type: 'max', name: '最大值'},
{type: 'min', name: '最小值'}
]
}
},
{
name: '女',
type: 'bar',
data: [],
markPoint: {
data: [
{type: 'max', name: '最大值'},
{type: 'min', name: '最小值'}
]
}
}
]
});4.點(diǎn)擊搜索按鈕觸發(fā)的函數(shù)
function loadsexnums(){
var nums_man=[]; //存放男性數(shù)量
var nums_women=[]; //存放女性數(shù)量
myChart.showLoading(); //數(shù)據(jù)加載完之前先顯示一段簡(jiǎn)單的loading動(dòng)畫(huà)
$.ajax({
type : "post",
async : true, //異步請(qǐng)求(同步請(qǐng)求將會(huì)鎖住瀏覽器,用戶其他操作必須等待請(qǐng)求完成才可以執(zhí)行)
url : "/queryMiddleAppinfo", //請(qǐng)求發(fā)送到TestServlet處
data : {},
dataType : "json", //返回?cái)?shù)據(jù)形式為json
success : function(result) {
//請(qǐng)求成功時(shí)執(zhí)行該函數(shù)內(nèi)容,result即為服務(wù)器返回的json對(duì)象
if (result) {
var man = result.man;
var women = result.women;
for(var i=0;i<man.length;i++){
nums_man.push(man[i]); //挨個(gè)取出類別并填入類別數(shù)組
}
for(var i=0;i<women.length;i++){
nums_women.push(women[i]); //挨個(gè)取出銷量并填入銷量數(shù)組
}
myChart.hideLoading(); //隱藏加載動(dòng)畫(huà)
myChart.setOption({ //加載數(shù)據(jù)圖表
series: [
{
data: nums_man //此處只對(duì)data數(shù)據(jù)修改即可
},
{
data: nums_women
}
]
});
}
},
error : function(errorMsg) {
alert("圖表請(qǐng)求數(shù)據(jù)失敗!");
myChart.hideLoading();
}
})
}5.效果

每次點(diǎn)擊查詢圖標(biāo)展示都會(huì)變化
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-mugen-scroll組件實(shí)現(xiàn)pc端滾動(dòng)刷新
這篇文章主要為大家詳細(xì)介紹了vue-mugen-scroll組件實(shí)現(xiàn)pc端滾動(dòng)刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
vue自定義表格列的實(shí)現(xiàn)過(guò)程記錄
這篇文章主要給大家介紹了關(guān)于vue自定義表格列的相關(guān)資料,表格組件在開(kāi)發(fā)中經(jīng)常會(huì)用到,文章通過(guò)示例代碼介紹的也很詳細(xì),需要的朋友可以參考下2021-06-06
Vue $emit $refs子父組件間方法的調(diào)用實(shí)例
今天小編就為大家分享一篇Vue $emit $refs子父組件間方法的調(diào)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue.js整合vux中的上拉加載下拉刷新實(shí)例教程
這篇文章主要給大家介紹了關(guān)于vue.js整合vux中上拉加載下拉刷新的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
vue結(jié)合v-for和input實(shí)現(xiàn)多選列表checkbox功能
在Vue中,可通過(guò)v-for指令和v-model實(shí)現(xiàn)多選列表功能,首先,使用v-for指令遍歷數(shù)組生成列表項(xiàng),每個(gè)列表項(xiàng)包含一個(gè)復(fù)選框,復(fù)選框的v-model綁定到一個(gè)數(shù)組變量,用于存儲(chǔ)選中的值,感興趣的朋友跟隨小編一起看看吧2024-09-09
defineProperty和Proxy基礎(chǔ)功能及性能對(duì)比
這篇文章主要為大家介紹了defineProperty和Proxy基礎(chǔ)功能及性能對(duì)比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue中v-if和v-for一起使用的弊端及解決辦法(同時(shí)使用 v-if 和 v-for不
當(dāng) v-if 和 v-for 同時(shí)存在于一個(gè)元素上的時(shí)候,v-if 會(huì)首先被執(zhí)行,這篇文章主要介紹了vue中v-if和v-for一起使用的弊端及解決辦法,需要的朋友可以參考下2023-07-07

