Angular 如何使用角度为2或4的D3图表

Angular 如何使用角度为2或4的D3图表,angular,d3.js,charts,Angular,D3.js,Charts,我想在我的一个项目中使用D3图表,请帮助 我试着遵循安装过程。但是它不能正常工作,Peans为我提供了另一个解决方案,以便在现有项目中实现它 npm install d3-ng2-service --save import { BrowserModule } from '@angular/platform-browser'; import { NgModule, ApplicationRef } from '@angular/core'; import { CommonModule } fro

我想在我的一个项目中使用D3图表,请帮助

我试着遵循安装过程。但是它不能正常工作,Peans为我提供了另一个解决方案,以便在现有项目中实现它

npm install d3-ng2-service --save

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

import { D3Service } from 'd3-ng2-service'; // <-- import statement

@NgModule({
  declarations: [
    AppComponent,
    TestD3Component // <-- declaration of the D3 Test component used below
  ],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule
  ],
  providers: [D3Service], // <-- provider registration
  entryComponents: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {

}
npm安装d3-ng2-service——保存
从“@angular/platform browser”导入{BrowserModule};
从'@angular/core'导入{NgModule,ApplicationRef};
从“@angular/common”导入{CommonModule};
从'@angular/forms'导入{FormsModule};
从“./app.component”导入{AppComponent};

从“d3-ng2-service”导入{D3Service};// D3图表有两种实现方式

1.ng2-nvd3图表

2.ngx图表

所以我要实现ng2-nvd3图表

还可以从

首先需要安装它

npm install ng2-nvd3 --save
然后在NgModule中导入它,还需要导入d3和nvd3,正如我在下面导入的一样

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import 'd3';
import 'nvd3'
import {NvD3Module} from "ng2-nvd3";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NvD3Module,

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
在此之后,您必须在.angular-cli.json文件中添加样式

"styles": [
        "styles.css",
        "../node_modules/nvd3/build/nv.d3.css"
      ],
接下来,您必须转到我使用的示例中的component.ts文件 app.component.ts,您必须向图表提供数据和选项对象 指示

import {Component} from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  options: any;
  data: any;


  constructor() {
    this.options = {
      chart: {
        type: 'pieChart',
        height: 500,
        x: function (d) {
          return d.key;
        },
        y: function (d) {
          return d.y;
        },
        showLabels: true,
        duration: 500,
        labelThreshold: 0.01,
        labelSunbeamLayout: true,
        legend: {
          margin: {
            top: 5,
            right: 35,
            bottom: 5,
            left: 0
          }
        }
      }
    };

    this.data = [
      {
        key: "P60-1",
        y: 256
      },
      {
        key: "P60-2",
        y: 445
      },
      {
        key: "P40",
        y: 225
      },
      {
        key: "P73",
        y: 127
      },
      {
        key: "P71",
        y: 128
      }
    ];
  }
}
在Html中完成后,需要在我的示例中提供图表指令,即app.component.Html

<div>
  <nvd3 [options]="options" [data]="data"></nvd3>
</div>

ngx-nvd3的端到端解决方案。角度6

要求:

1.Install "d3": "^3.5.17"
2.Install "ngx-nvd3": "^1.0.9"
app.module.ts(appModule)

admin.component.html(html)


那么,你的错误是什么?这似乎可以让你在控制台中得到任何错误?如果是这样,请张贴他们。吉特回购似乎是一个空白项目的角度,没有任何相关的图表。你能更新一下密码吗。
import { NvD3Module } from 'ngx-nvd3'
imports: [NvD3Module]
<div>
    <nvd3 [options]="options" [data]="datas"> 
    </nvd3>
</div>
import * as d3 from "d3";

datas:any

styleUrls: ['./adminpanel.component.css','../../../node_modules/nvd3/build/nv.d3.css']

this.options = {
  chart: {
    type: 'discreteBarChart',
    height: 450,
    margin: {
      top: 20,
      right: 20,
      bottom: 50,
      left: 55
    },
    x: function (d) {
      return d.label;
    },
    y: function (d) {
      return d.value + (1e-10);
    },
    showValues: true,
    valueFormat: function (d) {
      return d3.format(',.4f')(d);
    },
    duration: 500,
    xAxis: {
      axisLabel: 'X Axis'
    },
    yAxis: {
      axisLabel: 'Y Axis',
      axisLabelDistance: -10
    }
  }
};

  this.datas = [
    {
      key: "Cumulative Return",
      values: [
        {
          "label" : "A" ,
          "value" : -29.765957771107
        } ,
        {
          "label" : "B" ,
          "value" : 0
        } ,
        {
          "label" : "C" ,
          "value" : 32.807804682612
        } ,
        {
          "label" : "d" ,
          "value" : 60.807804682612
        } ,
        {
          "label" : "e" ,
          "value" : 70.807804682612
        } ,
        {
          "label" : "f" ,
          "value" : 80.807804682612
        } ,
      ]
    }
  ];