24

I can't figure out how to get flot.pie to change the data shown in the labels from a percentage of the "raw data" to the actual data. In my example i've created a pie chart with the numbers of read/unread messages.

Number of read messages: 50. Number of unread messages: 150.

The created pie shows the percentage of read messages as 25%. On this spot i want to show the actual 50 messages. See image below:

enter image description here

The code i used to create the pie:

var data = [
    { label: "Read", data: 50, color: '#614E43' },
    { label: "Unread", data: 150, color: '#F5912D' }
];

And:

    $(function () {
        $.plot($("#placeholder"), data,
           {
            series: {
                pie: {
                    show: true,
                    radius: 1,
                    label: {
                        show: true,
                        radius: 2 / 3,
                        formatter: function (label, series) {
                            return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';

                        },
                        threshold: 0.1
                    }
                }
            },
            legend: {
                show: false
            }
        });
    });

Is this possible?

With the answer of @Ryley I came to a dirty solution. When I output the series.data the values "1,150" and "1,50" were returned. I came up with the idea to substract the first 2 characters of the returned value and display the substracted value.

String(str).substring(2, str.lenght)

This is the pie chart I created with this solution:

enter image description here

This is not the best solution, but it works for me. if someone knows a better solution....

casperOne
  • 73,706
  • 19
  • 184
  • 253
Ruud van de Beeten
  • 1,185
  • 2
  • 9
  • 17

4 Answers4

44

I found the answer to the question. The data object is a multi-dimensional array. To get the acual data use the following code:

    $(function () {
    $.plot($("#placeholder"), data,
       {
        series: {
            pie: {
                show: true,
                radius: 1,
                label: {
                    show: true,
                    radius: 2 / 3,
                    formatter: function (label, series) {
                        return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + series.data[0][1] + '</div>';

                    },
                    threshold: 0.1
                }
            }
        },
        legend: {
            show: false
        }
    });
});

Notice the code " series.data[0][1] " to extract the data.

Ruud van de Beeten
  • 1,185
  • 2
  • 9
  • 17
2

This somewhat depends on what you mean with "On this spot i want to show the actual 50 messages".
Let's assume that you want to have a div popup when they mouseover the Read or Unread section and then show the messages in there.

First step is to make your pie chart interactive. You need to add the grid option like so:

legend: {
    show: true;
},
grid: {
    hoverable: true,
    clickable: true
}

Next, you have to bind the click/hover events to functions that retrieve your messages and display them:

$("#placeholder").bind('plothover',function(e,pos,obj){

});

$("#placeholder").bind('plotclick',function(e,pos,obj){
    if (obj.series.label == 'Read'){
       //show your read messages
    } else {
       //show your unread messages
    }       
});

That's it!


Now, if what you meant is simply that you want to display all the messages directly in the pie all the time, you just need to change your formatter to reference a global variable that contains your messages.

Ryley
  • 21,046
  • 2
  • 67
  • 81
  • Thanks for your answer. When I call the following code 'alert(obj.series.data);' the for the data with the label 'Read' the output is "1,50". Can you tell me why it's not "50" instead? – Ruud van de Beeten Apr 18 '11 at 06:50
  • @Ruud, flot is doing some manipulation of your data in the background... and it's not very clear what exactly. As you figured out, it's buried in an array instead of being easily available... – Ryley Apr 18 '11 at 15:13
1

You could use:

formatter: function (label, series) {
    return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+Math.round(series.percent)+"%<br/>" + series.data[0][1] +'</div>';
},
Anonymous
  • 11,748
  • 6
  • 35
  • 57
0

Try something like ProtoVis. There are also a few good answers on SO with links to other free useful data visualization libraries. If you have some $ fusion charts is also quite good.

FoneyOp
  • 336
  • 1
  • 6
  • you are misinformed. Flot recently released a new version. As to whether it is hard/unfeatured, that's your opinion. – Ryley Apr 15 '11 at 15:22
  • errrr, my mistake, I was thinking of http://www.filamentgroup.com/lab/update_to_jquery_visualize_accessible_charts_with_html5_from_designing_with/ – FoneyOp Apr 15 '11 at 15:25
  • Flot is still actively developed, has a very active google group/mailing list and recently created a mirror on GitHub. They lose points on their documentation, since its a text file, but anyone who can read can understand it. – Kai Apr 15 '11 at 15:25
  • Thanks but I can't change the library used. – Ruud van de Beeten Apr 18 '11 at 06:53