【问题标题】:How to Pass data from api into pie chart with angular如何将数据从api传递到带有角度的饼图
【发布时间】:2020-07-22 21:04:20
【问题描述】:

我想在带有角度的饼图画布中传递 API 的输出,但我无法获得任何结果,API 已被消耗,但我在将其与饼图(数据点)关联时遇到问题 下面的代码。

app.component.ts

import { Component, OnInit } from '@angular/core';
import { EmailValidator } from '@angular/forms';
import { Router, NavigationEnd } from '@angular/router';
import { ApiService } from './api.service';
import { ApistatService } from './apistat.service';
import * as CanvasJS from './canvasjs.min';
//var CanvasJS = require('./canvasjs.min');
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  elements: any[];
  constructor(
    private apistatService: ApistatService
  ) {  
    this.elements = [];
  }
    ngOnInit() {
    let chart = new CanvasJS.Chart("chartContainer", {
        theme: "light2",
        animationEnabled: true,
        exportEnabled: true,
        title:{
            text: "Monthly Expense"
        },
        data: [{
            type: "pie",
            showInLegend: true,
            toolTipContent: "<b>{elements.total}</b>: ${y} (#status)",
            indexLabel: "{name} - #percent%",
            dataPoints: [
                
                { y: 120, name: "Insurance" },
                { y: 300, name: "Traveling" },
                { y: 800, name: "Housing" },
                { y: 150, name: "Education" },
                { y: 150, name: "Shopping"},
                { y: 250, name: "Others" }
            ]
        }]
  });   
  this.apistatService.getData().subscribe((data:any) => {
    if(data.status === 200) {
      console.log(data.response);
      this.elements = data.response;
    }
  })    
    chart.render();
    }
}

app.component.html

<div id="chartContainer"  style="height: 370px; width: 100%; margin-left:auto;margin-right:auto;">
  

  
</div>

【问题讨论】:

    标签: node.js angular api canvas


    【解决方案1】:

    要在饼图中使用 API 数据,您只需将 API 中的数据设置为 dataPoints 数组,只需确保您的 API 数据具有与您在渲染图表时传入数据数组的 dataPoints 数组中当前可用的格式相同的格式,它将起作用.

    【讨论】:

    • 你说的合乎逻辑,但我没有这样做的想法,我在文档中查看了很多但我没有找到解决方案,我是 Angular 的初学者。
    • 你能分享一些我可以处理你的代码的链接吗,也许这样我可以帮助你?
    • 给我你的不和谐或电子邮件,以便我可以联系你..
    • 您可以通过 priteshkapuriya@gmail.com 与我联系
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 2020-01-26
    • 1970-01-01
    • 2016-02-27
    • 2019-08-14
    • 2018-07-08
    相关资源
    最近更新 更多