在ng2-charts中未显示多个饼图

问题描述 投票:0回答:1

这是我的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 resopnse的方式,我怀疑的是如何绑定聚会 - >座位到数据集或数据并绑定聚会 - > 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."
}
angular pie-chart ng2-charts
1个回答
0
投票

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

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

如果您需要多个图表,则您将拥有多个阵列(标签和数据)。

你可以检查一下working demo here

© www.soinside.com 2019 - 2024. All rights reserved.