flot display the date in flot based on timestamp

2019-01-18 05:56发布

Does anyone know how to display the date in flot based on timestamp

<script id="source" language="javascript" type="text/javascript">
$(function () {

var d1 = [
[1262818800,100],[1262732400,100],[1262646000,100],[1262559600,100],[1262473200,100],[1262386800,100],[1262300400,100],[1262214000,100],[1262127600,100],[1262041200,100],[1261954800,100],[1261868400,100],[1261782000,100],[1261695600,100],[1261609200,100],[1261522800,95],[1261436400,110],[1261350000,110],[1261263600,110],[1261177200,100];

var d2 = [
[1262818800,23],[1262732400,23],[1262646000,23],[1262559600,23],[1262473200,23],[1262386800,23],[1262300400,25],[1262214000,25],[1262127600,25],[1262041200,25],[1261954800,25],[1261868400,25],[1261782000,25],[1261695600,25],[1261609200,25],[1261522800,25],[1261436400,10],[1261350000,10],[1261263600,10],[1261177200,10]

$.plot($("#placeholder"), [{data:d1,lines:{show: true},label:"Mountain"},{data:d2,lines:{show: true},label:"Valley"}],{yaxis: {label:"cm"}},
{xaxis: {mode:"time"
}}
);

});
</script>

4条回答
何必那么认真
2楼-- · 2019-01-18 06:29

I just ran into this and I think we both used the same bad Flot example. The signature is:

var plot = $.plot(placeholder, data, options)

And your code is doing something like

var plot = $.plot(placeholder, data, xoptions, yoptions)

So to fix it, just do this instead:

$.plot(
    $("#placeholder"), 
    [{data:d1,lines:{show: true},label:"Mountain"},{data:d2,lines:{show:true},label:"Valley"}],
    {yaxis: {label:"cm"}, xaxis: {mode:"time"}}
);
查看更多
3楼-- · 2019-01-18 06:31

Try defining the 'timeformat' attribute, and define the pattern that flot will use to format the millisecond value.

xaxis:{
    mode: "time",
    timeformat: "%M:%S"
},
查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-18 06:35

I guess all you need to do is to multiply the timestamp (which look like unix timestamps) by 1000.

Unix timestamp tracks time as a running total of seconds starting from January 1st, 1970. While javascript timestamps measure milliseconds. So just multiply by 1000 and you should be fine

查看更多
Bombasti
5楼-- · 2019-01-18 06:37

I use this:

    var options = {
    lines: { show: true },
    points: { show: true },
    xaxis: { mode: "time",  timeformat: "%m/%d/%y",   minTickSize: [1, "day"]}
};
查看更多
登录 后发表回答