0

我正在尝试在 jqplot 中创建一个饼图。我不确定我是否在这里遗漏了任何东西,但图表根本没有呈现。

下面是我在 Rails 中的dashboard_controller 中的一种操作方法:

def index
    @trans = Transaction.joins(:category).group("categories.name").sum(:amount)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @trans }
    end
 end

这是我执行action方法后得到的json数据。

{"Bills & Utilities":1019.11,"Uncategorized":196.57,"Home":30.29,"Entertainment":300.0,"Food & Dining":465.45,"Personal Care":63.92,"Auto & Transport":571.44}

这是我的 html 和 javascript

<script type="text/javascript" src="/assets/jqplot/jqplot.js"></script>
<script type="text/javascript" src="/assets/jqplot/plugins/jqplot.pieRenderer.min.js"></script>
<script type="text/javascript" src="/assets/jqplot/plugins/jqplot.ciParser.js"></script>
<script type="text/javascript" src="/assets/jqplot/plugins/jqplot.json2.js"></script>
<link rel="stylesheet" type="text/css" href="/assets/jqplot/jqplot.css" />

<div id="chart10" style="margin-top:20px; margin-left:20px; width:460px; height:300px;"></div>

<script class="code" type="text/javascript">
    $(document).ready(function() {
        $.getJSON('/dashboard/index.json', function(data){
            //var data = [['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14]];
            plot = jQuery.jqplot('chart10', data, {
                title: ' ',
                //dataRenderer: $.jqplot.ciParser,
                seriesDefaults: {
                    shadow: true,
                    renderer: $.jqplot.PieRenderer,
                    rendererOptions: {
                        fill: false,
                        sliceMargin: 4,
                        showDataLabels: true
                        }
                },
                legend: { show:true }
            });
        });
    }); // document.ready
</script>

有什么想法可能是错的吗?

4

1 回答 1

0

我错过了什么吗?您的 json 返回变量data如下所示:

{"Bills & Utilities":1019.11,"Uncategorized":196.57,"Home":30.29,"Entertainment":300.0,"Food & Dining":465.45,"Personal Care":63.92,"Auto & Transport":571.44}

虽然 jqPlot 想要这样:

[["Bills & Utilities",1019.11],["Uncategorized",196.57],["Home",30.29],["Entertainment",300.0],["Food & Dining",465.45],["Personal Care",63.92],["Auto & Transport":571.44]]

编辑评论

以正确的格式放置数据:

data = {"Bills & Utilities":1019.11,"Uncategorized":196.57,"Home":30.29,"Entertainment":300.0,"Food & Dining":465.45,"Personal Care":63.92,"Auto & Transport":571.44};

jqData = []
$.each(data, function(k,v){jqData.push([k,v]);});

在这里看小提琴。

于 2012-08-29T13:42:04.037 回答