8 var timeseries =
function(spaced, data, enableBrush) {
9 classd = spaced.replace(
new RegExp(
" "),
".");
10 render(classd, spaced, data, enableBrush);
17 function lessThanDay(d) {
18 return (d ===
"hours" || d ===
"minutes" || d ===
"seconds") ?
true :
false;
26 return date.valueOf();
34 return date.valueOf();
41 function timeRangePad(dates) {
42 var minDate, maxDate, pad;
43 if (dates.length > 1) {
44 minDate = moment(_.min(dates));
45 maxDate = moment(_.max(dates));
46 pad = getDatePadding(minDate, maxDate);
47 minDate.subtract(1, pad);
50 minDate = moment(dates[0]).subtract(1,
'hour');
51 maxDate = moment(dates[0]).add(1,
'hour');
60 function getDatePadding(minDate, maxDate) {
61 if (maxDate.diff(minDate,
'years') > 0)
63 else if (maxDate.diff(minDate,
'months') > 0)
65 else if (maxDate.diff(minDate,
'days') > 0)
67 else if (maxDate.diff(minDate,
'hours') > 0)
69 else if (maxDate.diff(minDate,
'minutes') > 0)
79 function render(classd, spaced, data, enableBrush) {
81 var padding = timeRangePad(_.pluck(data,
'value'));
89 var width = window.innerWidth - 150;
90 var height = (lessThanDay(padding.pad)) ? (100 - margin.top - margin.bottom) : (300 - margin.top - margin.bottom);
92 var x = d3.time.scale().range([0 + margin.right, width - margin.left]),
94 .range([margin.top, height - margin.bottom - margin.top]);
96 var ticks = width > 800 ? 8 : 4;
98 x.domain(d3.extent([padding.minDate, padding.maxDate]));
100 var xFormat, yFormat;
101 if (lessThanDay(padding.pad)) {
103 yFormat =
"%m/%d/%y";
104 y.domain(d3.extent([padding.minDate]));
106 xFormat =
"%m/%d/%y";
108 var start =
new Date(2012, 0, 1, 0, 0, 0, 0).getTime();
109 var stop =
new Date(2012, 0, 1, 23, 59, 59, 59).getTime();
110 y.domain(d3.extent([start, stop]));
113 var xAxis = d3.svg.axis().scale(x).orient(
"bottom")
115 .tickSize(-height, 0)
116 .tickFormat(d3.time.format(xFormat));
118 var yAxis = d3.svg.axis().scale(y).orient(
"left")
120 .tickSize(-width + margin.right, margin.left)
121 .tickFormat(d3.time.format(yFormat));
123 var svg = d3.select(
"." + classd).append(
"svg")
124 .attr(
"width", width + margin.left + margin.right)
125 .attr(
"height", height + margin.top + margin.bottom);
127 var context = svg.append(
"g")
128 .attr(
"class",
"context")
129 .attr(
"transform",
"translate(" + margin.left +
"," + margin.top +
")");
132 .attr(
"class",
"x axis")
133 .attr(
"transform",
"translate(" + margin.left +
"," + (margin.top + (height - margin.bottom)) +
")")
137 .attr(
"class",
"y axis")
138 .attr(
"transform",
"translate(" + margin.left +
"," + margin.top +
")")
141 var circles = context.append(
"g")
142 .attr(
"transform",
"translate(" + margin.left +
"," + margin.top +
")")
144 circles.selectAll(
".circ")
146 .enter().append(
"circle")
147 .attr(
"class",
"circ")
148 .attr(
"cx",
function(d) {
149 return (lessThanDay(padding.pad)) ? x(d.value) : x(getDate(d.value));
151 .attr(
"cy",
function(d, i) {
152 return (lessThanDay(padding.pad)) ? y(getDate(d.value)) : y(getTime(d.value));
155 .on(
"click",
function(d) {
156 console.log(
new Date(d.value));
162 var brush = d3.svg.brush()
164 .on(
"brush", _.throttle(brushed, 200));
167 .attr(
"class",
"brush")
171 .attr(
"height", height - margin.bottom);
173 var brushEl =
'<div class="brush-control"><div class="brush-info"><i>Click and drag on the timeseries to create a brush.</i></div><button class="clear-brush">Clear brush</button></div>';
174 window.document.getElementsByClassName(spaced)[0].insertAdjacentHTML(
'beforeend', brushEl);
177 if (!brush.empty()) {
178 d3.select(
'.clear-brush').style(
"display",
"inline-block");
179 d3.select(
'.brush-info')[0][0].innerText = brush.extent();
183 d3.select(
'.clear-brush').on(
"click",
function(d) {
184 if (!brush.empty()) {
185 d3.selectAll(
"g.brush").call(brush.clear());
186 d3.select(
'.brush-info')[0][0].innerText =
"";
187 d3.select(
'.clear-brush').style(
"display",
"none");
191 timeseries.getBrushExtent =
function() {
193 return brush.extent();
201 d3.selectAll(
".circ")
203 .style(
"opacity",
function(d) {
204 return d.selected ? 1 : 0.6;
206 .attr(
"r",
function(d) {
207 return d.selected ? 15 : 7;
211 if (typeof define ===
"function" && define.amd) define(timeseries);
212 else if (typeof module ===
"object" && module.exports) module.exports = timeseries;
213 this.timeseries = timeseries;