我现在将角核从2.00 RC4转换为4.1,所以,我想知道ng2-nvD3图表控制是否会在角4上工作?
发布于 2017-05-23 12:42:46
它确实奏效了,尽管有点不正统。
如何让它在我的4项目中运行:
安装如您在一个角2项目:npm install ng2-nvd3
注意:为了避免错误的这,我必须包括一个基于ReferenceError: nv is not defined的解决方案。
在index.html内部包括:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.4/nv.d3.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.4/nv.d3.min.js"></script>几乎可以肯定有一个更好的解决办法,但为了回答“它是否有效”的问题,这就是我所做的。
在您的主模块文件中(我的是基于用app/app.module.ts创建的基本项目的角CLI),包括导入和声明指令:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {nvD3} from 'ng2-nvd3';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent,
nvD3,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }然后在您的组件(我的是app/app.component.ts)中,指定选项和数据函数(我使用了这示例):
import {Component} from '@angular/core';
declare const d3: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
options = {
chart: {
type: 'lineChart',
height: 450,
margin: {
top: 20,
right: 20,
bottom: 40,
left: 55
},
x: function (d) {
return d.x;
},
y: function (d) {
return d.y;
},
useInteractiveGuideline: true,
xAxis: {
axisLabel: 'Time (ms)'
},
yAxis: {
axisLabel: 'Voltage (v)',
tickFormat: function (d) {
return d3.format('.02f')(d);
},
axisLabelDistance: -10
}
}
};
data: any = this.sinAndCos();
sinAndCos() {
const sin = [], sin2 = [], cos = [];
// Data is represented as an array of {x,y} pairs.
for (let i = 0; i < 100; i++) {
sin.push({x: i, y: Math.sin(i / 10)});
sin2.push({x: i, y: i % 10 === 5 ? null : Math.sin(i / 10) * 0.25 + 0.5});
cos.push({x: i, y: .5 * Math.cos(i / 10 + 2) + Math.random() / 10});
}
// Line chart data should be sent as an array of series objects.
return [
{
values: sin, // values - represents the array of {x,y} data points
key: 'Sine Wave', // key - the name of the series.
color: '#ff7f0e' // color - optional: choose your own line color.
},
{
values: cos,
key: 'Cosine Wave',
color: '#2ca02c'
},
{
values: sin2,
key: 'Another sine wave',
color: '#7777ff',
area: true // area - set to true if you want this line to turn into a filled area chart.
}
];
}
}最后在html中使用它(我的是app/app.component.html):
<div>
<nvd3 [options]="options" [data]="data"></nvd3>
</div>祝你好运。请随时更新现有的任何更好的解决方案:)
https://stackoverflow.com/questions/43890924
复制相似问题