【问题标题】:How to use the ajax Json Web Method with amcharts in asp.net如何在asp.net中使用带有amcharts的ajax Json Web Method
【发布时间】:2016-04-22 04:04:38
【问题描述】:

Demo like this map//这是图表功能..我想将国家/地区ID传递给地图上的聊天数据//使用ajax json web方法。 //Error: Uncaught TypeError: Cannot read property '0' of undefined.

    var chart;
    var map;
    var chartData = {};

    chartData.world = [
        { source: "Oil", energy: 3882.1 },
        { source: "Natural Gas", energy: 2653.1 },
        { source: "Coal", energy: 3278.3 },
        { source: "Nuclear", energy: 610.5 },
        { source: "Hydro", energy: 740.3 }];
    chartData.KH = [
       { source: "Oil", energy: 404.6 },
       { source: "Natural Gas", energy: 79.8 },
       { source: "Coal", energy: 1537.4 },
       { source: "Nuclear", energy: 15.9 },
       { source: "Hydro", energy: 139.3 }];
    chartData.CN = [
        { source: "Oil", energy: 842.9 },
        { source: "Natural Gas", energy: 588.7 },
        { source: "Coal", energy: 498 },
        { source: "Nuclear", energy: 190.2 },
        { source: "Hydro", energy: 62.2 }];    

    chartData.MY = [
        { source: "Oil", energy: 124.9 },
        { source: "Natural Gas", energy: 350.7 },
        { source: "Coal", energy: 82.9 },
        { source: "Nuclear", energy: 37 },
        { source: "Hydro", energy: 39.8 }];
    chartData.PH = [
       { source: "Oil", energy: 148.5 },
       { source: "Natural Gas", energy: 46.7 },
       { source: "Coal", energy: 245.8 },
       { source: "Nuclear", energy: 3.8 },
       { source: "Hydro", energy: 24 }];
    chartData.TH = [
        { source: "Oil", energy: 148.5 },
        { source: "Natural Gas", energy: 46.7 },
        { source: "Coal", energy: 245.8 },
        { source: "Nuclear", energy: 3.8 },
        { source: "Hydro", energy: 24 }];

    chartData.ID = [
        { source: "Oil", energy: 197.6 },
        { source: "Natural Gas", energy: 78.7 },
        { source: "Coal", energy: 108.8 },
        { source: "Nuclear", energy: 62.1 },
        { source: "Hydro", energy: 16.7 }];
    chartData.VN = [
     { source: "Oil", energy: 197.6 },
     { source: "Natural Gas", energy: 78.7 },
     { source: "Coal", energy: 108.8 },
     { source: "Nuclear", energy: 62.1 },
     { source: "Hydro", energy: 16.7 }];

    AmCharts.ready(function () {
        // *** CREATE CHART *********************************************************
        // PIE CHART
        chart = new AmCharts.AmPieChart();

        // title of the chart
        chart.addLabel("0", "!20", "World", "center", 16);

        chart.backgroundAlpha = 0.4;
        chart.backgroundColor = "#000000";
        chart.dataProvider = chartData.world;
        chart.titleField = "source";
        chart.valueField = "energy";
        chart.sequencedAnimation = true;
        chart.startEffect = "elastic";
        chart.labelsEnabled = false;
        chart.labelText = "[[title]]";
        chart.startDuration = 2;
        chart.labelRadius = 10;

        // WRITE                                 
        chart.write("chartdiv");

        // *** CREATE MAP ***********************************************************

        var chartData1 = generateChartData();
        alert(chartData1);
        map = new AmCharts.AmMap();
        map.pathToImages = "http://www.ammap.com/lib/images/";
        //map.panEventsEnabled = true; // this line enables pinch-zooming and dragging on touch devices
        var dataProvider = {
            mapVar: AmCharts.maps.worldLow
        };
        map.areasSettings = {
            unlistedAreasColor: "#DDDDDD",
            rollOverOutlineColor: "#FFFFFF",
            rollOverColor: "#CC0000"
        };
        dataProvider.areas = [
            { title: "Cambodia", id: chartData1[0].id, selectable: true },
             //{ title: "Cambodia", id:"KH", selectable: true },
            { title: "China", id: chartData1[1].id, selectable: true },
            { title: "Indonesia", id: chartData1[2].id, selectable: true },
            { title: "Malaysia", id: chartData1[3].id, selectable: true },
            { title: "Philippines", id: chartData1[4].id, selectable: true },
            { title: "Thailand", id: chartData1[5].id, selectable: true },
            { title: "Vietnam", id: chartData1[6].id, selectable: true }
        ];

        map.dataProvider = dataProvider;
        map.write("mapdiv");

        map.addListener("clickMapObject", function (event) {
            if (event.mapObject.id != undefined && chartData[event.mapObject.id] != undefined) {
                chart.dataProvider = chartData[event.mapObject.id];
                chart.clearLabels();
                chart.addLabel("0", "!20", event.mapObject.title, "center", 16);
                chart.validateData();
            }
        });

    });

//-----这是我正在调用的函数..返回值-------


  function generateChartData() {
        debugger;
        var chartData2 = [];
        var chartDataResults = new Array();
             $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            url: 'varmap.aspx/GetDataonload',
            data: {},
            success: function (response) {
                var aData = response.d;

                for (var i = 0; i < aData.length; i++) {
                 var country = aData[i].costtype; 
                    alert(country);
                        chartDataResults.push({
                            id: country
                                        });
                               }
                return chartDataResults;
            }
        });
     }

【问题讨论】:

    标签: json asp.net-ajax amcharts


    【解决方案1】:

    jQuery 中的 AJAX 请求是异步的。这意味着您的 generateChartData() 函数在实际加载数据之前完成并继续创建没有任何数据的图表。

    你需要在AJAX调用success处理函数开始创建图表:

    function createChart() {
      debugger;
      var chartData2 = [];
      var chartDataResults = new Array();
      $.ajax( {
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        url: 'varmap.aspx/GetDataonload',
        data: {},
        success: function( response ) {
          var aData = response.d;
    
          for ( var i = 0; i < aData.length; i++ ) {
            var country = aData[ i ].costtype;
            alert( country );
            chartDataResults.push( {
              id: country
            } );
          }
    
          // create the chart here
          // PIE CHART
          chart = new AmCharts.AmPieChart();
    
          // title of the chart
          chart.addLabel( "0", "!20", "World", "center", 16 );
    
          chart.backgroundAlpha = 0.4;
          chart.backgroundColor = "#000000";
          chart.dataProvider = chartDataResults;
          chart.titleField = "source";
          chart.valueField = "energy";
          chart.sequencedAnimation = true;
          chart.startEffect = "elastic";
          chart.labelsEnabled = false;
          chart.labelText = "[[title]]";
          chart.startDuration = 2;
          chart.labelRadius = 10;
    
          // WRITE                                 
          chart.write( "chartdiv" );
        }
      } );
    }
    
    $( document ).ready( createChart );
    

    【讨论】:

    • 您能否将代码粘贴到函数中的排列中..我不知道如何使用它..Thnks @martynasma
    • Uncaught TypeError: AmCharts.AmPieChart is not a constructor..Tihis error is come when using this code..你能看看演示链接..就像我想做Ammap一样..跨度>
    • 请帮帮我..我需要制作地理图表。@martynasma
    • 你包括pie.js吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2014-08-02
    • 2016-11-08
    相关资源
    最近更新 更多