1

我使用了http://codeblitz.wordpress.com/2009/06/22/jquery-charts/提供的代码

它使用 jqPlot。所以我有以下示例代码 Default.html 有效:

<script type="text/javascript">

var jsonObj = { "pageHits": [30, 60, 22, 5, 60, 88, 102], "rssHits": [33, 45, 121, 23, 55, 35, 77], "xAxis": ['Jan 2009', 'Feb 2009', 'Mar 2009', 'Apr 2009', 'May 2009', 'June 2009', 'Jul 2009'] };

$(function () {
    $.jqplot('chartDiv', [jsonObj.pageHits, jsonObj.rssHits], CreateBarChartOptions());
});

    function CreateBarChartOptions()
    {
        var optionsObj = {
            title: 'Blog Statistics',
            axes: {
                 xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: jsonObj.xAxis
                }
            },
            series: [{label:'Page Hits'}, {label: 'RSS Hits'}],
            legend: {
                show: true,
                location: 'nw'
            },
            seriesDefaults:{
                shadow: true,
                renderer:$.jqplot.BarRenderer,
                rendererOptions:{
                   barPadding: 8,
                   barMargin: 10
               }
            },
            highlighter: {
                showTooltip: true,
                tooltipFade: true
            }
        };
        return optionsObj;
    }
</script>

我已复制代码并将其放入 Default.aspx。我唯一想要改变的是能够从外部文件中获取数据,所以现在我的代码是:

<script type="text/javascript">

  var jsonObj;
  $.getJSON('example.json', function (response)
  {
    jsonObj = response;
    alert(jsonObj.property);
  });

$(function () {
  $.jqplot('chartDiv', [jsonObj.pageHits, jsonObj.rssHits], CreateBarChartOptions());
    });

    function CreateBarChartOptions()
    {
      var optionsObj = {
        title: 'Blog Statistics',
        axes: {
          xaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            ticks: jsonObj.xAxis
          }
        },
        series: [{ label: 'Page Hits' }, { label: 'RSS Hits'}],
        legend: {
          show: true,
          location: 'nw'
        },
        seriesDefaults: {
          shadow: true,
          renderer: $.jqplot.BarRenderer,
          rendererOptions: {
            barPadding: 8,
            barMargin: 10
          }
        },
        highlighter: {
          showTooltip: true,
          tooltipFade: true
        }
      };
      return optionsObj;
    }

</script>

但是 jsonObj 总是未定义,我假设我的文件格式不正确。我试过 example.json 来包含这个:

{"pageHits": [30, 60, 22, 5, 60, 88, 102], "rssHits": [33, 45, 121, 23, 55, 35, 77], "xAxis": ['Jan 2009' , '2009 年 2 月', '2009 年 3 月', '2009 年 4 月', '2009 年 5 月', '2009 年 6 月', '2009 年 7 月']}

还有这个:

{"pageHits": [30, 60, 22, 5, 60, 88, 102], "rssHits": [33, 45, 121, 23, 55, 35, 77], "xAxis": ["2009 年 1 月" , "2009 年 2 月", "2009 年 3 月", "2009 年 4 月", "2009 年 5 月", "2009 年 6 月", "2009 年 7 月"]}

但无济于事。我究竟做错了什么?

感谢您的任何帮助,

朱利安

4

1 回答 1

2

您可能需要执行以下操作:

$.getJSON('example.json', function (response)
{
  var jsonObj = response;
  $.jqplot('chartDiv', [jsonObj.pageHits, jsonObj.rssHits], CreateBarChartOptions());
});

您现在拥有它的方式是触发 jqplot 的 annon 函数将运行“内联”,而 ajax 加载仍将继续。

于 2012-07-10T13:46:52.477 回答