从函数中使用dataloader时,未显示Amchart

时间:2017-03-03 05:17:17

标签: javascript html jsp servlets amcharts

我正在调用函数返回amchart dataloader。单击按钮即可调用该函数。并且单击该按钮也会调用图表。但问题是我没有得到图表。

 function myFunction() {
              var edited = "[{";
                for(var i=0;i<3;i++) {
              var one = document.getElementById("testid"+i).value;
              var two =  document.getElementById("testavg"+i).value;
              edited += '"'+"Test_Id"+'":"'+one+'",'+'"Test_Average"'+':'+two+'},{';
                }
              edited = edited.slice(0,-2);
              edited += "]";
              alert(edited);
              return edited;  
              }


            function charts(){
            var chartDataa = myFunction();  
            var chart = AmCharts.makeChart( "chartdiv", {
                  "type": "serial",
                  "theme": "light",
                  "dataProvider": chartDataa,
                  "valueAxes": [ {
                    "gridColor": "#FFFFFF",
                    "gridAlpha": 0.2,
                    "dashLength": 0
                  } ],
                  "gridAboveGraphs": true,
                  "startDuration": 1,
                  "graphs": [ {
                    "balloonText": "[[category]]: <b>[[value]]</b>",
                    "fillAlphas": 0.8,
                    "lineAlpha": 0.2,
                    "type": "column",
                    "valueField": "Test_Average"
                  } ],
                  "chartCursor": {
                    "categoryBalloonEnabled": false,
                    "cursorAlpha": 0,
                    "zoomable": false
                  },
                  "categoryField": "Test_Id",
                  "categoryAxis": {
                    "gridPosition": "start",
                    "gridAlpha": 0,
                    "tickPosition": "start",
                    "tickLength": 20
                  },
                  "export": {
                    "enabled": true
                  }
                });
  }

The chart that I am getting right now looks like this

1 个答案:

答案 0 :(得分:1)

您的函数myFunction返回一个JSON字符串。 amCharts dataProvider prop需要一个对象数组(一个JS对象,而不是一个JSON字符串)。尝试将myFunction更改为一个函数,该函数创建一个包含数据对象的数组。 (我还建议将其重命名为更具描述性的内容)。以下是如何做到的:

function getChartData() {
          var dataprovider = [];
          for(var i = 0; i < 3; i++) {

            dataprovider.push({
              "Test_Id": document.getElementById("testid"+i).value,
              "Test_Average": document.getElementById("testavg"+i).value
            });

          }

          return dataprovider; 
}


function charts(){
        var chartData = getChartData();  
        var chart = AmCharts.makeChart( "chartdiv", {
              "type": "serial",
              "theme": "light",
              "dataProvider": chartData,
              "valueAxes": [ {
                "gridColor": "#FFFFFF",
                "gridAlpha": 0.2,
                "dashLength": 0
              } ],
              "gridAboveGraphs": true,
              "startDuration": 1,
              "graphs": [ {
                "balloonText": "[[category]]: <b>[[value]]</b>",
                "fillAlphas": 0.8,
                "lineAlpha": 0.2,
                "type": "column",
                "valueField": "Test_Average"
              } ],
              "chartCursor": {
                "categoryBalloonEnabled": false,
                "cursorAlpha": 0,
                "zoomable": false
              },
              "categoryField": "Test_Id",
              "categoryAxis": {
                "gridPosition": "start",
                "gridAlpha": 0,
                "tickPosition": "start",
                "tickLength": 20
              },
              "export": {
                "enabled": true
              }
            });
}

您还可以按照原样保持代码,并将myFunction的最后一行更改为:return JSON.parse(edited);