• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    AngularJS2 與 D3.js集成實現自定義可視化的方法

    來源:懂視網 責編:小采 時間:2020-11-27 22:24:39
    文檔

    AngularJS2 與 D3.js集成實現自定義可視化的方法

    AngularJS2 與 D3.js集成實現自定義可視化的方法:本文介紹了ANGULAR2 與 D3.js集成實現自定義可視化的方法,分享給大家,具體如下: 目標 展現層與邏輯層分離 數據與可視化組件相分離 數據與視圖雙向綁定,實時更新 代碼結構清晰,易于維護與修改 基本原理 angular2 的組件生命周期鉤子方法\父子組件交互機
    推薦度:
    導讀AngularJS2 與 D3.js集成實現自定義可視化的方法:本文介紹了ANGULAR2 與 D3.js集成實現自定義可視化的方法,分享給大家,具體如下: 目標 展現層與邏輯層分離 數據與可視化組件相分離 數據與視圖雙向綁定,實時更新 代碼結構清晰,易于維護與修改 基本原理 angular2 的組件生命周期鉤子方法\父子組件交互機

    本文介紹了ANGULAR2 與 D3.js集成實現自定義可視化的方法,分享給大家,具體如下:

    目標

    1. 展現層與邏輯層分離
    2. 數據與可視化組件相分離
    3. 數據與視圖雙向綁定,實時更新
    4. 代碼結構清晰,易于維護與修改

    基本原理

    angular2 的組件生命周期鉤子方法\父子組件交互機制\模板語法

    源碼解析

    代碼結構很簡單,其中除主頁index.html和main.ts之外的代碼結構如下所示:

    代碼結構

    app.module.ts

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    //components
    import { AppComponent } from './app.component';
    import { Bubbles } from './bubbles.component';
    
    @NgModule({
     declarations: [
     AppComponent,
     Bubbles
     ],
     imports: [
     BrowserModule,
     FormsModule
     ],
     providers: [],
     bootstrap: [AppComponent]
    })
    export class AppModule { }
    
    

    app.component.html

    實現宿主視圖定義,

    2個按鈕,按鈕可以綁定了2點點擊事件,執行相應的動作,刷新數組,同時完成汽泡圖的更新;

    1個汽泡圖子組件,其中values為子組件的輸入屬性,實現父子組件之間的通信,numArray為汽泡圖的輸入數據數組,后續為隨機生成的數組

    <h1>
     <button (click)="refreshArr()" >開始刷新氣泡圖</button>
     <button (click)="stopRefresh()" >停止刷新氣泡圖</button>
     <bubbles [values]="numArray"></bubbles>
    </h1>

    app.component.ts

    通過指定一個3秒刷新一次的定時器,刷新數據,這里需要注意,需要先清空數組,再添加元素,直接修改數組元素值而不改變引用,則無法刷新汽泡圖

    import { Component, OnDestroy, OnInit } from '@angular/core';
    @Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
     styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit, OnDestroy {
     intervalId = 0;
     numArray = [];
     // 清除定時器
     private clearTimer() {
     console.log('stop refreshing');
     clearInterval(this.intervalId);
     }
     // 生成指定范圍內的隨機數
     private getRandom(begin, end) {
     return Math.floor(Math.random() * (end - begin));
     }
     ngOnInit() {
     for (let i in this.numArray) {
     this.numArray[i] = this.getRandom(0, 100000000); // "0", "1", "2",
     };
     }
     // 元素關閉清除定時器
     ngOnDestroy() { this.clearTimer(); }
     // 啟動定時刷新數組
     refreshArr() {
     this.clearTimer()
     this.intervalId = window.setInterval(() => {
     this.numArray = [];
     for (let i=0;i<8;i++)
     {
     this.numArray.push(this.getRandom(0, 100000000));
     }
     }, 3000);
     }
     // 停止定時刷新數組
     stopRefresh() {
     this.clearTimer();
     }
    }
    

    bubbles.component.ts 汽泡圖組件類

    1. ngOnChanges() 生命周期方法,可以在輸入屬性values發生變化時,自動被調用;
    2. @ViewChild 可以獲取對子元素svg的引用,其中#target自定義變量用于標識svg子元素
    import { Component, Input, OnChanges, AfterViewInit, ViewChild} from '@angular/core';
    import {BubblesChart} from './bubbles.chart';
    declare var d3;
    @Component({
     selector: 'bubbles',
     template: '<svg #target width="900" height="300"></svg>',
    })
    export class Bubbles implements OnChanges, AfterViewInit {
     @Input() values: number[];
     chart: BubblesChart;
     @ViewChild('target') target;//獲得子組件的引用
     constructor() {
     }
     // 每當元素對象上綁定的數據 輸入屬性值 values 發生變化時,執行下列函數,實現圖表動態變化
     ngOnChanges(changes) {
     if (this.chart) {
     // 先清空汽泡圖,再重新調用汽泡圖對象的render方法,根據變動后的值繪制圖形
     this.chart.destroy();
     this.chart.render(changes.values.currentValue);
     }
     }
     
     ngAfterViewInit() {
     // 初始化汽泡圖
     this.chart = new BubblesChart(this.target.nativeElement);
     this.chart.render(this.values);
     }
    }
    

    bubbles.chart.ts 汽泡圖類

    1. d3.js 語法定義的汽泡圖類,自帶一個繪制方法和擦除方法
    2. 需要在index.html當中先引入 <script src="https://www.gxlcms.com//d3js.org/d3.v2.js"></script>
    declare var d3;
    // define a bubble chart class 
    // Exports the visualization module
    export class BubblesChart {
     target: HTMLElement;
     //構造函數, 基于一個 HTML元素對象內部來繪制
     constructor(target: HTMLElement) {
     this.target = target;
     }
     // 渲染 入參為數值 完成基于一個數組的 汽泡圖的繪制
     render(values: number[]) {
     console.log('start rendering');
     console.log(values);
     d3.select(this.target)
     // Get the old circles
     .selectAll('circle')
     .data(values)
     .enter()
     // For each new data point, append a circle to the target SVG
     .append('circle')
     // Apply several style attributes to the circle
     .attr('r', d => Math.log(d)) // 半徑
     .attr('fill', '#5fc') // 顏色
     .attr('stroke', '#333') // 輪廓顏色
     .attr('transform', (d, i) => { // 移動位置
     var offset = i * 30 + 3 * Math.log(d);
     return `translate(${offset}, ${offset})`;
     });
     }
    
     destroy() {
     d3.select(this.target).selectAll('circle').remove();
     }
    }
    
    

    效果展示

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    AngularJS2 與 D3.js集成實現自定義可視化的方法

    AngularJS2 與 D3.js集成實現自定義可視化的方法:本文介紹了ANGULAR2 與 D3.js集成實現自定義可視化的方法,分享給大家,具體如下: 目標 展現層與邏輯層分離 數據與可視化組件相分離 數據與視圖雙向綁定,實時更新 代碼結構清晰,易于維護與修改 基本原理 angular2 的組件生命周期鉤子方法\父子組件交互機
    推薦度:
    標簽: 實現 可視化 d3.js
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 日韩精品无码一区二区中文字幕 | 国产精品美女久久久久| 亚洲一级Av无码毛片久久精品| 国产一区二区精品| 亚洲精品国产精品乱码不卡√ | 人妻无码久久精品| 国产亚洲精品a在线无码| 欧美日韩国产精品系列| 2021国产精品视频网站| 国产精品无码午夜福利| 亚洲精品黄色视频在线观看免费资源| 国产精品高清在线| 国产成人精品视频在放| 漂亮人妻被黑人久久精品| 日韩精品一区二区三区中文字幕| 四虎国产精品永久地址49| 国产suv精品一区二区33| 无码国产乱人伦偷精品视频| 日韩精品一区二区三区中文字幕 | 午夜精品视频在线| 国产精品久久久久久影院| 久久久久久久久无码精品亚洲日韩 | 久久九九精品99国产精品| 欧美日韩精品系列一区二区三区国产一区二区精品 | 欧美日韩精品在线观看| 国产精品无码无卡无需播放器| 精品一区二区三区在线视频| 国产福利精品一区二区| 精品人妻中文av一区二区三区| 午夜精品久久久久久99热| 欧美XXXX黑人又粗又长精品| 国产亚洲精品影视在线产品| 国产精品人人做人人爽| 91精品婷婷国产综合久久| 精品无人区麻豆乱码1区2区| 55夜色66夜色国产精品视频| 国产区精品一区二区不卡中文 | 99热精品久久只有精品| 精品无码国产一区二区三区51安| 亚洲av永久无码精品网站 | 青青草国产精品|