基于Ionic3實現(xiàn)選項卡切換并重新加載echarts
要求
選項卡每切換一次,就加載對應(yīng)的一個echarts 圖,要有一開始的動畫效果
效果如下:

注意點
1、echarts要想每次都能重新加載,需要移除"_echarts_instance_"屬性,否則一切換選項卡,就再也加載不出來了
2、在ts 中獲取html 頁面元素,不能寫在構(gòu)造方法里,應(yīng)該寫在ionViewDidEnter()方法里
3、選項卡<ion-segment> 不要搭配[ngSwitch] 的方式,因為這種情況下,只能獲取當前選項卡中的元素,未選中的選項卡里的元素獲取不到,在第2步驟ionViewDidEnter() 方法里自然也是獲取不到。應(yīng)當搭配(ionChange)使用方式
4、不能直接將chart作為全局變量,這樣的話第二次加載就沒有動畫效果了
獻上代碼
html文件
<ion-header>
<ion-navbar>
<ion-title>區(qū)域</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-segment [(ngModel)]="choice" (ionChange)="segmentChanged($event)">
<ion-segment-button value="choice1">
省份增速排名
</ion-segment-button>
<ion-segment-button value="choice2">
市州增速排名
</ion-segment-button>
<ion-segment-button value="choice3">
全省走勢
</ion-segment-button>
<ion-segment-button value="choice4">
市州占比
</ion-segment-button>
</ion-segment>
<div id="chartContainer" style="width: 100%; height: 300px;"></div>
</ion-content>
ts文件
import {Component} from '@angular/core';
import * as echarts from 'echarts';
@Component({
selector: 'page-data-area',
templateUrl: 'area.html'
})
export class DataAreaPage {
choice: string = "choice1";
ec: any = echarts;
chartContainer: any;
constructor() {
}
clickChart1() {
const chart1 = this.ec.init(this.chartContainer);
chart1.setOption({
series: {
type: 'pie',
data: [{
name: 'A',
value: 10
}, {
name: 'B',
value: 20
}, {
name: 'C',
value: 30
}, {
name: 'D',
value: 40
}]
}
}, true);
this.chartContainer.removeAttribute("_echarts_instance_");
}
clickChart2() {
const chart2 = this.ec.init(this.chartContainer);
chart2.setOption({
series: {
type: 'pie',
data: [{
name: 'A',
value: 10
}, {
name: 'B',
value: 20
}, {
name: 'C',
value: 30
}]
}
}, true);
this.chartContainer.removeAttribute("_echarts_instance_");
}
clickChart3() {
const chart3 = this.ec.init(this.chartContainer);
chart3.setOption({
series: {
type: 'pie',
data: [{
name: 'A',
value: 10
}, {
name: 'B',
value: 20
}, {
name: 'C',
value: 30
}, {
name: 'D',
value: 40
}, {
name: 'E',
value: 50
}]
}
}, true);
this.chartContainer.removeAttribute("_echarts_instance_");
}
clickChart4() {
const chart4 = this.ec.init(this.chartContainer);
chart4.setOption({
series: {
type: 'pie',
data: [{
name: 'A',
value: 10
}, {
name: 'B',
value: 20
}, {
name: 'C',
value: 30
}, {
name: 'D',
value: 40
}, {
name: 'E',
value: 50
}, {
name: 'F',
value: 60
}]
}
}, true);
this.chartContainer.removeAttribute("_echarts_instance_");
}
segmentChanged(e) {
if (e.value == "choice1") {
this.clickChart1();
} else if (e.value == "choice2") {
this.clickChart2();
} else if (e.value == "choice3") {
this.clickChart3();
} else if (e.value == "choice4") {
this.clickChart4();
}
}
ionViewDidEnter() {
this.chartContainer = document.getElementById('chartContainer');
this.clickChart1();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實現(xiàn)動態(tài)網(wǎng)頁飄落的雪花
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)動態(tài)網(wǎng)頁飄落的雪花,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
js 數(shù)據(jù)類型轉(zhuǎn)換總結(jié)筆記
js 數(shù)據(jù)類型轉(zhuǎn)換總結(jié)筆記,需要的朋友可以參考下。2011-01-01
JavaScript實現(xiàn)ArrayBuffer到Base64的轉(zhuǎn)換
本文探討了在 JavaScript 中將 ArrayBuffer 轉(zhuǎn)換為 Base64 字符串時遇到的棧溢出問題,并提供了幾種實用的解決方案,我們將通過生動的比喻來解釋相關(guān)概念,比較不同方法的性能和兼容性,最終提供一個平衡而實用的方法,需要的朋友可以參考下2024-10-10
JavaScript-html標題滾動效果的簡單實現(xiàn)
下面小編就為大家?guī)硪黄狫avaScript-html標題滾動效果的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
微信小程序?qū)崙?zhàn)篇之購物車的實現(xiàn)代碼示例
本篇文章主要介紹了微信小程序?qū)崙?zhàn)篇之購物車的實現(xiàn)代碼示例,詳細的介紹了購物車的功能實現(xiàn),具有一定的參考價值,有興趣的可以了解一下2017-11-11
OkHttp踩坑隨筆為何 response.body().string() 只能調(diào)用一次
想必大家都用過或接觸過 OkHttp,我最近在使用 Okhttp 時,就踩到一個坑,在這兒分享出來,以后大家遇到類似問題時就可以繞過去2018-01-01

