gpt4 book ai didi

javascript - ionic显示来自json http请求的chartjs数据

转载 作者:行者123 更新时间:2023-12-01 01:31:02 24 4
gpt4 key购买 nike

我正在构建一个 ionic 应用程序,通过从 URL/HTTP 请求检索数据来显示仪表板(饼图)。这是我在 .ts 文件中调用来自 url 的数据的代码,我设法使用 e.g. 以表格形式显示它。 .html 文件中的 {{ items.data }}:

public items : Array<any> = [];

ionViewWillEnter() : void{
this.load();
}

load() : void{
this.http.get('http://localhost/test/retrieve-data.php')
.subscribe((data : any) =>{
console.dir(data);
this.items = data;
},
(error : any) =>{
console.dir(error);
});
}

我的问题是我想从一行检索数据并将其显示在饼图中。例如,这是我想要获取的一行:

[{"id":9,"twitter_name":"Max Payne","positive_tweets":24,"negative_tweets":14,"total_tweets":38,"pos_percent":63,"neg_percent":37}]

我想显示一个饼图,其中显示 pos_percentneg_percent 的值。

这是我一直在做的事情,但仍然坚持调用行数据:

@ViewChild('pieChart') pieChart;

public pieChartEl : any;

createPieChart()
{

this.pieChartEl = new Chart(this.pieChart.nativeElement,
{
type: 'pie',
data: {
labels: ["Positive","Negative"],
datasets: [{
data : [],
duration : 2000,
easing : 'easeInQuart',
backgroundColor : "rgba(54, 116, 152, 0.5)",
hoverBackgroundColor : "rgba(160, 116, 152, 0.5)"
}]
},
options : {
maintainAspectRatio: false,
layout: {
padding: {
left : 50,
right : 0,
top : 0,
bottom : 0
}
},
animation: {
duration : 5000
}
}
});



this.chartLoadingEl = this.pieChartEl.generateLegend();
}

ionViewDidLoad()
{
this.createPieChart();
}

如何获取该数据?

最佳答案

要通过 HTTP 请求绘制饼图,请按照以下说明操作:

1- 确保安装 Angular 2 图表和 Charts.js,只需键入以下命令:

npm install ng2-charts --save
npm install chart.js --save

2-注册ChartsModule:

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';

import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { ChartsModule } from 'ng2-charts';
import { Http, HttpModule } from '@angular/http';

@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
imports: [
HttpModule,
BrowserModule,
IonicModule.forRoot(MyApp),
ChartsModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}

3- 将 Canvas 标签添加到您的 html 文件中,如下所示:

<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<h2>Welcome to Ionic!</h2>

<div style="display: block">
<canvas baseChart #baseChart="base-chart"
[data]="doughnutChartData"
[labels]="doughnutChartLabels"
[chartType]="doughnutChartType"></canvas>
</div>

</ion-content>

4:获取json数据并绘制到canvas:

注意:我通过 HTTP 从 asset/datas.json 加载 json。

import { Component, ViewChild } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http, Response} from '@angular/http';
import { BaseChartDirective } from 'ng2-charts';

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {

@ViewChild("baseChart") pieChartEl: BaseChartDirective;

public doughnutChartLabels: string[] = [];
public doughnutChartData: number[] = [];
public doughnutChartType: string = 'doughnut';

constructor(public navCtrl: NavController, public http: Http) {

}

ionViewDidLoad() {
this.updateData();
}

public updateData() {
this.http.get("/assets/datas.json").subscribe((res: Response) => {
let jsons = res.json();
let data = jsons[0];
this.doughnutChartLabels.push("pos_percent");
this.doughnutChartLabels.push("neg_percent");

this.doughnutChartData.push(data.pos_percent);
this.doughnutChartData.push(data.neg_percent);

console.log(this.doughnutChartLabels);
console.log(this.doughnutChartData);

if(this.pieChartEl !== undefined){
this.pieChartEl.ngOnDestroy();
this.pieChartEl.chart = this.pieChartEl.getChartBuilder(this.pieChartEl.ctx);
}
});
}
}

5:这是我的结果:

enter image description here

您可以在 ionic3-chart 中查看我的代码我希望这能有所帮助:)。

关于javascript - ionic显示来自json http请求的chartjs数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53272805/

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