首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将天气预报数据载入气象图表资料机

将天气预报数据载入气象图表资料机
EN

Stack Overflow用户
提问于 2018-10-09 08:20:35
回答 1查看 212关注 0票数 1

我对编程很陌生。所以请容忍我。我想用图表显示实时天气预报数据,特别是气温和降水量与时段的关系。我从openweathermap.org获取的天气数据。示例:"https://samples.openweathermap.org/data/2.5/forecast?q=M%C3%BCnchen,DE&appid=b6907d289e10d714a6e88b30761fae22“,我希望在下面的标准图表示例中使用它。

代码语言:javascript
运行
复制
var chart = AmCharts.makeChart("chartdiv", {
        "type": "serial",
        "theme": "dark",
        "dataLoader": {
          "url": "data/serial2.json",
          "showErrors": true,
          "complete": function ( chart ) {
            console.log( "Loading complete" );
          },
          "load": function ( options, chart ) {
            console.log( "File loaded: ", options.url );
          },
          "error": function ( options, chart ) {
            console.log( "Error occured loading file: ", options.url );
          }
        },
        "categoryField": "year",
        "startDuration": 1,
        "rotate": false,
        "categoryAxis": {
          "gridPosition": "start"
        },
        "valueAxes": [{
          "position": "top",
          "title": "Million USD",
          "minorGridEnabled": true
        }],
        "graphs": [{
          "type": "column",
          "title": "Income",
          "valueField": "income",
          "fillAlphas":1,
          "balloonText": "<span style='font-size:13px;'>[[title]] in [[category]]:<b>[[value]]</b></span>"
        }, {
          "type": "line",
          "title": "Expenses",
          "valueField": "expenses",
          "lineThickness": 2,
          "bullet": "round",
          "balloonText": "<span style='font-size:13px;'>[[title]] in [[category]]:<b>[[value]]</b></span>"
        }],
        "legend": {
          "useGraphSettings": true
        },
        "creditsPosition": "top-right",
        "responsive": {
          "enabled": true
        }
      });

      function reloadData() {
        chart.dataLoader.loadData();
      }

我所面临的问题是,天气数据是一个复杂的json,我不能简单地用温度和降水来代替地震场和数值场。

有人能指导我怎么做吗?任何线索都会被发现。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-09 14:51:16

考虑到源JSON格式复杂,不能直接使用AmCharts,您必须使用dataLoader的postProcess回调来获取响应并使其适应您的需要。如果您查看openweathermap示例API响应文档,您将看到它映射出每个字段以及它们对应的内容。感兴趣的主要性质有:main.tempdtrain.3hsnow.3h。您需要为每个点提取这些信息,并将其分配给您的数组。您的API响应在list数组下有每个点,因此您需要遍历它。

下面是postProcess方法的样子:

代码语言:javascript
运行
复制
  "dataLoader": {
    "url": "YOUR API URL HERE",
    "postProcess": function(jsonData) { 
      var newData = []; //dataProvider for your chart

      //loop through your API response's list array for the data you need
      jsonData.list.forEach(function(periodInfo) {
        //set up the data point with the converted timestamp,
        //converted temperature, and placeholder for precipitation
        var dataPoint = {
          "date": periodInfo.dt * 1000, //convert to milliseconds
          "temperature": periodInfo.main.temp - 273.15, //convert kelvin to celsius
          "precipitation": 0
        };
        //check if we have a value for rain precipitation before adding it to our precipitation property
        if (periodInfo.rain !== undefined && periodInfo.rain['3h'] !== undefined) {
          dataPoint.precipitation += periodInfo.rain['3h'];
        }
        //check if we have a value for snow precipitation before adding it in
        if (periodInfo.snow !== undefined && periodInfo.snow['3h'] !== undefined) {
          dataPoint.precipitation += periodInfo.snow['3h'];
        }
        //finally, add it to your new data array
        newData.push(dataPoint);
      });
      //return the new array to be assigned to the chart's dataProvider
      return newData;
    }
  },

现在您已经映射了数据,您必须更新makeChart调用以查找这些属性,方法是使用相应的valueField属性(temperatureprecipitation)创建graph对象,将categoryField设置为date,并创建一个启用parseDatescategoryAxis,并创建一个minPeriod设置为hh,因为数据是每小时一次的。您还可能希望为您的降水值创建第二个值轴。

下面是更新的makeChart属性的片段:

代码语言:javascript
运行
复制
  //create value axes for both temperature and precip values
  "valueAxes": [{
    "id": "temperature",
    "title": "Temperature (C)"
  }, {
    "id": "precipitation",
    "title": "Precipitation (mm)",
    "position": "right"
  }],
  "synchronizeGrid": true, //make sure the grids from both axes are synchronized
  "graphs": [{
    "bullet": "round",
    "valueField": "temperature"
  },{
    "fillAlphas": 0.9,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "precipitation",
    "valueAxis": "precipitation" //plot this against the precipitation value axis
  }],
  "categoryField": "date",
  "categoryAxis": {
    "parseDates": true,
    "minPeriod": "hh" //make sure we plot hourly data correctly
  },

下面是一个使用上述API响应的静态JSON文件的演示来说明这一点。我添加了一些其他的生活质量设置,如光标和设置精度。我建议查看AmCharts API文档以获得更多信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52716398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档