Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Flot pie charts show data value instead of percentage

Tags:

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....

like image 968
Ruud van de Beeten Avatar asked Apr 15 '11 07:04

Ruud van de Beeten


People also ask

How do you show a value in a pie chart?

To display percentage values as labels on a pie chart On the design surface, right-click on the pie and select Show Data Labels. The data labels should appear within each slice on the pie chart. On the design surface, right-click on the labels and select Series Label Properties.

Do pie charts have to be in percentages?

Is a pie chart always in percentage? Yes, though sometimes it's in a roundabout way. While most pie charts list percentages, it's also possible to create a pie chart using non-percentage data, so long as the data constitutes all parts of the whole.

How do I show numbers instead of percentages in Excel pie chart?

Right click your Pie Chart. Choose "Chart Options". Under "Data Labels" Tab, choose "Show Value" instead of "Show Percent".


1 Answers

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.

like image 193
Ruud van de Beeten Avatar answered Sep 20 '22 07:09

Ruud van de Beeten