# Yarn: $ yarn add ng2-charts chart.js # or npm $ npm install ng2-charts charts.js --save
//: .angular-cli.json (partial) "script": [ "../node_modules/chart.js/dist/Chart.min.js" ]
//: 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 {}
//: 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.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);
}
}
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
];
//: 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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有