Chart.js是一個(gè)流行的JavaScript圖表庫(kù),ng2圖表是Angular 2+的包裝器,可以輕松地將Chart.js集成到Angular中。 我們來(lái)看看基本用法。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),祿勸企業(yè)網(wǎng)站建設(shè),祿勸品牌網(wǎng)站建設(shè),網(wǎng)站定制,祿勸網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,祿勸網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
安裝
首先,在項(xiàng)目中安裝 Chart.js 和 ng2-charts:
# Yarn: $ yarn add ng2-charts chart.js # or npm $ npm install ng2-charts charts.js --save
當(dāng)然 ,如果你是使用Angular CLI構(gòu)建的項(xiàng)目,你也可以很容易的添加Chart.js 添加.angular-cli.json配置文件中,以便將它與應(yīng)用綁定在一直:
//: .angular-cli.json (partial) "script": [ "../node_modules/chart.js/dist/Chart.min.js" ]
現(xiàn)在,你需要在你的 app 模塊或功能模塊導(dǎo)入 ng2-charts 的ChartsModule:
//: app.module.ts import { 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 給我們提供了一個(gè)可以應(yīng)用于HTML canvas元素的baseChart指令。 以下是一個(gè)示例,其中顯示了一些用于輸入的選項(xiàng)以及該指令輸出的chartClick事件:
//: app.component.html <div > <canvas baseChart [chartType]="'line'" [datasets]="chartData" [labels]="chartLabels" [options]="chartOptions" [legend]="true" (chartClick)="onChartClick($event)"> </canvas> </div>
這就是組件類現(xiàn)在的樣子:
//: app.component.ts import { 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); } }
選項(xiàng)
以下就是不同的可選輸入項(xiàng):
chartType
設(shè)置圖表的基本類型, 值可以是pipe,doughnut,bar,line,polarArea,radar或horizontalBar。
legend
一個(gè)布爾值,用于是否在圖表上方顯示圖例。
datasets
包含數(shù)據(jù)數(shù)組和每個(gè)數(shù)據(jù)集標(biāo)簽的對(duì)象數(shù)組。
data
如果你的圖表很簡(jiǎn)單,只有一個(gè)數(shù)據(jù)集,你可以使用data而不是datasets。
labels
x軸的標(biāo)簽集合
options
包含圖表選項(xiàng)的對(duì)象。 有關(guān)可用選項(xiàng)的詳細(xì)信息,請(qǐng)參閱官方Chart.js文檔。
在上面的例子中,我們將圖表設(shè)置為自適應(yīng)模式,根據(jù)視口大小進(jìn)行自動(dòng)調(diào)整。
colors
在上面的例子中未顯示,但你可以定義自己的顏色, 傳入包含以下值的對(duì)象文字?jǐn)?shù)組:
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 ];
使用自定義顏色時(shí),必須為每個(gè)數(shù)據(jù)集提供一個(gè)顏色對(duì)象字面量。
事件
發(fā)出兩個(gè)事件,chartClick和chartHover,它們?cè)试S對(duì)與圖表交互的用戶做出反應(yīng)。 當(dāng)前活動(dòng)點(diǎn)和標(biāo)簽作為發(fā)射事件數(shù)據(jù)的一部分返回。
動(dòng)態(tài)更新數(shù)據(jù)集
當(dāng)然,Chart.js的優(yōu)點(diǎn)在于,您的圖表可以輕松地通過(guò)動(dòng)態(tài)更新/響應(yīng)從后端或用戶輸入的數(shù)據(jù)。
下面這個(gè)示例中,我們?yōu)?月份添加了一個(gè)新的數(shù)據(jù)集合:
//: 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>
你可能注意到了,我們不會(huì)對(duì)圖表的數(shù)據(jù)集進(jìn)行改動(dòng),而是使用新數(shù)據(jù)返回包含先前數(shù)據(jù)的新對(duì)象。 Object.assign可以很容易地做到這一點(diǎn)。
在這個(gè)特定的例子中,如果沒(méi)有提供參數(shù)時(shí),我們?yōu)?個(gè)數(shù)據(jù)集設(shè)定了默認(rèn)值為100。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文名稱:在Angular中使用Chart.js和ng2-charts的示例代碼
URL標(biāo)題:http://aaarwkj.com/article24/igihje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、建站公司、軟件開(kāi)發(fā)、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)