Skip to content

Commit 7d540e5

Browse files
committed
Add bar chart for list of days
1 parent 9f13fd2 commit 7d540e5

File tree

6 files changed

+153
-14
lines changed

6 files changed

+153
-14
lines changed

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "Web Activity Time Tracker",
55
"short_name": "Web Time Tracker",
6-
"version": "1.0.2",
6+
"version": "1.0.3",
77
"minimum_chrome_version": "26",
88

99
"description": "Track and limit time your activity in the browser.",

src/scripts/chart/chart-core.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function donutChart() {
9191
})
9292
.style("font-size", 13)
9393
.attr("y", 10)
94-
.attr("x", 11);
94+
.attr("x", 13);
9595

9696
// ===========================================================================================
9797
// add tooltip to mouse events on slices and labels
@@ -168,7 +168,6 @@ function donutChart() {
168168
});
169169
}
170170

171-
// getter and setter functions. See Mike Bostocks post "Towards Reusable Charts" for a tutorial on how this works.
172171
chart.width = function (value) {
173172
if (!arguments.length) return width;
174173
width = value;
@@ -224,4 +223,57 @@ function donutChart() {
224223
};
225224

226225
return chart;
226+
}
227+
228+
function barChart(data) {
229+
var margin = { top: 5, right: 5, bottom: 25, left: 5 },
230+
width = 485,
231+
height = 160;
232+
233+
// set the ranges
234+
var x = d3.scaleBand()
235+
.range([0, width])
236+
.padding(0.1);
237+
var y = d3.scaleLinear()
238+
.range([height, 0]);
239+
240+
// append the svg object to the body of the page
241+
// append a 'group' element to 'svg'
242+
// moves the 'group' element to the top left margin
243+
var svg = d3.select("#barChart").append("svg")
244+
.attr("width", width + margin.left + margin.right)
245+
.attr("height", height + margin.top + margin.bottom)
246+
.append("g")
247+
.attr("transform",
248+
"translate(" + margin.left + "," + margin.top + ")");
249+
250+
var tip = d3.tip()
251+
.attr('class', 'd3-tip')
252+
.offset([-10, 0])
253+
.html(function (d) {
254+
return "<strong>" + convertShortSummaryTimeToLongString(d.total) + "</strong>";
255+
});
256+
257+
svg.call(tip);
258+
259+
// Scale the range of the data in the domains
260+
x.domain(data.map(function (d) { return d.date; }));
261+
y.domain([0, d3.max(data, function (d) { return d.total; })]);
262+
263+
// append the rectangles for the bar chart
264+
svg.selectAll(".bar")
265+
.data(data)
266+
.enter().append("rect")
267+
.attr("class", "bar")
268+
.attr("x", function (d) { return x(d.date); })
269+
.attr("width", x.bandwidth())
270+
.attr("y", function (d) { return y(d.total); })
271+
.attr("height", function (d) { return height - y(d.total); })
272+
.on('mouseover', tip.show)
273+
.on('mouseout', tip.hide);
274+
275+
// add the x Axis
276+
svg.append("g")
277+
.attr("transform", "translate(0," + height + ")")
278+
.call(d3.axisBottom(x));
227279
}

src/scripts/chart/d3.v4.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/ui.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class UI {
9898
ui.addHrAfterChart();
9999
}
100100

101+
drawBarChart(days) {
102+
d3.select('#barChart').datum(days);
103+
barChart(days);
104+
}
105+
101106
addTableHeader(currentTypeOfList, counterOfSite, totalDays) {
102107
var p = document.createElement('p');
103108
p.classList.add('table-header');
@@ -212,6 +217,8 @@ class UI {
212217

213218
addBlockForCalendar(range) {
214219
var div = document.getElementById('byDays');
220+
var barChart = document.createElement('div');
221+
barChart.id = 'barChart';
215222

216223
var from = document.createElement('span');
217224
from.innerHTML = 'From';
@@ -234,6 +241,7 @@ class UI {
234241
var tableForDaysBlock = document.createElement('div');
235242
tableForDaysBlock.id = 'tableForDaysBlock';
236243

244+
div.appendChild(barChart);
237245
div.appendChild(from);
238246
div.appendChild(calendarFirst);
239247
div.appendChild(to);
@@ -257,10 +265,14 @@ class UI {
257265
};
258266
}
259267

260-
fillListOfDays(days) {
268+
fillListOfDays(days, allDays) {
261269
var parent = document.getElementById('tableForDaysBlock');
262270
parent.innerHTML = null;
271+
document.getElementById('barChart').innerHTML = null;
263272
if (days.length > 0) {
273+
var daysForBarChart = this.fillDaysForBarChart(days, allDays);
274+
this.drawBarChart(daysForBarChart);
275+
264276
var header = document.createElement('div');
265277
header.classList.add('table-header');
266278

@@ -315,4 +327,23 @@ class UI {
315327
this.fillEmptyBlock('tableForDaysBlock');
316328
}
317329
}
330+
331+
fillDaysForBarChart(days, allDays) {
332+
var resultList = [];
333+
allDays.forEach(element => {
334+
var day = days.find(x => x.date == element);
335+
if (day !== undefined){
336+
resultList.push({
337+
'date': day.date,
338+
'total': day.total
339+
});
340+
}
341+
else resultList.push({
342+
'date': element,
343+
'total': 0
344+
});
345+
});
346+
347+
return resultList;
348+
}
318349
}

src/scripts/webact.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,30 @@ function getTabsByDays(tabs) {
266266
}
267267
if (range.from !== 'Invalid Date' && range.to !== 'Invalid Date') {
268268
var listOfDays = [];
269-
tabs.map(function (a) {
270-
return a.days.map(function (a) {
271-
var item = listOfDays.find(x => x.date == a.date);
269+
tabs.forEach(tab => {
270+
return tab.days.forEach(day => {
271+
var item = listOfDays.find(x => x.date == day.date);
272272
if (item !== undefined) {
273-
return item.total += a.summary;
273+
return item.total += day.summary;
274274
}
275-
if (item === undefined && isDateInRange(a.date, range))
275+
if (item === undefined && isDateInRange(day.date, range))
276276
return listOfDays.push({
277-
'date': a.date,
278-
'total': a.summary
277+
'date': day.date,
278+
'total': day.summary
279279
});
280280
});
281281
});
282282
listOfDays = listOfDays.sort(function (a, b) {
283283
return convertToDate(a.date) - convertToDate(b.date);
284284
});
285285

286-
ui.fillListOfDays(listOfDays);
286+
var getDaysArray = function(start, end) {
287+
for(var arr=[],dt=start; dt<=end; dt.setDate(dt.getDate()+1)){
288+
arr.push(dt.toLocaleDateString());
289+
}
290+
return arr;
291+
};
292+
ui.fillListOfDays(listOfDays, getDaysArray(getValueFromArrayRange(range.from), getValueFromArrayRange(range.to)));
287293
}
288294
else {
289295
ui.fillEmptyBlockForDaysIfInvalid();

src/style/chart.css

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,54 @@ tspan{
3636
.percentageValue{
3737
font-size: 1.8em !important;
3838
font-weight: 600 !important;
39-
}
39+
}
40+
41+
/*styles for chart of days*/
42+
.axis path,
43+
.axis line {
44+
fill: none;
45+
stroke: #000;
46+
shape-rendering: crispEdges;
47+
}
48+
49+
.bar {
50+
fill: orange;
51+
}
52+
53+
.bar:hover {
54+
fill: orangered ;
55+
}
56+
57+
.x.axis path {
58+
display: none;
59+
}
60+
61+
.d3-tip {
62+
font-size: 11px;
63+
line-height: 1;
64+
font-weight: bold;
65+
padding: 7px;
66+
background: rgba(0, 0, 0, 0.8);
67+
color: #fff;
68+
border-radius: 2px;
69+
}
70+
71+
/* Creates a small triangle extender for the tooltip */
72+
.d3-tip:after {
73+
box-sizing: border-box;
74+
display: inline;
75+
font-size: 10px;
76+
line-height: 1;
77+
color: rgba(0, 0, 0, 0.8);
78+
content: "\25BC";
79+
position: absolute;
80+
text-align: center;
81+
right:0;
82+
}
83+
84+
/* Style northward tooltips differently */
85+
.d3-tip.n:after {
86+
margin: -1px 0 0 0;
87+
top: 100%;
88+
left: 0;
89+
}

0 commit comments

Comments
 (0)