在添加新数据时删除json数组中的元素? [英] Remove element in json array as new data added?
问题描述
我有一个折线图,随着从MySQL数据库中提取新数据,折线图每5秒更新一次.
I have a line graph that is being updated every 5 seconds as new data is pulled from a mySQL database.
https://gist.github.com/Majella/5fc4cd5f41a6ddf2df23
每次调用数据以停止压缩行/路径时,如何从数组中删除第一个/最旧的元素?
How do I remove the first/oldest element from the array each time the data is called to stop the line/path being compressed?
我试图在调用数据后立即在update函数中添加data.shift(),但仅适用于第一次调用吗?
I've tried adding data.shift() in the update function just after the data is called but only works for the first call?
推荐答案
我不知道getdata.php背后的详细信息,但我认为每次每次返回的数据点都在逐渐增加,因此仅删除了第一个数据点.仍然可以为您提供比所需更大的数据集.因此,您有两种选择:
I don't know the details of what lives behind getdata.php, but I assume it's returning progressively more data points each time, thus removing only the first one still lives you with a larger data set than you want. So you have a couple choices:
-
更改getdata.php的服务器端,使其仅返回最新的x个数据点(或添加一个querystring参数,以获取多少点/分钟/要检索的内容)
Change the server-side of getdata.php to return only the latest x data points (or maybe add a querystring parameter for how many points/minutes/whatever to retrieve)
在updateData中更改客户端以检查数组的长度,并从lengthYouWant减去lengthYouReceived(假设数据已经正确排序)开始对元素进行切片.
Change the client-side in updateData to check the length of the array and .slice off the elements starting at lengthYouWant minus lengthYouReceived (assuming the data is already sorted correctly)
这篇关于在添加新数据时删除json数组中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!