javascript - 带有ajax数据加载的Highcharts Windbarb
问题描述
好的。改写之前未回答的问题。
我有一个如下图所示的图表,其中数据通过 ajax 动态加载。此图的简化版本(没有 ajax)可以正常工作,如jsfiddle所示。
下面的代码是我使用它的图表,直接来自它的上下文。
这个想法是图中有几个时间序列(x 轴),我希望通过风刺显示这些时间的风速和风向特征。windbarbs 在每个定义的 x 值处采用两个参数,所有其他系列采用一个参数(y 轴)。
我的问题是:为什么它不起作用(为什么是 stackdump),而它在 jsfiddle 中起作用(没有 ajax 调用,因此没有 addSeries 调用?
作为派生:在 jsfiddle 中,onSeries 属性不起作用。那里有什么问题?
最后:是否有可能在系列图上方获得风刺而不是固定在 x 轴上?
问题似乎出在加载其他系列之后加载风数据。由于该部分在具有 (idx == 'wind') 条件的 ajax 调用中,因此很容易发现。它使用以下堆栈转储中断了 highStock 的 setData 调用:
Uncaught TypeError: r is undefined
setData highstock.src.js:33902
init highstock.src.js:33182
init highstock.src.js:54816
init windbarb.src.js:361
initSeries highstock.src.js:27886
addSeries highstock.src.js:36888
fireEvent highstock.src.js:2112
addSeries highstock.src.js:36887
success line 2 > injectedScript:176
success line 2 > injectedScript:171
jQuery 6
doSensorIn2p5 line 2 > injectedScript:162
SetGraphView0 line 2 > injectedScript:276
onclick (index):1
没有风数据它工作正常。
var doSensorIn10 = function(){
let ReferenceColours = ['#79bc6a', '#bbcf4c', '#eec20b', '#f29305', '#960018' ];
let ReferenceConcentrations10 = [0,25,50,90,180];
let t ={chart: { renderTo: 'chartcontainerIn10',type: 'spline',alignTicks: false, zoomType: 'xy', pinchType: 'xy'},
title: { text: 'AirQuality Sensor In for PM10'},
credits: { enabled: true},
xAxis: { type: 'datetime', ordinal: false, dateTimeLabelFormats: { day: '%e %b',week: '%e %b %y',month: '%b %y',year: '%Y'} },
yAxis:
[{
title: { text: 'Luchtkwaliteit (μg/m3)'},
opposite: false, labels: { align: 'right',x: -5},
},
{
linkedTo: 0,
gridLineWidth: 0,
opposite: true,
title: { text: null},
labels: { align: 'left',x: 5},
tickInterval: 20
}],
legend: { enabled: true},
tooltip: { valueSuffix: 'μg/m3',valueDecimals: 1,xDateFormat: '%A, %b %e, %H:%M'},
series:[],
rangeSelector:
{
buttons:[{ count: 6,type: 'hour',text: '6h'},
{ count: 12,type: 'hour',text: '12h'},
{ type: 'all',text: 'All'}],
inputEnabled: false
}
};
let chart = new Highcharts.StockChart(t);
chart.showLoading();
$.ajax({
url: 'airlinkdataIn10.json',
cache: false,
dataType: 'json',
success: function(resp){
let titles = {
'In_pm10': 'In_pm10','In_pm10_1hr': 'In_pm10_1hr','In_pm10_3hr': 'In_pm10_3hr','In_pm10_24hr': 'In_pm10_24hr','In_pm10_nowcast': 'In_pm10_nowcast','wind': 'wind'}
let idxs = ['In_pm10','In_pm10_1hr','In_pm10_3hr','In_pm10_24hr','In_pm10_nowcast','wind']
let cnt = 0;
idxs.forEach(function(idx) {
console.log('idx = ' + idx);
if (idx in resp) {
if (idx == 'wind') {
console.log('Doing Wind correctly : Before addSeries');
chart.addSeries({name: titles[idx], type: 'windbarb', showInLegend: false, onSeries: 'InPM2p5', tooltip: {valueSuffix: ' m/s'}, data: resp[idx] }, false);
console.log('Doing Wind correctly : After addSeries');
}
else {
console.log('Doing ' + idx + ' correctly');
chart.addSeries({name: titles[idx], data: resp[idx]}, false);
}
chart.series[cnt].options.zIndex = cnt+50;
}
cnt++;
});
chart.hideLoading();
chart.redraw();
}
}
)};
数据系列(简称)如下:
{"In_pm2p5":[[1609484460000,26.20], ... ]],
"In_pm2p5_1hr":[[1609484460000,32.90], ... ]],
...
"wind":[[1609484460000,0.0,194], ...]]}
每个参数有 2880 个值,wind 可能少一个值(我在 jsfiddle 中测试过,似乎没有问题)。
解决方案
感谢您广泛而清晰的描述!
- 在这种情况下,如果不复制您的数据获取,就很难说为什么图表不能正确呈现。该功能应该可以正常工作,并且类型系列
addSeries
没有任何问题。windbarb
演示:https ://jsfiddle.net/BlackLabel/ktLyunw8/
2.
在 jsfiddle onSeries 属性不起作用。那里有什么问题?
3.
最后:是否有可能在系列图上方获得风刺而不是固定在 x 轴上?
就像在使用该onSeries
功能的情况下一样?还是将其完全渲染在情节区域之上?
我想将它渲染在绘图区域上方或绘图区域的顶部(就像没有 onSeries 它在底部),所以所有的风刺都在一行中。
在这种情况下,您可以渲染第二个xAxis
并将 winbarb 系列分配给它。
推荐阅读
- r - 将大型数据集的多个标题列转换为行
- uipath - UiPath 自动在网页上按顺序点击列表
- tensorflow - 使用 TensorFlow 进行迁移学习(MobileNet)
- lua - Gmod Lua HUDPaint 脚本未显示尝试使用绘图函数更改 HUD,为什么?
- python-3.x - ssl.CertificateError:主机名“example.com”与“exampleserver.com”中的任何一个都不匹配?
- vert.x - Vertx 事件总线中的限制消息
- laravel - 是否可以使用 Laravel 和 VueJS 监听 DOM 内事件?
- numpy - NumPy:如何根据某些条件确定 ndarray 的第一个轴的索引?
- c - 为什么这些定义在 C 中不合法?
- mysql - Mysql Batch 将大约 11 GB 的数据从一个表插入到另一个表