Skip to content

Commit

Permalink
Merge pull request #73 from codeforamerica/user-chart-labels
Browse files Browse the repository at this point in the history
Chart Labels
  • Loading branch information
milafrerichs committed Sep 9, 2015
2 parents 8b78a7c + d5851f4 commit 3a747f4
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function(config) {
'public/javascripts/helpers/helper.js',
'public/javascripts/helpers/templateHelper.js',
'public/javascripts/helpers/dataHelper.js',
'public/javascripts/helpers/raphaelHelper.js',
'public/javascripts/landing-pages.js',
'public/javascripts/search.js',
'public/javascripts/traffic.js',
Expand Down
1 change: 1 addition & 0 deletions karma.saucelabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = function(config) {
'public/javascripts/vendor/morris.min.js',
'public/javascripts/helpers/helper.js',
'public/javascripts/helpers/templateHelper.js',
'public/javascripts/helpers/raphaelHelper.js',
'public/javascripts/helpers/dataHelper.js',
'public/javascripts/landing-pages.js',
'public/javascripts/search.js',
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ <h2 class="brick-heading">Most popular pages</h2>
<script src="javascripts/helpers/dataHelper.js"></script>
<script src="javascripts/helpers/templateHelper.js"></script>
<script src="javascripts/helpers/helper.js"></script>
<script src="javascripts/helpers/raphaelHelper.js"></script>
<script src="javascripts/landing-pages.js"></script>
<script src="javascripts/search.js"></script>
<script src="javascripts/traffic.js"></script>
Expand Down
38 changes: 38 additions & 0 deletions public/javascripts/helpers/raphaelHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
window.raphaelHelper = function() {
var chart, options, elementHeight, data, yPos;
function raphael(chart_) {
chart = chart_;
options = chart.options;
data = chart.data;
yPos = chart.elementHeight - options.padding;
}
raphael.redrawAllLabels = function() {
var i, row, date,
dataLength = data.length;
for(i=0;i<dataLength;i++) {
row = data[dataLength - 1 - i];
date = row.src.date;
if(raphael.shouldDrawLabelForDate(date)) {
raphael.drawLabel(row._x, yPos, raphael.labelFormat(date));
}
}
}
raphael.labelFormat = function(date) {
var label = timeFormat.format("%-I %p")(date);
if(date.getHours() === 0) {
label = timeFormat.format("%a %-I %p")(date);
}
return label;
}
raphael.shouldDrawLabelForDate = function(date) {
return (date.getHours() % 6 === 0 && date.getMinutes() === 0);
}
raphael.drawLabel = function(x,y,text) {
chart.raphael.text(x, y, text)
.attr('font-size', options.gridTextSize)
.attr('font-family', options.gridTextFamily)
.attr('font-weight', options.gridTextWeight)
.attr('fill', options.gridTextColor)
}
return raphael;
}
6 changes: 4 additions & 2 deletions public/javascripts/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
stacked: true,
barColors: ["#265C8D", "#B26E00"],
hideHover: 'always',
xLabelMargin: 100,
xLabelFormat: function(data){
return timeFormat.format("%I %p")(data.label);
return "";
},
});
}
traffic.chart.setData(dataArray);
var chartHelper = window.raphaelHelper();
chartHelper(traffic.chart);
chartHelper.redrawAllLabels();
}
},
init: function(){
Expand Down
94 changes: 94 additions & 0 deletions tests/raphaelHelperSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
describe("raphaelHelper", function() {
beforeEach(function() {
sandbox = sinon.sandbox.create();
date = new Date();
options = { gridTextSize: 0, gridTextFamily: '', gridTextWeight: 0, padding: 0 };
data = [];
chart = { data: data, options: options, elementHeight: 0, raphael: {} };
subject = raphaelHelper();
});
afterEach(function() {
sandbox.restore();
});
describe("#labelFormat", function() {
it("returns a date in 12h format", function() {
date.setHours(10,0,0);
expect(subject.labelFormat(date)).to.eq("10 AM");
});
it("returns a date in 12h format without leading zero", function() {
date.setHours(8,0,0);
expect(subject.labelFormat(date)).to.eq("8 AM");
});
it("shows the weekday for 12AM", function() {
date.setHours(0,0,0);
currentDay = date.getDay();
date.setDate(date.getDate() + 3 - currentDay); //set Date to a wednesday
expect(subject.labelFormat(date)).to.eq("Wed 12 AM");
});
});
describe("shouldDrawLabelForDate", function() {
context("returns true", function() {
it("for 6AM", function() {
date.setHours(6,0,0);
expect(subject.shouldDrawLabelForDate(date)).to.eq(true);
});
it("for 6PM", function() {
date.setHours(18,0,0);
expect(subject.shouldDrawLabelForDate(date)).to.eq(true);
});
});
context("returns false", function() {
it("for 6:30 AM", function() {
date.setHours(6,30,0);
expect(subject.shouldDrawLabelForDate(date)).to.eq(false);
});
it("for 7AM", function() {
date.setHours(7,0,0);
expect(subject.shouldDrawLabelForDate(date)).to.eq(false);
});
});
});
describe("#drawLabel", function() {
beforeEach(function() {
attrSpy = sandbox.stub();
text = { attr: attrSpy }
attrSpy.returns(text);
textSpy = sandbox.stub().returns(text);
raphael = { text: textSpy }
chart.raphael = raphael;
subject(chart);
});
it("calls raphaels text method", function() {
subject.drawLabel(0,0,"");
expect(textSpy.withArgs(0,0,"")).to.have.been.calledOnce;
});
});
describe("#redrawAllLabels", function() {
context("no data", function() {
beforeEach(function() {
subject(chart);
});
it("does not redrawLabels", function() {
mock = sandbox.mock(subject).expects("drawLabel").never();
subject.redrawAllLabels();
mock.verify();
});
});
context("has data", function() {
beforeEach(function() {
date = new Date();
date.setHours(6,0,0);
differentDate = new Date();
differentDate.setHours(8,0,0);
data = [{src: { date: date}, _x: 0}, {src: { date: differentDate}, _x: 0}];
chart.data = data;
subject(chart);
});
it("does redrawLabels", function() {
mock = sandbox.mock(subject).expects("drawLabel").once();
subject.redrawAllLabels();
mock.verify();
});
});
});
});

0 comments on commit 3a747f4

Please sign in to comment.