-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from codeforamerica/user-chart-labels
Chart Labels
- Loading branch information
Showing
6 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |