【问题标题】:Turn $get result string into javascript array将 $get 结果字符串转换为 javascript 数组
【发布时间】:2013-06-07 03:33:57
【问题描述】:

我希望使用 jqplot 插件 (http://www.jqplot.com/tests/pie-donut-charts.php) 来生成饼图,但是我无法将 $get 函数的结果转换为 jqplot 函数将接受为可用数据的内容。

您可以在上面的 jqplot 链接中看到我需要如何创建数据变量来显示饼图。

这是我的 $.get 函数,它当前返回一个字符串:

//data is returned as a string: 'some_field_name',10,'some_other_field,33 etc
$.get("notesajax.php", {month:monthFilter, area:areaFilter}, function(data) {
    var arr = data.split(",");
    var results = [];
    for (var i = 0; i < arr.length; i++) {
        mini = [];
        mini[arr[i]] = "'"+arr[i]+"',"+arr[i+1];
        results.push(mini);
        i++;
    }

为了方便参考,这里是 jqplot 函数,在开始时包括一个定义的“数据”变量,以说明 jqplot 期望如何接收数据。

//this variable is just for illustrative purposes
var data = [
    ['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14], 
    ['Out of home', 16],['Commuting', 7], ['Orientation', 9]
];

var plot1 = jQuery.jqplot ('chartdiv', [results], 
    { 
        seriesDefaults: 
        {
            // Make this a pie chart.
            renderer: jQuery.jqplot.PieRenderer, 
            rendererOptions: {
                // Put data labels on the pie slices.
                // By default, labels show the percentage of the slice.
                showDataLabels: true
            }
        }, 
        legend: { show:true, location: 'e' }
    }
);

但到目前为止,我无法将我的 $get 返回数据转换为 jqplot 函数可以接受的格式。

【问题讨论】:

    标签: javascript jquery multidimensional-array get


    【解决方案1】:
    var field, value, data = [], str = "'some_field_name',10,'some_other_field,33";
    var arr = str.split(',');
    // just add 2 to each iteration to get every second item (last argument is i += 2):
    for (var i = 0; i < arr.length; i += 2) {
      field = arr[i].replace(/'/g, ""); // replace ', because otherwise your string will be "'some_field_name'"
      value = parseInt(arr[i+1], 10); // parseInt because you want numbers, but got a string
      data.push([field, value]); // push into your data-array an array with your field and value
    }
    

    jsfiddle 在这里:http://jsfiddle.net/wLxyZ/

    【讨论】:

      【解决方案2】:

      您正在生成错误的数组。您应该直接从服务器发回 JSON 对象,这是推荐且简单的,或者让您的解析脚本正常。

      $.get("notesajax.php", {month:monthFilter, area:areaFilter}, function(data) {
      var arr = data.split(",");
      var results = [];
      for (var i = 0; i < arr.length; i++) {
          var mini = new Array();
          mini.push(arr[i]);
          mini.push(arr[i+1]);
          results.push(mini);
      }
      

      【讨论】:

        【解决方案3】:

        我使用了您的代码并得出了以下解决方案,您的原始代码存在一些问题,尤其是在您如何构建迷你数组方面

        var arr = ['some_field_name',10,'some_other_field',33];
        var results = [];
        for (var i = 0; i < arr.length; i+=2) {        
            var mini = ["'"+arr[i]+"'",+arr[i+1]];
            results.push(mini);
        }
        alert(results[0][0]);
        

        【讨论】:

          猜你喜欢
          • 2013-04-12
          • 2010-12-13
          • 1970-01-01
          • 2022-01-23
          • 2020-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多