date - Javascript: Tabbed time period charts -


i'm trying produce graph following:

tabbed time line graph

from array of events follows:

var events = {      "0": {"guid": "78926349827546", "created": "2017-07-07 14:14:21" },     "1": {"guid": "78926349827546", "created": "2017-07-08 15:44:10" },     "2": {"guid": "20936752065745", "created": "2017-07-09 12:09:24" },     "3": {"guid": "20936752065745", "created": "2017-07-11 06:55:42" },     "4": {"guid": "20936752065745", "created": "2017-07-11 22:10:29" },     ... }; 

i'm using google line chart. although i'm happy aesthetic, still need find way produce tabbed display of several timescales, e.g. today, last 7 days, last month , total.

programmatically, proving sisyphean task, have count occurrences across (in 1 instance) every hour in last day, , (in instance) every day in last week etc.

and there's lot of date conversion, counting backwards today , on.

is there way of taking array , producing new array of human-readable dates relative today, across several timescales?

this duplicate of couple of questions where can find documentation on formatting date in javascript?, how add months date in javascript? , add days javascript date. there plenty of existing examples work from.

also, google charts has own date formatter.

anyway, might use function takes start date, end date , increment , returns array of timestamps in particular format. formatting strings can use second function or google charts formatter.

a bare bones version little code, add logic forward or backward series takes few more lines.

// return date string in yyyy-mm-dd hh:mm:ss format  function formatdate(date) {    function z(n){return (n<10? '0':'') + n}    return date.getfullyear() + '-' +           z(date.getmonth() + 1) + '-' +           z(date.getdate()) + ' ' +           z(date.gethours()) + ':' +           z(date.getminutes()) + ':' +           z(date.getseconds());  }    // return date strings start date end date  // increment inc in hours  function getdateseries(start, end, inc) {    var d = new date(+start);    inc = +inc;    var dates = [];        // deal backwards sequences    var reverse = false, t;    if (start > end) {      t = start;      start = end;      end = t;      reverse = true;    }    if (inc < 0) {      reverse = true;      inc *= -1;    }        while (start <= end) {      dates.push(formatdate(start));      start.sethours(start.gethours() + inc);    }    return reverse? dates.reverse() : dates;  }    // hourly intervals on 2 days forwards  console.log(getdateseries(new date(2017,7,18), new date(2017,7,19), 1));    // 6 hourly intervals on 10 days backwards  console.log(getdateseries(new date(2017,7,28), new date(2017,7,18), -6));    // hourly intervals going 24 hours  var = new date();  var end = new date(+now);  end.setdate(end.getdate() - 1);  console.log(getdateseries(now, end, -1))    // daily intervals today going 30 days  var = new date();  now.sethours(0,0,0,0);  var end = new date(+now);  end.setdate(end.getdate() - 30);  console.log(getdateseries(now, end, -24))

there plenty of libraries around formatting, incrementing , decrementing dates if want do, doesn't take write.

this modified start "now" or "today" , use interval in days rather start , end date hours.

where library come in handy if want monthly intervals on last day of month or similar (since months aren't of equal length). using moment.js do:

function getmonthlysequencestart(months) {    var format = 'yyyy-mm-dd hh:mm:ss'    var = moment().startof('month');    var count = math.abs(months);    var direction = months < 0? -1 : 1;    var result = [];    while (count--) {      result.push(now.format(format));      now.add(direction, 'months');    }    return result;  }    console.log(getmonthlysequencestart(-12));    function getmonthlysequenceend(months) {    var format = 'yyyy-mm-dd hh:mm:ss'    var = moment().endof('month').startof('day');    var count = math.abs(months);    var direction = months < 0? -1 : 1;    var result = [];    while (count--) {      result.push(now.format(format));      now.add(direction, 'months');      now.endof('month').startof('day');    }    return result;  }    console.log(getmonthlysequenceend(-12));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

not using library isn't hard either. following sets date first of month it's easy decrement 1 month, day before (the last day of previous month) string:

// sequence of end of months current month  // num months  function getmonthlysequenceend(num) {    var = new date();    now.sethours(0,0,0,0);    var t, result = [];    // set first day of next month    now.setmonth(now.getmonth() + 1, 1)    while (num--) {      t = new date(+now);      t.setdate(t.getdate() - 1);      result.push(formatdate(t));      now.setmonth(now.getmonth() - 1);    }    return result;  }    function formatdate(date) {    function z(n) {return (n < 10 ? '0' : '') + n}    return date.getfullyear() + '-' + z(date.getmonth() + 1) + '-' + z(date.getdate()) + ' ' +      z(date.gethours()) + ':' + z(date.getminutes()) + ':' + z(date.getseconds());  }    console.log(getmonthlysequenceend(24));

so think have enough whatever required.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -