亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年08月17日 09:04:26   作者:mntx  
本篇文章主要介紹了在 Angular 中使用Chart.js 和 ng2-charts的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下

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

安裝

首先,在項(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)在于,您的圖表可以輕松地通過(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論