2
votes

I have a controller that generates the following json data:

[

    {
        "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
    }

]

I want to make a pie chart from this data using jqplot. I am able to make a chart by following a tutorial but i am not able to show the text labels with the slices. Here is my js code:

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);
    });
});              

please tell me how do i show the text associated with pie chart slices. I know i am missing something but again i am a newbie to javascript.

1

1 Answers

4
votes

Your data structure is incorrect this link takes you to a working example.

jqPlot example

I think this will work

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