Empty Flot Charts?

2019-08-02 13:43发布

I'm running the code below and getting an empty chart using Flot/jQuery. Ideally what I am after is a bar chart of the data. I'm looked and looked at this with no joy - does anyone have any ideas?

<div id="user_breakdown_placeholder" style="width:300px;height:300px"></div>

<script>
    $(function () {
        var d = [["Unassigned", 310],["Maynard Schumm", 274]];
            var options = {};
        $.plot($("#user_breakdown_placeholder"), d, options);
    });

</script>

4条回答
Root(大扎)
2楼-- · 2019-08-02 13:57

You can do this but you just need to fake up your data a little bit.

$(function () {
    var data = [[0, 310],[1,274]];
    var datasets = [ {
                 "data": data
                     }, 
                   ];
    var options = {
                   bars: {show: true},
                   yaxis: { min: 0 },
                   xaxis: { ticks:  [
                                     [0.5, "Unassigned"],
                                     [1.5, "Maynard Schumm"]
                                    ],
                          },
                  };
   $.plot($("#user_breakdown_placeholder"), datasets, options);
});

------>

alt text

Presumably you can modify your data appropriately before passing it to Flot.

There are lots of great examples of real uses at FlotUsage. e.g. Fedora have some nice bar charts. The great thing is because it's JavaScript you can view source and crib ideas from anyone :)

查看更多
Juvenile、少年°
3楼-- · 2019-08-02 14:00

As Neil Middleton says in his comment, Flot does not support Bar Charts.

Perhaps this or this plugin can help you?

查看更多
甜甜的少女心
4楼-- · 2019-08-02 14:14

An old question but I don't think it was answered. According to the Flot API documentation:

Note that to simplify the internal logic in Flot both the x and y values must be numbers ... If you're getting mysterious errors, double check that you're inputting numbers and not strings.

If the above case you are using strings in your data (e.g. ["Unassigned", 310]) which isn't going to work according to the docs.

查看更多
疯言疯语
5楼-- · 2019-08-02 14:18

The current version of flot (v. 0.6) supports bar charts. Here is an example. In your plot() function, you just have to put:

bars: { show: true}
查看更多
登录 后发表回答