gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:21:28 26 4
gpt4 key购买 nike

我有一个最初有 2 页的选项卡项目。我试图让条形图显示在其中一页上,但什么也没有出现。所以,我创建了第三个页面来简单地进行图表测试。我直接从 ng-2charts 中提取示例代码文档,然后将其放入 ionic 组件中。但是,即使在新标签页中也没有任何显示。

这是我的代码:

在 app.ts/html 中显示我正确包含了页面:

//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;

我在实际页面中的代码可以在我上面的链接中找到,但为了速记,我会把它放在这里:

在 chart-test.ts/html 中显示我刚刚复制并粘贴,同时添加了必要的 ionic 标签:

<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);
}
}

图表选项卡的结果如下所示:

enter image description here

最佳答案

确保您还完成了以下操作:

  • 安装 Chart.js

npm install chart.js --save

  • 在您的 app.ts 文件中,确保导入 chart.js

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

  • 在显示选项卡的页面的scss文件中,插入以下内容:

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

那么您的图表应该可以工作了。

关于angular - Ionic2 Angular2 : I can't seem to get an ng2-chart to appear in my app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com