- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对编程很陌生。所以请多多包涵。我想使用 amcharts 显示实时天气预报数据,特别是温度和降水量与时间段的关系。我从 openweathermap.org 获取的天气数据。示例:“https://samples.openweathermap.org/data/2.5/forecast?q=M%C3%BCnchen,DE&appid=b6907d289e10d714a6e88b30761fae22”我希望它在以下带有数据加载器的标准 amcharts 示例中使用。
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,我无法简单地将类别字段和值字段替换为温度和降水量。
谁能指导我如何去做这件事?任何潜在客户将不胜感激。谢谢你!
最佳答案
鉴于您的源 JSON 格式复杂,不能直接与 AmCharts 一起使用,您必须使用 dataLoader 的 postProcess
回调来获取响应并使其适应您的需求。如果您查看 openweathermap sample API response documentation ,您会看到它映射出每个字段及其对应的内容。感兴趣的主要属性是:main.temp
、dt
、rain.3h
和 snow.3h
。您需要为每个点提取此信息并将其分配给您的数组。您的 API 响应在 list
数组下包含每个点,因此您需要遍历它。
postProcess
方法如下所示:
"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
属性的 graph
对象来查找这些属性(温度
和 precipitation
),将您的 categoryField
设置为 date
并使用 parseDates 创建一个
启用并将 categoryAxis
minPeriod
设置为 hh
因为数据是每小时的。您可能还想为降水值创建第二个值轴。
这是更新后的 makeChart 属性的片段:
//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
},
这是一个 demo使用上述 API 响应的静态 JSON 文件来说明这一点。我添加了一些其他生活质量设置,例如光标和设置精度。我建议查看 AmCharts API 文档以获取更多信息。
关于javascript - 获取天气预报数据以加载到 amcharts 数据加载器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52716398/
amcharts 轴中可以有组吗? 检查 highcharts 中的示例:example 最佳答案 有点......这不是官方功能,但你可以伪造它。 您可以为标签创建一个图表,其中 valueFiel
有人知道将 amcharts 中的气球对齐方式更改为图 1 中的对齐方式的属性吗?默认情况下,我的项目中呈现的图表中的气球对齐方式如图 2 所示,如果气球文本为,有时会导致截断和溢出,如图 3 所示太
一些 AmCharts demos使用AmCharts.makeChart 其他 AmCharts demos使用new AmCharts.AmSerialChart(); 这两种方法有什么区别? 最
我正在使用带有 Angular 和 TypeScript 的 AmChart V4( map )。 目前,当我构建用于生产的应用程序时,我得到一个“pdfmake”文件,它的成本约为 2 MB。是否可
目前我正在尝试设置当您将鼠标悬停在具有动态内容(公司名称)的 map 图像上时出现的工具提示的样式。 我的目标是将背景设置为特定颜色,为字体指定颜色并应用 CSS 属性“box-shadow”。 对于
如何将下图中的默认黄色替换为红色?基本上,我需要实现红色和绿色的组合。 提前致谢!! 最佳答案 lineColor 和 fillColors 是 AmGraph 负责颜色的属性。 关于amcharts
如何删除 am 图表中的侧刻度轴。例如。在这个 fiddle 中,我想删除顶部的鳞片和左侧的鳞片。我需要操作哪些属性或方法。 演示图表。 http://jsfiddle.net/JSTQW/ 目前,我
问题: 我无法使用 amcharts 库将粗体属性传递给 Axis 类别中所需的标签。 案例: 我可以使用数据集本身为图表的每个元素传递 fillColor。这是我正在使用的数据集结构的示例: var
在 IcCube 报告中,我添加了一个 amchart,在小部件高级属性中,我将 Scrollbar.Hide Resize Grips 设置为“no”以显示滚动... 滚动可见,但拖动图标不可见..
我在我的应用程序中使用条形图(amcharts),我需要包含该条形图的图例。所以,我为栏脚本添加了以下脚本, legend = new AmCharts.AmLegend(); legend.posi
Amchart 为我提供了令人惊叹的 HTML 图形,但是... 如何设置数轴格式?目前它显示了 65,000,我需要像 65000 这样的值。没有逗号! 谢谢! 最佳答案 我解决了问题! 进入AmC
在 AmMap 中,存在“homeButtonClicked”事件,当您单击主页按钮时会触发该事件。 我想手动触发该事件。有什么办法可以做到吗? 最佳答案 要完全缩小 map ,请使用 zoomToS
我正在使用免费版本的 AmCharts,我有一个简单的问题 - 图表加载后如何删除?我在网上搜索过,但没有找到太多内容。 最佳答案 如果您想销毁图表对象,请调用: chart.clear(); 然后将
我有一个条形列 amchart,并且每列上都有单击事件。但是我如何将鼠标光标更改为其他图标(例如手)以使用户知道该列是可点击的 最佳答案 您可以使用showHandOnHover如果您希望鼠标指针悬停
chart.columnSpacing 属性不适用于简单柱形图。我需要最小化两列之间的空间。 如何在简单柱形图中最小化列之间的空间? 最佳答案 你可以尝试添加... "columnWidth": [n
我正在使用 AMCHARTS 创建商业智能应用程序。该应用程序应该是多语言的。如何将 AMCHARTS 制作的图表转换为 rtl? 最佳答案 目前 amCharts 还没有可以翻转整个图表的一键式解决
我得到了以下动态图表(见附图)。为了可重复性,我根据标准 icCube 的销售模型创建了一个非常简单的图表。 这个想法是在列轴上有两个事实以及选定年份的数据。为了区分这些事实,我想给那些随着时间的推移
在 amcharts 3 中有 categoryAxis.guides。但是我无法在 amcharts 4 中找到它。下面是我能够在 amcharts 3 中使用的指南数组 var guidesArr
我现在正在使用 amcharts flash。参见这里:http://www.myinvestmentdecision.com.au/Sample-Results 我想更改它以使用 javascrip
我正在尝试在同一页面上创建 2 个 Amchart 实例。在这里,我手动放置数据。但是我使用 dataloader 从 php 文件加载数据,使用 mysql 输出 json。为此,我将使用 2 个不
我是一名优秀的程序员,十分优秀!