{"id":26366,"date":"2015-10-16T15:22:50","date_gmt":"2015-10-16T09:52:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=26366"},"modified":"2015-10-16T15:33:20","modified_gmt":"2015-10-16T10:03:20","slug":"adjust-time-scale-representation-on-x-axis","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/adjust-time-scale-representation-on-x-axis\/","title":{"rendered":"Adjust time scale representation on axis &#8211; D3js"},"content":{"rendered":"<p>In d3.js we use <strong><a href=\"https:\/\/github.com\/mbostock\/d3\/wiki\/Time-Scales\">Time Scale<\/a><\/strong> on an axis, generally x-axis, to represent time in charts. To simplify manipulation of iteration over time intervals, D3 provides a handful of utilities in addition to time scale and time format.<br \/>\n<strong>d3.time.scale()<\/strong> creates a time scale with some domain and range values, the time scale also provides suitable ticks based on time intervals.<\/p>\n<p>The following time intervals are considered for automatic ticks:<\/p>\n<ul>\n<li>1-, 5-, 15- and 30-second.<\/li>\n<li>1-, 5-, 15- and 30-minute.<\/li>\n<li>1-, 3-, 6- and 12-hour.<\/li>\n<li>1- and 2-day.<\/li>\n<li>1-week.<\/li>\n<li>1- and 3-month.<\/li>\n<li>1-year.<\/li>\n<\/ul>\n<p>Usually, we use:<\/p>\n<p>[javascript]<br \/>\nvar x = d3.time.scale()<br \/>\n.domain([dates])<br \/>\n.range([values]);<br \/>\n[\/javascript]<\/p>\n<p>If <em>dates<\/em> is specified, <strong>scale.domain([dates])<\/strong> sets the scale&#8217;s input domain to the specified array of dates. The array must contain two or more dates.<br \/>\nIf <em>values<\/em> is specified, <strong>scale.range([values])<\/strong> sets the scale&#8217;s output range to the specified array of values. The array must contain two or more values.<br \/>\nFor more details refer <a href=\"https:\/\/github.com\/mbostock\/d3\/wiki\/Time-Scales#scale\" target=\"_blank\">Time Scales<\/a>.<\/p>\n<p>Defining your axis:<\/p>\n<p>[java]<br \/>\nvar xAxis = d3.svg.axis().scale(x).orient(&quot;bottom&quot;).ticks(12);<br \/>\n[\/java]<\/p>\n<p>This would give us this default time intervals provided by <strong>d3.time.scale<\/strong>, the ticks will always include prior mentioned automatic ticks.<\/p>\n<p>Recently, I was working on a project which required charts showing Time-based Statistics. We wanted to have 2-days aggregated data, hence my requirement was to have a tick only after 2-days interval. It works well, until there is a change in month. This <a href=\"http:\/\/jsfiddle.net\/2g3et5gn\/11\/\">JSFiddle<\/a> shows the problem. Notice the dates when month changes from March to April.<\/p>\n<p>If you want to avoid auto-generated ticks, then you would need to change the implementation of <strong>d3.time.scale<\/strong>. To do so, all you need is a custom function which will override former&#8217;s existing definition with the following signature:<\/p>\n<p>[java]function customTickFunc (t0, t1, step)<br \/>\n{}<br \/>\n[\/java]<\/p>\n<p>where:<br \/>\n<strong>t0<\/strong> and <strong>t1<\/strong> are start and end of dates on domain.<br \/>\n<strong>step<\/strong> is optional step count.<\/p>\n<p>Here is my function&#8217;s implementation<\/p>\n<p>[java]<br \/>\nfunction customTickFunc (t0, t1, step)<br \/>\n{<br \/>\n    var startTime = new Date(t0),<br \/>\n        endTime= new Date(t1), times = [];<br \/>\n    endTime.setUTCDate(endTime.getUTCDate() + 1);<br \/>\n    while (startTime &lt; endTime) {<br \/>\n      startTime.setUTCDate(startTime.getUTCDate() + 2);<br \/>\n      times.push(new Date(startTime));<br \/>\n    }<br \/>\n    return times;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now when we do<\/p>\n<p>[java]<br \/>\nvar xAxis = d3.svg.axis().scale(x).orient(&quot;bottom&quot;).ticks(customTickFunc);<br \/>\n[\/java]<\/p>\n<p>The above will create the required ticks, overriding default tick behaviour. Here is the <a href=\"http:\/\/jsfiddle.net\/315v9d07\/3\/\">JSFiddle<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In d3.js we use Time Scale on an axis, generally x-axis, to represent time in charts. To simplify manipulation of iteration over time intervals, D3 provides a handful of utilities in addition to time scale and time format. d3.time.scale() creates a time scale with some domain and range values, the time scale also provides suitable [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":20},"categories":[1],"tags":[2588],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26366"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=26366"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26366\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=26366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=26366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=26366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}