简体   繁体   中英

Ionic2 Angular2: I can't seem to get an ng2-chart to appear in my app

I have a tab project that originally had 2 pages. I was trying to get a bar graph to show up on one of the pages but nothing was appearing. So, I created a third page simply for chart testing. I pulled the sample code directly from ng-2charts documentation, and then put it inside ionic components. However, nothing shows up still even in the new tab.

Here is the code that I have:

In the app.ts/html to show that I am including the pages correctly:

//html file
<ion-tabs greenTheme [selectedIndex]="index">
  <ion-tab tabIcon="people" #content tabTitle="My Roster" [root]="tab2"></ion-tab>
  <ion-tab tabIcon="stats" #content tabTitle="Wage Overview" [root]="tab1"></ion-tab>
  <ion-tab tabIcon='stats' #content tabTitle='Chart test' [root]='tab3'></ion-tab>
</ion-tabs>

//.ts file
import {MetricLandingPage} from './pages/metric-landing/metric-landing';
import {Roster} from './pages/roster/roster';
import {GlobalService} from './providers/global-service/global-service';
import {BarChartDemoComponent} from './pages/chart-test/chart-test';
....

//in the constructor
this.tab2 = Roster;
this.tab1 = MetricLandingPage;
this.tab3 = BarChartDemoComponent;

The code I have in the actual page can be found on the link I put above, but for shorthand, I'll put it here to:

In the chart-test.ts/html to show that I just copied and pasted while adding the necessary ionic tags:

<ion-navbar *navbar greenTheme>
  <button menuToggle>
    <ion-icon name="menu"></ion-icon>
  </button>
  <ion-title>TEST</ion-title>
  <ion-buttons menuToggle='right' end>
    <button>
      <ion-icon name='add'></ion-icon>
    </button>
  </ion-buttons>
</ion-navbar>
<ion-content>
  <base-chart class="chart"
       [datasets]="barChartData"
       [labels]="barChartLabels"
       [options]="barChartOptions"
       [legend]="barChartLegend"
       [chartType]="barChartType"
       (chartHover)="chartHovered($event)"
       (chartClick)="chartClicked($event)"></base-chart>
</ion-content>

import {Component} from '@angular/core';
import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass} from '@angular/common';

import {CHART_DIRECTIVES} from 'ng2-charts/ng2-charts';

// webpack html imports

@Component({
  selector: 'bar-chart-demo',
  templateUrl: 'build/pages/chart-test/chart-test.html',
  directives: [CHART_DIRECTIVES, NgClass, CORE_DIRECTIVES, FORM_DIRECTIVES]
})
export class BarChartDemoComponent {
  public barChartOptions:any = {
    scaleShowVerticalLines: false,
    responsive: true
  };
  public barChartLabels:string[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
  public barChartType:string = 'bar';
  public barChartLegend:boolean = true;

  public barChartData:any[] = [
    {data: [65, 59, 80, 81, 56, 55, 40], label:'Series A'},
    {data: [28, 48, 40, 19, 86, 27, 90], label:'Series B'}
  ];

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

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

Here is what the result looks like for the chart tab:

在此输入图像描述

Make sure you have also done the following:

  • Install Chart.js

npm install chart.js --save

  • In your app.ts file, make sure you import chart.js

import '../node_modules/chart.js/dist/Chart.bundle.min.js';

  • In the scss file of the page that displays the tab, insert the following:

.chart { display: block; height: 100%; }

Then your chart should work.

Looks like you are running into this issue with ng2-charts. Try using display style as stated by couple of folks in the above post. display:block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM