2013-02-02 58 views
0

这就是我的JsonjQuery的解析阵列的Json

[{"name":"Male","data":[10,34,30]},{"name":"Female","data":[20,22,15]},{"name":"Male","data":[22,21,21]},{"name":"Female","data":[13,20,31]}] 

,然后推动它来创建图表,我做了一些事情错了,在这里我的代码:

<!DOCTYPE HTML> 
    <html> 
<head> 

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Highcharts Example</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.3.5/jquery.min.js"></script> 
    <script type="text/javascript"> 



      /* var items = [{ 
       "name": "Male", 
       "data": [10, 34, 30] 
      }, { 
       "name": "Female", 
       "data": [20, 22, 15] 

      }];*/ 

       var chart; 
     $(document).ready(function() { 
      //define the options 
      var options = { 
       chart: { 
        renderTo: 'container', 
        type: 'bar' 
       }, 
       title: { 
        text: 'Historic World Population by Region' 
       }, 
       subtitle: { 
        text: 'Source: Wikipedia.org' 
       }, 
       xAxis: { 
        categories: ['Age 20-24', 'Age 25-30', 'Age 30-40'], 
        title: { 
         text: null 
        } 
       }, 
       yAxis: { 
        min: 0, 
        title: { 
         text: 'Population (millions)', 
         align: 'high' 
        }, 
        labels: { 
         overflow: 'justify' 
        } 
       }, 
       tooltip: { 
        formatter: function() { 
         return '' + 
          this.series.name + ': ' + this.y + ' millions'; 
        } 
       }, 
       plotOptions: { 
        bar: { 
         dataLabels: { 
          enabled: true 
         } 
        } 
       }, 
       legend: { 
        layout: 'vertical', 
        align: 'right', 
        verticalAlign: 'top', 
        x: -100, 
        y: 100, 
        floating: true, 
        borderWidth: 1, 
        backgroundColor: '#FFFFFF', 
        shadow: true 
       }, 
       credits: { 
        enabled: false 
       }, 
       series: [{ 

       }] 
      }; 


      //Calls the JSON 
      jQuery.getJSON("XSportWithAgegroup", 
          null, function (items) { 
           //Creates the new series as stated in the documentation 
           //http://www.highcharts.com/ref/#series 
           var series = { 

            name: 'Browser Share', 
            data: [] 
           }; 
           jQuery.each(items, function (itemNo, item) { 
            //Get the items from the JSON and add then 
            //to the data array of the series 



             series.data.push({ 
              name: item.name, 
              y: item.data[0], 
             }) 

           }); 
           options.series.push(series); 
           //Create the chart 
           chart = new Highcharts.Chart(options); 
           chart.render(); 
          }); 
     }); 

    </script> 
</head> 
<body> 

    <script src="http://code.highcharts.com/highcharts.js"></script> 
    <script src="http://code.highcharts.com/modules/exporting.js"></script> 

    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

</body> 

这里我methoed将查询结果转换为json:

public JsonResult XSportWithAgegroup() { 

     var series = (from m in db.Agegroup 
        where m.Gender == "Male" || m.Gender == "Female" 
        select new 
        { 
         name = m.Gender, 
         data = new List<double> { m.Age20To24, m.Age25To30, m.Age30To40 } 
        }).ToArray(); 

     return Json(series, JsonRequestBehavior.AllowGet); 

    } 

我想我的问题是与jQuery的 图表的演示是:http://www.highcharts.com/demo/bar-basic

更新:

的json:

[{"name":"Male","data":[10,34,30]},{"name":"Female","data":[20,22,15]}] 

我的JQuery:

//Calls the JSON 
      jQuery.getJSON("XDiesesWithAgegroup", 
          null, function (items) { 
           //Creates the new series as stated in the documentation 
           //http://www.highcharts.com/ref/#series 
           var series = { name: 'll', data: [] }; 




           jQuery.each(items, function (itemNo, item) { 
            //Get the items from the JSON and add then 
            //to the data array of the series 


            for (i = 0; i <= 3; i++){ 
             series.data.push({ 
              name: item.name, 
              y: item.data[i], 
             }); 


            }//end for 

           }); 

           options.series.push(series); 

           //Create the chart 
           chart = new Highcharts.Chart(options); 
           chart.render(); 

          }); 

这里的图表如何看起来像现在:

+0

你有没有尝试添加图表=新Highcharts.Chart(选件);外面的jQuery.getJSON块,我面临类似的问题,但通过在系列更新代码的底部创建图表,但在document.reaydy – Yashprit

+0

yah有一段时间了。但在我的情况下,jQuery的推动。 – user1476956

回答

0

你有没有财产Gender在JSON数组,只是name。你可以改变你的JSON数据使用Gender代替name或更改代码的另一种方式圆

series.data.push({ 
    name: item.name, 
    y: item.data[i], 
}); 

有几点与大家发布的代码。当你看着控制台时,它们都显示出来。我将代码复制到JSFiddle中,并与Highcharts - How To Use一起修改了一下

y: item.data[i],i未定义。我只花了第一个元素没有定义

series.data.push({ 
    name: item.Gender, 
    y: item.data[0], 
}) 

options和没有目标图表

HTML:

<div id="container"></div> 

JS:

var options = { 
    chart: { renderTo: 'container' }, 
    series: [] 
}; 

我认为主要的一点是,您必须为图表提供一个目标。

更新

您的数据与您所提供的链接进行比较,我只是下降的循环,并直接分配你的JSON数据series。结合酒吧类型,图表开始看起来类似。

var options = { 
    chart: { renderTo: 'container', type: 'bar' }, 
    series: items 
}; 

JSFiddle

更新2

所以你只有两个条目,MaleFemale,与每次三子条目。 Y轴标记为Age 20-24,Age 25-30Age 30-40

var options = { 
    chart: { renderTo: 'container', type: 'bar' }, 
    xAxis: { 
       categories: ['Age 20-24', 'Age 25-30', 'Age 30-40'], 
       title: { 
        text: null 
       } 
      }, 
    series: items 
}; 

JSFiddle

更新3

var options = { 
    chart: { renderTo: 'container', type: 'bar' }, 
    xAxis: { 
       categories: ['Age 20-24', 'Age 25-30', 'Age 30-40'], 
       title: { 
        text: null 
       } 
      }, 
    series: null 
}; 

jQuery.getJSON("XDiesesWithAgegroup", function (items) { 
    options.series = items; 

    //Create the chart 
    chart = new Highcharts.Chart(options); 
}); 
+0

你说什么是对的,我改变了,但我仍然看不到图表。 – user1476956

+0

我更新我的代码见上面我有'容器' – user1476956

+0

请再次检查我的代码,我使用了错误的图表类型,现在至少获取图表,但不是与我提供的演示文件相同的格式。 – user1476956