在 Angular 中使用Chart.js 和 ng2-charts的示例代碼
Chart.js是一個(gè)流行的JavaScript圖表庫,ng2圖表是Angular 2+的包裝器,可以輕松地將Chart.js集成到Angular中。 我們來看看基本用法。
安裝
首先,在項(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 style="width: 40%;"> <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)在于,您的圖表可以輕松地通過動(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è)特定的例子中,如果沒有提供參數(shù)時(shí),我們?yōu)?個(gè)數(shù)據(jù)集設(shè)定了默認(rèn)值為100。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Chart.js圖表庫制作漂亮的響應(yīng)式表單
- Chart.js 輕量級(jí)HTML5圖表繪制工具庫(知識(shí)整理)
- 詳解Chart.js輕量級(jí)圖表庫的使用經(jīng)驗(yàn)
- 使用Vue.js 和Chart.js制作絢麗多彩的圖表
- ichart.js繪制虛線、平均分虛線效果的實(shí)現(xiàn)代碼
- Chart.js在Laravel項(xiàng)目中的應(yīng)用示例
- vue集成chart.js的實(shí)現(xiàn)方法
- 利用ECharts.js畫K線圖的方法示例
- JavaScript Chart 插件整理
- 詳解vue文件中使用echarts.js的兩種方式
- vue.js+Echarts開發(fā)圖表放大縮小功能實(shí)例
- Chart.js功能與使用方法小結(jié)
相關(guān)文章
Angular實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多選復(fù)選框的彈出框指令實(shí)例
下面小編就為大家?guī)硪黄狝ngular實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多選復(fù)選框的彈出框指令實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Angular4學(xué)習(xí)筆記之準(zhǔn)備和環(huán)境搭建項(xiàng)目
Angular4實(shí)現(xiàn)鼠標(biāo)懸停3d傾斜效果

