国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

在 Angular 中使用Chart.js 和 ng2-charts的示例代碼

2019-11-19 15:46:13
字體:
來源:轉載
供稿:網友

Chart.js是一個流行的JavaScript圖表庫,ng2圖表是Angular 2+的包裝器,可以輕松地將Chart.js集成到Angular中。 我們來看看基本用法。

安裝

首先,在項目中安裝 Chart.js 和 ng2-charts:

# Yarn:$ yarn add ng2-charts chart.js# or npm $ npm install ng2-charts charts.js --save

當然 ,如果你是使用Angular CLI構建的項目,你也可以很容易的添加Chart.js 添加.angular-cli.json配置文件中,以便將它與應用綁定在一直:

//: .angular-cli.json (partial)"script": [ "../node_modules/chart.js/dist/Chart.min.js"]

現在,你需要在你的 app 模塊或功能模塊導入 ng2-charts 的ChartsModule:

//: app.module.tsimport { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import { ChartsModule } from '@angular/charts';import { AppComponent } from './app.component';@NgModule({ declarations: [ AppComponent ], imports: [  BrowserModule,  ChartsModule ], providers: [], bootstrap: [ AppComponent ]})export class AppModule {}

使用

ng2-charts 給我們提供了一個可以應用于HTML canvas元素的baseChart指令。 以下是一個示例,其中顯示了一些用于輸入的選項以及該指令輸出的chartClick事件:

//: app.component.html<div style="width: 40%;"> <canvas   baseChart   [chartType]="'line'"   [datasets]="chartData"   [labels]="chartLabels"   [options]="chartOptions"   [legend]="true"   (chartClick)="onChartClick($event)"> </canvas></div>

這就是組件類現在的樣子:

//: app.component.tsimport { Component } from '@angular/core';@Component({ ... })export class AppComponent { chartOptions = {  responsive: true }; chartData = [  { data: [330, 600, 260, 700], label: 'Account A' },  { data: [120, 455, 100, 340], label: 'Account B' },  { data: [45, 67, 800, 500], label: 'Account C' } ]; chartLabels = ['January', 'February', 'Mars', 'April']; onChartClick(event) {  console.log(event); }}

選項

以下就是不同的可選輸入項:

chartType

設置圖表的基本類型, 值可以是pipe,doughnut,bar,line,polarArea,radar或horizontalBar。

legend

一個布爾值,用于是否在圖表上方顯示圖例。

datasets

包含數據數組和每個數據集標簽的對象數組。

data

如果你的圖表很簡單,只有一個數據集,你可以使用data而不是datasets。

labels

x軸的標簽集合

options

包含圖表選項的對象。 有關可用選項的詳細信息,請參閱官方Chart.js文檔。

在上面的例子中,我們將圖表設置為自適應模式,根據視口大小進行自動調整。

colors

在上面的例子中未顯示,但你可以定義自己的顏色, 傳入包含以下值的對象文字數組:

myColors = [ {  backgroundColor: 'rgba(103, 58, 183, .1)',  borderColor: 'rgb(103, 58, 183)',  pointBackgroundColor: 'rgb(103, 58, 183)',  pointBorderColor: '#fff',  pointHoverBackgroundColor: '#fff',  pointHoverBorderColor: 'rgba(103, 58, 183, .8)' }, // ...colors for additional data sets];

使用自定義顏色時,必須為每個數據集提供一個顏色對象字面量。

事件

發出兩個事件,chartClick和chartHover,它們允許對與圖表交互的用戶做出反應。 當前活動點和標簽作為發射事件數據的一部分返回。

動態更新數據集

當然,Chart.js的優點在于,您的圖表可以輕松地通過動態更新/響應從后端或用戶輸入的數據。

下面這個示例中,我們為5月份添加了一個新的數據集合:

//: app.component.ts(partial)newDataPoint(dataArr = [100, 100, 100], label) { this.chartData.forEach((dataset, index) => {  this.chartData[index] = Object.assign({}, this.chartData[index], {   data: [...this.chartData[index].data, dataArr[index]]  }); }); this.chartLabels = [...this.chartLabels, label];}

它可以像這樣使用:

//: app.component.html(partial)<button (click)="newDataPoint([900, 50, 300], 'May')"> Add data point</button>

你可能注意到了,我們不會對圖表的數據集進行改動,而是使用新數據返回包含先前數據的新對象。 Object.assign可以很容易地做到這一點。

在這個特定的例子中,如果沒有提供參數時,我們為3個數據集設定了默認值為100。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湘乡市| 赤壁市| 浦城县| 株洲市| 东莞市| 保山市| 原阳县| 乌审旗| 开阳县| 太仆寺旗| 武定县| 郯城县| 内江市| 尖扎县| 萨迦县| 宝清县| 松阳县| 丰台区| 保康县| 定南县| 疏附县| 赤水市| 循化| 太康县| 德保县| 昌江| 马鞍山市| 泽普县| 射洪县| 长葛市| 伊金霍洛旗| 乌鲁木齐县| 蚌埠市| 陈巴尔虎旗| 临猗县| 阿合奇县| 淮阳县| 邵武市| 临汾市| 广德县| 广安市|