Angular Google 圖表 - 配置語法
在本章中,我們將展示在 Angular 中使用 Google 圖表 API 繪製圖表所需的配置。
步驟 1 - 建立 Angular 應用程式
按照以下步驟更新我們在Angular 6 - 專案設定章節中建立的 Angular 應用程式 −
| 步驟 | 描述 |
|---|---|
| 1 | 按照Angular 6 - 專案設定章節中說明,建立一個名為googleChartsApp的專案。 |
| 2 | 修改app.module.ts、app.component.ts和app.component.html,如下所述。保持其他檔案不變。 |
| 3 | 編譯並執行應用程式以驗證已實現邏輯的結果。 |
以下是修改後的模組描述符app.module.ts的內容。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { GoogleChartsModule } from 'angular-google-charts';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,GoogleChartsModule
],
providers: [], bootstrap: [AppComponent]
})
export class AppModule { }
以下是修改後的 HTML 主機檔案app.component.html的內容。
<google-chart #chart [title]="title" [type]="type" [data]="data" [columnNames]="columnNames" [options]="options" [width]="width" [height]="height"> </google-chart>
理解配置後,我們將在文末看到更新後的app.component.ts。
步驟 2 - 使用配置
設定標題
title = 'Browser market shares at a specific website, 2014';
設定圖表型別
type='PieChart';
資料
配置要在圖表上顯示的資料。
data = [ ['Firefox', 45.0], ['IE', 26.8], ['Chrome', 12.8], ['Safari', 8.5], ['Opera', 6.2], ['Others', 0.7] ];
列名稱
配置要顯示的列名稱。
columnNames = ['Browser', 'Percentage'];
選項
配置其他選項。
options = {
colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'], is3D: true
};
示例
考慮以下示例以進一步瞭解配置語法 −
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Browser market shares at a specific website, 2014';
type = 'PieChart';
data = [
['Firefox', 45.0],
['IE', 26.8],
['Chrome', 12.8],
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
];
columnNames = ['Browser', 'Percentage'];
options = {
};
width = 550;
height = 400;
}
結果
驗證結果。
廣告