echarts折线图x轴动态刷新

半途流浪 2017-03-28 09:15:44
var base = +new Date(2014, 9, 3);
var oneDay = 24 * 3600 * 1000;
var date = [];

var data = [Math.random() * 150];
var now = new Date(base);

function addData(shift) {
now = [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/');
date.push(now);
data.push((Math.random() - 0.4) * 10 + data[data.length - 1]);

if (shift) {
date.shift();
data.shift();
}

now = new Date(+new Date(now) + oneDay);
}

for (var i = 1; i < 6; i++) {
addData();
}

option = {
xAxis: {
type: 'category',
boundaryGap: false,
data: date
},
yAxis: {
boundaryGap: [0, '50%'],
type: 'value'
},
series: [
{
name:'成交',
type:'line',
smooth:true,
symbol: 'none',
stack: 'a',
areaStyle: {
normal: {}
},
data: data
}
]
};

setInterval(function () {
addData(true);
myChart.setOption({
xAxis: {
data: date
},
series: [{
name:'成交',
data: data
}]
});
}, 1000);


想让x轴单位格式为:
年/月/日
小时:分钟:秒

但是我写出来小时分钟秒有问题,带秒的问题如下:
改分钟刷新

var base = +new Date(2014, 9, 3, 5, 12,10);
var oneMin = 1000;
var date = [];

var data = [Math.random() * 150];
var now = new Date(base);

function addData(shift) {
now = [now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()].join('/');
date.push(now);
data.push((Math.random() - 0.4) * 10 + data[data.length - 1]);

if (shift) {
date.shift();
data.shift();
}

now = new Date(+new Date(now) + oneMin);

}

for (var i = 1; i < 6; i++) {
addData();
}

option = {
xAxis: {
type: 'category',
boundaryGap: false,
data: date
},
yAxis: {
boundaryGap: [0, '50%'],
type: 'value'
},
series: [
{
name:'成交',
type:'line',
smooth:true,
symbol: 'none',
stack: 'a',
areaStyle: {
normal: {}
},
data: data
}
]
};

setInterval(function () {
addData(true);
myChart.setOption({
xAxis: {
data: date
},
series: [{
name:'成交',
data: data
}]
});
}, 1000);


谢谢。
...全文
1049 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
2017-03-28
  • 打赏
  • 举报
回复
说了不要覆盖now啊,你还是给now赋值了。
半途流浪 2017-03-28
  • 打赏
  • 举报
回复
嗯 自己解决了 需要的朋友参考吧
var base = +new Date(2014, 9, 3, 15, 12,10);
var oneMin =  1000;
var date = [];
 
var data = [Math.random() * 150];
var now = new Date(base);
 
function addData(shift) {
    now = [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/') + "\n" + [ now.getHours(), now.getMinutes(), now.getSeconds()].join(':');
    date.push(now);
    data.push((Math.random() - 0.4) * 10 + data[data.length - 1]);
 
    if (shift) {
        date.shift();
        data.shift();
    }
 
    now = new Date(+new Date(now) + oneMin);
     
}
 
for (var i = 1; i < 6; i++) {
    addData();
}
 
option = {
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: date
    },
    yAxis: {
        boundaryGap: [0, '50%'],
        type: 'value'
    },
    series: [
        {
            name:'成交',
            type:'line',
            smooth:true,
            symbol: 'none',
            stack: 'a',
            areaStyle: {
                normal: {}
            },
            data: data
        }
    ]
};
 
setInterval(function () {
    addData(true);
    myChart.setOption({
        xAxis: {
            data: date
        },
        series: [{
            name:'成交',
            data: data
        }]
    });
}, 1000);
半途流浪 2017-03-28
  • 打赏
  • 举报
回复
引用 1 楼 Free_Wind22 的回复:
addDate里不要覆盖now

    var tmp = [now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()].join('/');
    date.push(tmp);
老哥,我试了一下,push这个tmp编辑器内容有误。
var tmp = new Date(base);
var data = [Math.random() * 150];
var now = new Date(base);
 
function addData(shift) {
    now = [now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()].join('/');
    tmp = [now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()].join('/');
    date.push(tmp);
    data.push((Math.random() - 0.4) * 10 + data[data.length - 1]);
 
    if (shift) {
        date.shift();
        data.shift();
    }
 
    now = new Date(+new Date(now) + oneMin);
     
}
2017-03-28
  • 打赏
  • 举报
回复
addDate里不要覆盖now

    var tmp = [now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()].join('/');
    date.push(tmp);
半途流浪 2017-03-28
  • 打赏
  • 举报
回复
引用 4 楼 Free_Wind22 的回复:
说了不要覆盖now啊,你还是给now赋值了。
嗯嗯,我不赋值按你的思路试了也对。 我最后写的那种,赋值也可以。 这是为什么....

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧