簡體   English   中英

JSON數據中的餅圖

[英]Pie chart from JSON Data

我有一個生成以下json數據的控制器:

[

    {
        "title": "Option 0",
        "votes": 0
    },
    {
        "title": "Option 1",
        "votes": 1
    },
    {
        "title": "Option 2",
        "votes": 2
    },
    {
        "title": "Option 3",
        "votes": 3
    },
    {
        "title": "Option 4",
        "votes": 4
    },
    {
        "title": "Option 5",
        "votes": 5
    },
    {
        "title": "Option 6",
        "votes": 6
    },
    {
        "title": "Option 7",
        "votes": 7
    },
    {
        "title": "Option 8",
        "votes": 8
    },
    {
        "title": "Option 9",
        "votes": 9
    }

]

我想使用jqplot從此數據制作一個餅圖。 我可以按照教程制作圖表,但無法顯示帶有切片的文本標簽。 這是我的js代碼:

jQuery(document).ready(function () {
    urlDataJSON = '/Poll/PollData?pollId=3';

    $.getJSON(urlDataJSON, "", function (data) {
        var dataSlices = [];
        var dataLabels = "";

        $.each(data, function (entryindex, entry) {
            dataSlices.push(entry['votes']);
            dataLabels = dataLabels + entry['title'];
        });
        options = {
            legend: { show: true },
            title: 'Poll Results',
            seriesDefaults: {
                // Make this a pie chart.
                renderer: jQuery.jqplot.PieRenderer,
                rendererOptions: {
                    // Put data labels on the pie slices.
                    // By default, labels show the percentage of the slice.
                    showDataLabels: true
                }
            }
        }
        var plot = $.jqplot('chartdiv', [dataSlices], options);
    });
});              

請告訴我如何顯示與餅圖切片關聯的文本。 我知道我缺少什么,但我還是JavaScript的新手。

您的數據結構不正確,此鏈接將您帶到一個有效的示例。

jqPlot示例

我認為這會起作用

dataSlices.push([entry['title'], entry['votes']]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM