首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示图.角度2

显示图.角度2
EN

Stack Overflow用户
提问于 2017-03-06 11:02:29
回答 2查看 473关注 0票数 1

我试图在我的角度2应用程序中显示图表,即线状图。但是我对图表的显示方式还是很陌生的。我是跟随来自勇敢软件的文档,但我没有得到一个好的结果。我的控制台出现了一个错误。我在我的app.module.ts.中导入了ChartsModule我该怎么做才能正确地处理这条线图?

dashboard.html

代码语言:javascript
复制
<div class="row">
  <div class="col-md-6">
    <div style="display: block;">
    <canvas baseChart width="400" height="400"
                [datasets]="lineChartData"
                [labels]="lineChartLabels"
                [options]="lineChartOptions"
                [colors]="lineChartColors"
                [legend]="lineChartLegend"
                [chartType]="lineChartType"
                (chartHover)="chartHovered($event)"
                (chartClick)="chartClicked($event)"></canvas>
    </div>
  </div>
  <div class="col-md-6" style="margin-bottom: 10px">
    <table class="table table-responsive table-condensed">
      <tr>
        <th *ngFor="let label of lineChartLabels">{{label}}</th>
      </tr>
      <tr *ngFor="let d of lineChartData">
        <td *ngFor="let label of lineChartLabels; let j=index">{{d && d.data[j]}}</td>
      </tr>
    </table>
    <button (click)="randomize()">CLICK</button>
  </div>
</div>

dashboardComponent

代码语言:javascript
复制
@Component({
  selector: 'line-chart-demo',
  templateUrl: './dashboard.html'
})
export class LineChartDemoComponent {
  // lineChart
  public lineChartData:Array<any> = [
    {data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A'},
    {data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'},
    {data: [18, 48, 77, 9, 100, 27, 40], label: 'Series C'}
  ];
  public lineChartLabels:Array<any> = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
  public lineChartOptions:any = {
    responsive: true
  };
  public lineChartColors:Array<any> = [
    { // grey
      backgroundColor: 'rgba(148,159,177,0.2)',
      borderColor: 'rgba(148,159,177,1)',
      pointBackgroundColor: 'rgba(148,159,177,1)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(148,159,177,0.8)'
    },
    { // dark grey
      backgroundColor: 'rgba(77,83,96,0.2)',
      borderColor: 'rgba(77,83,96,1)',
      pointBackgroundColor: 'rgba(77,83,96,1)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(77,83,96,1)'
    },
    { // grey
      backgroundColor: 'rgba(148,159,177,0.2)',
      borderColor: 'rgba(148,159,177,1)',
      pointBackgroundColor: 'rgba(148,159,177,1)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(148,159,177,0.8)'
    }
  ];
  public lineChartLegend:boolean = true;
  public lineChartType:string = 'line';

  public randomize():void {
    let _lineChartData:Array<any> = new Array(this.lineChartData.length);
    for (let i = 0; i < this.lineChartData.length; i++) {
      _lineChartData[i] = {data: new Array(this.lineChartData[i].data.length), label: this.lineChartData[i].label};
      for (let j = 0; j < this.lineChartData[i].data.length; j++) {
        _lineChartData[i].data[j] = Math.floor((Math.random() * 100) + 1);
      }
    }
    this.lineChartData = _lineChartData;
  }

  // events
  public chartClicked(e:any):void {
    console.log(e);
  }

  public chartHovered(e:any):void {
    console.log(e);
  }
}

angular-cli.json

代码语言:javascript
复制
"scripts": [
        "../node_modules/chart.js/src/chart.js"

       ],

误差

代码语言:javascript
复制
Uncaught ReferenceError: require is not defined
    at eval (eval at webpackJsonp.1166.module.exports (addScript.js:9), <anonymous>:4:38)
    at eval (<anonymous>)
    at webpackJsonp.1166.module.exports (addScript.js:9)
    at Object.519 (chart.js?7d5f:1)
    at __webpack_require__ (bootstrap 48f1b30…:52)
    at Object.1215 (addScript.js:10)
    at __webpack_require__ (bootstrap 48f1b30…:52)
    at webpackJsonpCallback (bootstrap 48f1b30…:23)
    at scripts.bundle.js:1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-06 12:39:13

  1. 在您的<script src="node_modules/chart.js/src/chart.js"></script>中包括dashboard.html。
  2. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.min.js"></script>添加到index.html文件中。
票数 1
EN

Stack Overflow用户

发布于 2017-11-09 05:53:48

首先,您必须安装chart.js

代码语言:javascript
复制
npm install ng2-charts chart.js --save

once it's done need to import it in your app.module.ts file
import { ChartsModule } from 'ng2-charts';
@NgModule( declarations: [],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ChartsModule,

])

这些图表依赖于C3,所以必须包括它,也包括

代码语言:javascript
复制
 npm install c3

您错过的最后一件事,您还必须添加样式under angular.cli.json

代码语言:javascript
复制
"styles": [
    "../node_modules/c3/c3.min.css"
  ],
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42623909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档