Jquery Flot pie charts show data value instead of percentage

前端 未结 4 1741
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 05:09

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

相关标签:
4条回答
  • 2021-02-05 05:40

    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.

    0 讨论(0)
  • 2021-02-05 05:44

    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>';
    },
    
    0 讨论(0)
  • 2021-02-05 05:46

    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.

    0 讨论(0)
  • 2021-02-05 05:47

    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.

    0 讨论(0)
提交回复
热议问题