将数据作为变量传递时,jQuery Flot数据的格式是什么? [英] What is the format of jQuery Flot data when passing in the data as variables

查看:48
本文介绍了将数据作为变量传递时,jQuery Flot数据的格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建jQuery Flot饼图,但是我无法使用"data"参数来工作.我有0..6个值,总计应为100(%),这些值需要单独添加,一次添加一对标签值.

I have been trying to create a jQuery Flot pie chart, but I can't get the "data" parameter to work. I have 0..6 values that should total to 100(%), and these values need to be added individually, one label value pair at a time.

我所看到的所有示例都假定数据值是硬连线的,但是不幸的是,这不是现实世界的工作方式.我需要一些有关如何构造标签的真实示例-Flot使用的数据对.

All of the examples I have seen assume that the data values are hard wired, and unfortunately that is not the way the real world works. I need some real world examples of how to construct the label - data pairs that Flot uses.

推荐答案

当我意识到数据应该以jSON对象符号表示时,我终于使它工作了.旧灯泡可能是暗淡的,但并非完全熄灭.这是代码:

I finally got it working when I realized that the data is supposed to be in jSON object notation. The old light bulb may be dim, but not entirely out. Here's the code:

function updatePieChart() {
    if ('#pieChart') {
        var total = 0;
        var data = new Array();
        var verb = "";
        var dataIndex = 0;
        var count = 0;

        for (var dataIndex = 0; dataIndex < 5; dataIndex++) {
            var duty = "DUTY_" + (dataIndex + 1);
            verb = getData(duty + "_SKILL");
            var d = getData(duty + "_PERCENTAGETIMESPENT");
            if (isNumeric(d)) {
                d = parseInt(d);
                if (d > 0) {
                    total += d;
                    if (verb == "") verb = duty + dataIndex;
                    var dataPoint = {
                        label: verb,
                        data: d
                    }
                    data[count++] = dataPoint;
                    log(dataPoint.label + ' = ' + dataPoint.data);
                }
            }
        }

        if (total != 100) {
            if (total < 100) {
                var missingDataNum = 100 - total;
                var dataPoint = {
                    label: '**MISSING**',
                    data: missingDataNum
                }
                data[count] = dataPoint;
                log(dataPoint.label + ' = ' + dataPoint.data);
            }
        }
        if ($.isFunction($.plot)) {
            log('Plot version: ' + $.plot.version);
            var pie = $.plot($('#pieChart'), data,
                {
                    series: {
                        pie: {
                            show: true,
                            label: {
                                show: true,
                                radius: 1,
                                formatter: function (label, series) {
                                    return '<div style="font-size:11px; text-align:center; padding:2px; color:black;">' + label + '<br />' + Math.round(series.percent) + '%</div>';
                                },
                                background: {
                                    opacity: 0.0
                                }
                            },
                            combine: {
                                color: '#999',
                                threshold: 0.05
                            }
                        }
                    },
                    legend: {
                        show: false,
                        labelFormatter: function (label, series) {
                            return '<b>' + label + '</b>';
                        }
                    }
                });
             }
        }
    }
}

这篇关于将数据作为变量传递时,jQuery Flot数据的格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆