highcharts如何实现动态刷新
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了highcharts如何实现动态刷新相关的知识,希望对你有一定的参考价值。
参考技术A highcharts如何实现动态刷新实现的是折线图。目前已经实现了从数据库中加载数据,但是定时刷新数据不知道怎么实现。
使用chart.series[0].setData(data);每次刷新后表数据就没有了,不知道是不是data的格式写错了,
想问下参数data的数据格式的例子。
function getForm()
//使用JQuery从后台获取JSON格式的数据
$.ajax(
type: "POST",
url: "chart.ashx",
success: function(data)
chart.series[0].setData(data);
,
error:function(msg)
alert("通信错误!");
);
$(document).ready(function()
//每隔1秒自动调用方法,实现图表的实时更新
window.setInterval(getForm,10000);
);本回答被提问者和网友采纳
我如何在Highcharts图表上使用动态数据?
我正在从页面上的Websocket连接接收数据,我想在Highcharts深度图上绘制该数据。
深度图:https://www.highcharts.com/docs/stock/depth-chart
这是我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script language="javascript">
mySocket.onmessage = function(event) {
var data = JSON.parse(event.data);
rawAsks = data['asks']
rawBids = data['bids']
const asks = rawasks.map(x => x.map(y => parseFloat(y))) //DATA TO USE ON THE CHART
const bids = rawbids.map(x => x.map(y => parseFloat(y))) //DATA TO USE ON THE CHART
};
Highcharts.chart('container', {
chart: {
type: 'area',
zoomType: 'xy'
},
title: {
text: 'ETH-BTC Market Depth'
},
xAxis: {
minPadding: 0,
maxPadding: 0,
},
yAxis: [{
lineWidth: 1,
gridLineWidth: 1,
title: null,
tickWidth: 1,
tickLength: 5,
tickPosition: 'inside',
labels: {
align: 'left',
x: 8
}
}, {
opposite: true,
linkedTo: 0,
lineWidth: 1,
gridLineWidth: 0,
title: null,
tickWidth: 1,
tickLength: 5,
tickPosition: 'inside',
labels: {
align: 'right',
x: -8
}
}],
legend: {
enabled: false
},
plotOptions: {
area: {
fillOpacity: 0.2,
lineWidth: 1,
step: 'center'
}
},
tooltip: {
headerFormat: '<span style="font-size=10px;">Price: {point.key}</span><br/>',
valueDecimals: 2
},
series: [{
name: 'Bids',
data: Bids, //HERE GOES THE DATA
color: '#03a7a8'
}, {
name: 'Asks',
data: Asks, //HERE GOES THE DATA
color: '#fc5857'
}]
});
</script>
这里是我的图表存在的问题:1)要绘制的数据是asks
和bids
。问题是这两个变量从websocket连接每秒更新一次,因此它们不是静态值。 2)如何将asks
和bids
传递到Highcharts图表,并且每次刷新数据时如何刷新图表?预先感谢!
这里是我收到的需要绘制的数据的示例:
var sampleData = [
[7062.24, 0.402181],
[7062.56, 2.472812],
[7062.59, 0.006595],
[7063.01, 1.2001],
[7063.27, 0.4],
[7063.28, 0.100615],
[7063.48, 0.4],
[7063.76, 0.086983],
[7063.83, 0.005],
[7064.19, 0.399752],
[7064.2, 1.70819],
[7064.41, 0.25],
[7064.43, 0.015026],
]
答案
您可以使用map
功能如下获取每个series
数据
var data = [
[7062.24, 0.402181],
[7062.56, 2.472812],
[7062.59, 0.006595],
[7063.01, 1.2001],
[7063.27, 0.4],
[7063.28, 0.100615],
[7063.48, 0.4],
[7063.76, 0.086983],
[7063.83, 0.005],
[7064.19, 0.399752],
[7064.2, 1.70819],
[7064.41, 0.25],
[7064.43, 0.015026],
];
const asks = data.map(x => x[0]); //DATA TO USE ON THE CHART
const bids = data.map(x => x[1]); //DATA TO USE ON THE CHART
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://code.highcharts.com/highcharts.js"></script>
<div id = "container"> </div>
<script language = "javascript">
var data = [
[7062.24, 0.402181],
[7062.56, 2.472812],
[7062.59, 0.006595],
[7063.01, 1.2001],
[7063.27, 0.4],
[7063.28, 0.100615],
[7063.48, 0.4],
[7063.76, 0.086983],
[7063.83, 0.005],
[7064.19, 0.399752],
[7064.2, 1.70819],
[7064.41, 0.25],
[7064.43, 0.015026],
];
const asks = data.map(x => x[0]); //DATA TO USE ON THE CHART
const bids = data.map(x => x[1]); //DATA TO USE ON THE CHART
Highcharts.chart('container', {
chart: {
type: 'area',
zoomType: 'xy'
},
title: {
text: 'ETH-BTC Market Depth'
},
xAxis: {
minPadding: 0,
maxPadding: 0,
},
yAxis: [{
lineWidth: 1,
gridLineWidth: 1,
title: null,
tickWidth: 1,
tickLength: 5,
tickPosition: 'inside',
labels: {
align: 'left',
x: 8
}
}, {
opposite: true,
linkedTo: 0,
lineWidth: 1,
gridLineWidth: 0,
title: null,
tickWidth: 1,
tickLength: 5,
tickPosition: 'inside',
labels: {
align: 'right',
x: -8
}
}],
legend: {
enabled: false
},
plotOptions: {
area: {
fillOpacity: 0.2,
lineWidth: 1,
step: 'center'
}
},
tooltip: {
headerFormat: '<span style="font-size=10px;">Price: {point.key}</span><br/>',
valueDecimals: 2
},
series: [{
name: 'Bids',
data: bids, //HERE GOES THE DATA
color: '#03a7a8'
}, {
name: 'Asks',
data: asks, //HERE GOES THE DATA
color: '#fc5857'
}]
});
</script>
以上是关于highcharts如何实现动态刷新的主要内容,如果未能解决你的问题,请参考以下文章