从JSON读取温度数据至Highcharts失败,请求排查问题
问题分析与修复方案
核心错误点
- 语法错误:你代码里的
function (data->temp)和data->temp不是合法的JavaScript语法,JS中不存在这种箭头式的参数解构或属性访问写法。必须用标准的参数接收和.属性访问方式。 - 数据格式适配(潜在问题):Highcharts的datetime轴要求数据点为
[时间戳, 数值]的数组结构,如果你的JSON返回的时间是字符串格式,需要先转换为时间戳才能正确渲染。
修正后的代码
Highcharts.getJSON( 'https://www.pogodakrotoszyn.info/tempdata.json', function (data) { // 如果JSON中的时间是字符串(如"YYYY-MM-DD HH:MM:SS"),取消下面注释转换为时间戳 // const formattedData = data.temp.map(item => [new Date(item[0]).getTime(), item[1]]); Highcharts.chart('container', { chart: { zoomType: 'x' }, title: { text: 'Temperature', align: 'left' }, subtitle: { text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in', align: 'left' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: 'Temperature' } }, legend: { enabled: false }, plotOptions: { area: { fillColor: { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [ [0, Highcharts.getOptions().colors[0]], [1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')] ] }, marker: { radius: 2 }, lineWidth: 1, states: { hover: { lineWidth: 1 } }, threshold: null } }, series: [{ type: 'area', name: 'Temperature', data: data.temp // 替换为标准属性访问写法 // 若需转换时间格式,替换为:data: formattedData }] }); } );
内容的提问来源于stack exchange,提问作者Shamy
相关产品推荐
相关产品推荐

