【问题标题】:Jquery Flot pie charts show data value instead of percentageJquery Flot饼图显示数据值而不是百分比
【发布时间】:2011-04-15 07:53:35
【问题描述】:

我不知道如何让 flot.pie 将标签中显示的数据从“原始数据”的百分比更改为实际数据。在我的示例中,我创建了一个饼图,其中包含已读/未读消息的数量。

已读消息数:50。 未读消息数:150。

创建的饼图显示已读消息的百分比为 25%。在这个地方,我想展示实际的 50 条消息。见下图:

我用来创建馅饼的代码:

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

还有:

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

这可能吗?

在@Ryley 的回答下,我找到了一个肮脏的解决方案。当我输出 series.data 时,返回值“1,150”和“1,50”。我想到了减去返回值的前 2 个字符并显示减去的值。

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

这是我使用此解决方案创建的饼图:

这不是最好的解决方案,但它对我有用。如果有人知道更好的解决方案....

【问题讨论】:

  • 我无法让格式化程序工作,调试器甚至不属于那一行...

标签: jquery flot pie-chart


【解决方案1】:

我找到了问题的答案。数据对象是一个多维数组。要获取实际数据,请使用以下代码:

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

注意代码“series.data[0][1]”来提取数据。

【讨论】:

  • 当时对我有用。
  • 在我的图表中只显示一部分
【解决方案2】:

这在某种程度上取决于您对“在这个地方我想显示实际的 50 条消息”的意思。
假设您希望当他们将鼠标悬停在“已读”或“未读”部分上时弹出一个 div,然后在其中显示消息。

第一步是让您的饼图具有交互性。您需要像这样添加grid 选项:

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

接下来,您必须将点击/悬停事件绑定到检索消息并显示它们的函数:

$("#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
    }       
});

就是这样!


现在,如果您的意思只是想一直在饼图中直接显示所有消息,您只需更改格式化程序以引用包含您的消息的全局变量。

【讨论】:

  • 感谢您的回答。当我调用以下代码'alert(obj.series.data);'对于带有“读取”标签的数据,输出为“1,50”。你能告诉我为什么不是“50”吗?
  • @Ruud,flot 正在后台对您的数据进行一些操作……目前还不清楚具体是什么。正如你所知道的,它被隐藏在一个数组中,而不是很容易获得......
【解决方案3】:

你可以使用:

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>';
},

【讨论】:

    【解决方案4】:

    试试ProtoVis。在 SO 上也有一些很好的答案,其中包含指向其他免费有用的数据可视化库的链接。如果有$fusion charts也挺好的。

    【讨论】:

    • 你被误导了。 Flot 最近发布了一个新版本。至于它是否硬/没有特色,那是你的意见。
    • errrr,我错了,我在想filamentgroup.com/lab/…
    • Flot 仍在积极开发中,有一个非常活跃的 google 群组/邮件列表,最近在 GitHub 上创建了一个镜像。他们在文档上丢分,因为它是一个文本文件,但任何能阅读的人都可以理解它。
    • 谢谢,但我无法更改使用的库。
    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多