0
你好我试图使用从http://www.chartjs.org图表之一,但我不能使它的工作..我安装npm安装chart.js - 保存为文档要求和使用此代码来创建图表。使用Angular2无法使用Chart.js
.TS
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(){
var lineChartData: [12, 19, 3, 5, 2, 3];
var lineChartLabels: string[] = ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"];
var lineChartOptions: any = {
responsive: true,
animation: false,
beginAtZero:true
};
var lineChartColors: Array<any> = [
{ // grey
backgroundColor: 'rgba(33,150,243,0.2)',
borderColor: 'rgba(33,150,243,1)',
pointBackgroundColor: 'rgba(33,150,243,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(33,150,243,0.8)'
},
{ // dark grey
backgroundColor: 'rgba(76,175,80,0.2)',
borderColor: 'rgba(76,175,80,1)',
pointBackgroundColor: 'rgba(76,175,80,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(76,175,80,1)'
},
{ // grey
backgroundColor: 'rgba(244,67,54,0.2)',
borderColor: 'rgba(244,67,54,1)',
pointBackgroundColor: 'rgba(244,67,54,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(244,67,54,0.8)'
},
{ // grey
backgroundColor: 'rgba(103,58,183,0.2)',
borderColor: 'rgba(103,58,183,1)',
pointBackgroundColor: 'rgba(103,58,183,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(103,58,183,0.8)'
},
{ // grey
backgroundColor: 'rgba(255,152,0,0.2)',
borderColor: 'rgba(255,152,0,1)',
pointBackgroundColor: 'rgba(255,152,0,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(255,152,0,0.8)'
},
{ // grey
backgroundColor: 'rgba(96,125,139,0.2)',
borderColor: 'rgba(96,125,139,1)',
pointBackgroundColor: 'rgba(96,125,139,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(96,125,139,0.8)'
}
];
var lineChartLegend: boolean = true;
var lineChartType: string = 'line';
}
}
.HTML
<script src="node_modules/chart.js/src/chart.js"></script>
<canvas baseChart height="200"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
></canvas>
.MODULE
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ChartsModule } from 'ng2-charts/ng2-charts';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ChartsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
更新代码,页面是空白的,没有呃罗斯在控制台中,但此消息:
角正在运行在开发模式。调用enableProdMode()以启用生产模式。
嗨,我从'ng2-charts/ng2-charts'中添加了import {ChartsModule};到应用程序的模块,但在组件,这使得一个错误: this.myChart =新图(this.ctx,{ 它说找不到名称表 –
你还在导入“图表”的成分。 – Notmfb
no im not ...我会为你上传新的代码,检查 –