欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS2 與 D3.js集成實(shí)現(xiàn)自定義可視化的方法

 更新時(shí)間:2017年12月01日 14:31:47   作者:dudubird85  
本篇文章主要介紹了ANGULAR2 與 D3.js集成實(shí)現(xiàn)自定義可視化的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

目標(biāo)

  1. 展現(xiàn)層與邏輯層分離
  2. 數(shù)據(jù)與可視化組件相分離
  3. 數(shù)據(jù)與視圖雙向綁定,實(shí)時(shí)更新
  4. 代碼結(jié)構(gòu)清晰,易于維護(hù)與修改

基本原理

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

源碼解析

代碼結(jié)構(gòu)很簡單,其中除主頁index.html和main.ts之外的代碼結(jié)構(gòu)如下所示:

代碼結(jié)構(gòu)

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

實(shí)現(xiàn)宿主視圖定義,

2個(gè)按鈕,按鈕可以綁定了2點(diǎn)點(diǎn)擊事件,執(zhí)行相應(yīng)的動作,刷新數(shù)組,同時(shí)完成汽泡圖的更新;

1個(gè)汽泡圖子組件,其中values為子組件的輸入屬性,實(shí)現(xiàn)父子組件之間的通信,numArray為汽泡圖的輸入數(shù)據(jù)數(shù)組,后續(xù)為隨機(jī)生成的數(shù)組

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

app.component.ts

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

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 = [];
 // 清除定時(shí)器
 private clearTimer() {
  console.log('stop refreshing');
  clearInterval(this.intervalId);
 }
 // 生成指定范圍內(nèi)的隨機(jī)數(shù)
 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",
  };
 }
 // 元素關(guān)閉清除定時(shí)器
 ngOnDestroy() { this.clearTimer(); }
 // 啟動定時(shí)刷新數(shù)組
 refreshArr() {
  this.clearTimer()
  this.intervalId = window.setInterval(() => {
   this.numArray = [];
   for (let i=0;i<8;i++)
   {
    this.numArray.push(this.getRandom(0, 100000000));
   }
  }, 3000);
 }
 // 停止定時(shí)刷新數(shù)組
 stopRefresh() {
  this.clearTimer();
 }
}

bubbles.component.ts 汽泡圖組件類

  1. ngOnChanges() 生命周期方法,可以在輸入屬性values發(fā)生變化時(shí),自動被調(diào)用;
  2. @ViewChild 可以獲取對子元素svg的引用,其中#target自定義變量用于標(biāo)識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() {
  }
  // 每當(dāng)元素對象上綁定的數(shù)據(jù) 輸入屬性值 values 發(fā)生變化時(shí),執(zhí)行下列函數(shù),實(shí)現(xiàn)圖表動態(tài)變化
  ngOnChanges(changes) {
    if (this.chart) {
      // 先清空汽泡圖,再重新調(diào)用汽泡圖對象的render方法,根據(jù)變動后的值繪制圖形
      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 語法定義的汽泡圖類,自帶一個(gè)繪制方法和擦除方法
  2. 需要在index.html當(dāng)中先引入 <script src="http://d3js.org/d3.v2.js"></script>
declare var d3;
// define a bubble chart class 
// Exports the visualization module
export class BubblesChart {
  target: HTMLElement;
  //構(gòu)造函數(shù), 基于一個(gè) HTML元素對象內(nèi)部來繪制
  constructor(target: HTMLElement) {
    this.target = target;
  }
  // 渲染 入?yún)閿?shù)值 完成基于一個(gè)數(shù)組的 汽泡圖的繪制
  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();
  }
}

效果展示

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談angularjs module返回對象的坑(推薦)

    淺談angularjs module返回對象的坑(推薦)

    下面小編就為大家?guī)硪黄獪\談angularjs module返回對象的坑(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • angularjs $http實(shí)現(xiàn)form表單提交示例

    angularjs $http實(shí)現(xiàn)form表單提交示例

    這篇文章主要介紹了angularjs $http實(shí)現(xiàn)form表單提交示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-06-06
  • AngularJS 過濾與排序詳解及實(shí)例代碼

    AngularJS 過濾與排序詳解及實(shí)例代碼

    這篇文章主要介紹了AngularJS 過濾與排序,這里整理了詳細(xì)的資料及簡單實(shí)例代碼,有需要的小伙伴可以參考下
    2016-09-09
  • Angular模板表單校驗(yàn)方法詳解

    Angular模板表單校驗(yàn)方法詳解

    這篇文章主要為大家詳細(xì)介紹了Angular模板表單校驗(yàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 分享Angular http interceptors 攔截器使用(推薦)

    分享Angular http interceptors 攔截器使用(推薦)

    AngularJS 是一個(gè) JavaScript 框架。它可通過 <script> 標(biāo)簽添加到 HTML 頁面。這篇文章主要介紹了分享Angular http interceptors 攔截器使用(推薦),需要的朋友可以參考下
    2019-11-11
  • BootStrap+Angularjs+NgDialog實(shí)現(xiàn)模式對話框

    BootStrap+Angularjs+NgDialog實(shí)現(xiàn)模式對話框

    在完成一個(gè)后臺管理系統(tǒng)時(shí),需要用表格顯示注冊用戶的信息。但是用戶地址太長了,不好顯示。所以想做一個(gè)模式對話框,點(diǎn)擊詳細(xì)地址按鈕時(shí),彈出對話框,顯示地址。下面小編給大家分享下實(shí)現(xiàn)方法,一起看下吧
    2016-08-08
  • AngularJS路由切換實(shí)現(xiàn)方法分析

    AngularJS路由切換實(shí)現(xiàn)方法分析

    這篇文章主要介紹了AngularJS路由切換實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了AngularJS路由切換的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-03-03
  • angular報(bào)錯(cuò)can't resolve all parameters for []的解決

    angular報(bào)錯(cuò)can't resolve all parameters&nb

    這篇文章主要介紹了angular報(bào)錯(cuò)can't resolve all parameters for []的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • AngularJS動態(tài)菜單操作指令

    AngularJS動態(tài)菜單操作指令

    在我們創(chuàng)建一個(gè)angularJS應(yīng)用的時(shí)候,菜單往往往是不可或缺的元素之一。接下來通過本文給大家介紹AngularJS動態(tài)菜單操作指令,感興趣的朋友一起學(xué)習(xí)吧
    2017-04-04
  • AngularJS基礎(chǔ) ng-readonly 指令簡單示例

    AngularJS基礎(chǔ) ng-readonly 指令簡單示例

    本文主要介紹AngularJS ng-readonly 指令,這里對ng-readonly指令的資料做了整理,有學(xué)習(xí)AngularJS 指令的同學(xué)可以參考下
    2016-08-08

最新評論