在ng2图表中未显示多个饼图

时间:2019-03-13 07:33:25

标签: angular pie-chart ng2-charts

这是我的html代码,我正在做的就是调用API并将数据绑定到图表,并且它用于多个图表...

<ng-container *ngFor="let historyitem of historydata?.data; let i=index;"> <div class="post history_post wow fadeInUp" id="{{historyitem.id}}" *ngIf="historyitem.party?.length > 0"> <canvas baseChart width="400" height="200" [data]="chartdata" [labels]="chartlabel" [colors]="ChartColors" [legend]="true" [chartType]="ChartType"></canvas> </div> </ng-container>

这是我如何获取api响应,而我的疑问是如何将聚会->席位绑定到数据集或数据,并将聚会-> partyname绑定到标签?我仍然对如何做到这一点感到困惑??,如果我调用map方法,但是map方法对多个数组对象无效,该如何解决?

{
    "data": [
        {
            "id": 15,
            "year": 2018,
            "government": "INC,JDS",
            "election_type": "GENERAL_ELECTION",
            "state_government_url": "karnataka-inc-jds-2018",
            "start_date": "2018-05-23",
            "end_date": "2023-05-23",
            "description": "The 15th Karnataka Legislative Assembly was constituted after the Karnataka Legislative Assembly elections in 2018. Polling was held on 12 May[1] for 222 constituencies out of the 224-member assembly,[2] with counting of votes and results declared on 15 May.[3] The term of the assembly is for five years.",
            "likes_count": 0,
            "comments_count": 0,
            "share_count": 0,
            "favourites_count": 0,
            "liked": false,
            "emoji": "NA",
            "added_to_favourite": false,
            "party": []
        },
        {
            "id": 16,
            "year": 2018,
            "government": "BJP",
            "election_type": "GENERAL_ELECTION",
            "state_government_url": "karnataka-bjp-2018",
            "start_date": "2018-05-17",
            "end_date": "2018-05-19",
            "description": "The 15th Karnataka Legislative Assembly was constituted after the Karnataka Legislative Assembly elections in 2018. Polling was held on 12 May[1] for 222 constituencies out of the 224-member assembly,[2] with counting of votes and results declared on 15 May.[3] The term of the assembly is for five years.",
            "likes_count": 0,
            "comments_count": 0,
            "share_count": 0,
            "favourites_count": 1,
            "liked": false,
            "emoji": "NA",
            "added_to_favourite": true,
            "party": []
        },
        {
            "id": 14,
            "year": 2013,
            "government": "INC",
            "election_type": "GENERAL_ELECTION",
            "state_government_url": "karnataka-inc-2013",
            "start_date": "2013-05-13",
            "end_date": "2018-05-17",
            "description": "INC",
            "likes_count": 3,
            "comments_count": 0,
            "share_count": 0,
            "favourites_count": 1,
            "liked": true,
            "emoji": "0x1F62F",
            "added_to_favourite": false,
            "party": [
                {
                    "party": {
                        "party_name": "INC",
                        "party_abbervation": "Indian National Congress"
                    },
                    "seats": 122
                },
                {
                    "party": {
                        "party_name": "BJP",
                        "party_abbervation": "Bharatiya Janata Party"
                    },
                    "seats": 40
                },
                {
                    "party": {
                        "party_name": "JD(S)",
                        "party_abbervation": "Janata Dal (Secular)"
                    },
                    "seats": 40
                },
                {
                    "party": {
                        "party_name": "IND",
                        "party_abbervation": "Independent"
                    },
                    "seats": 9
                },
                {
                    "party": {
                        "party_name": "KJP",
                        "party_abbervation": "Karnataka Janatha Paksha"
                    },
                    "seats": 6
                },
                {
                    "party": {
                        "party_name": "BSRCP ",
                        "party_abbervation": "Badavara Shramikara Raitara Congress Party"
                    },
                    "seats": 4
                }
            ]
        },
        {
            "id": 13,
            "year": 2008,
            "government": "BJP",
            "election_type": "GENERAL_ELECTION",
            "state_government_url": "karnataka-bjp-2008",
            "start_date": "2008-05-30",
            "end_date": "2013-05-05",
            "description": "BJP",
            "likes_count": 2,
            "comments_count": 0,
            "share_count": 0,
            "favourites_count": 1,
            "liked": false,
            "emoji": "NA",
            "added_to_favourite": true,
            "party": [
                {
                    "party": {
                        "party_name": "BJP",
                        "party_abbervation": "Bharatiya Janata Party"
                    },
                    "seats": 117
                },
                {
                    "party": {
                        "party_name": "INC",
                        "party_abbervation": "Indian National Congress"
                    },
                    "seats": 82
                },
                {
                    "party": {
                        "party_name": "JD(S)",
                        "party_abbervation": "Janata Dal (Secular)"
                    },
                    "seats": 31
                },
                {
                    "party": {
                        "party_name": "IND",
                        "party_abbervation": "Independent"
                    },
                    "seats": 7
                }
            ]
        }
    ],
    "code": 201,
    "message": "State government information retrieved successfully."
}

1 个答案:

答案 0 :(得分:0)

对于饼图,您需要将其绑定到一组数组(一个带有标签,第二个带有数据)。

由于您的数据包含多个选举数据,因此您可以向用户询问他们感兴趣的选举...,然后遍历您的数据以填充一组数组(标签和数据)。

如果您需要多个图表,则将具有一组以上的数组(标签和数据)。

您可以检查working demo here