Created
August 30, 2011 06:51
-
-
Save drewlesueur/1180345 to your computer and use it in GitHub Desktop.
mood-graph
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> | |
<!--[if IE]><script src="excanvas.js"></script><![endif]--> | |
<script src="mood_chart.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or 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
WeekChart = (options={}) -> | |
{padding, width, height, days, maxMood, maxActivities} = options | |
maxMood or= 10 | |
maxActivities or= 10 | |
padding ||= 10 | |
width = width - padding * 2 | |
height = height - padding * 2 | |
cellWidth = width / 5 | |
cellHeight = height | |
days ||= [ | |
mood: 1, | |
activities: 3 | |
, | |
mood: 2 | |
activities: 5 | |
, | |
mood: 5 | |
activities: 5 | |
, | |
mood: 6 | |
activities: 5 | |
, | |
mood: 10 | |
activities: 6 | |
] | |
self= {} | |
canvasEl = document.createElement 'canvas' | |
canvas = $(canvasEl) | |
canvas.attr "width", width + padding * 2 | |
canvas.attr "height", height + padding * 2 | |
canvas.css | |
border: "1px solid black" | |
if not canvasEl.getContext | |
G_vmlCanvasManager.initElement(canvasEl) | |
ctx = canvasEl.getContext '2d' | |
drawSquare = (x, y) -> | |
squareSide = 12 | |
ctx.beginPath() | |
ctx.fillStyle = "#007000" | |
ctx.fillRect x - squareSide / 2, y - squareSide / 2, squareSide, squareSide | |
drawCircle = (x, y) -> | |
radius = 4.5 | |
ctx.beginPath() | |
ctx.fillStyle = "#2DA2D4" | |
ctx.arc x, y, radius, 0, Math.PI*2, true | |
ctx.fill() | |
calcPosition = (index, value, maxValue) -> | |
x = (cellWidth * index) + cellWidth / 2 | |
y = (value / maxValue) * cellHeight | |
[x + padding, cellHeight - (y - padding)] | |
drawActivity = (index) -> | |
day = days[index] | |
if not day then return | |
activity = day.activities or null | |
if activity isnt null | |
[x, y] = calcPosition index, activity, maxActivities | |
drawSquare x, y | |
drawMood = (index) -> | |
day = days[index] | |
if not day then return | |
mood = day.mood or null | |
if mood isnt null | |
[x, y] = calcPosition index, mood, maxMood | |
drawCircle x, y | |
drawActivities = () -> | |
for day, index in days | |
drawActivity index | |
drawMoods = () -> | |
for day, index in days | |
drawMood index | |
drawLine = (prop, index) -> | |
if prop == "mood" | |
maxValue = maxMood | |
color = "#2DA2D4" | |
else | |
maxValue = maxActivities | |
color = "#007000" | |
value = days[index][prop] | |
value2 = days[index + 1][prop] | |
[x, y] = calcPosition index, value, maxValue | |
[x2, y2] = calcPosition index + 1, value2, maxValue | |
ctx.strokeStyle = color | |
ctx.lineWidth = 3 | |
ctx.beginPath() | |
ctx.moveTo x, y | |
ctx.lineTo x2, y2 | |
ctx.stroke() | |
drawFlatLine = (prop, index) -> | |
if prop == "mood" | |
maxValue = maxMood | |
color = "#2DA2D4" | |
else | |
maxValue = maxActivities | |
color = "#007000" | |
value = days[index][prop] | |
[x, y] = calcPosition index, value, maxValue | |
lineRadius = cellWidth / 4 | |
[x0, y0] = [x - lineRadius, y] | |
[x1, y1] = [x + lineRadius, y] | |
ctx.strokeStyle = color | |
ctx.lineWidth = 3 | |
ctx.beginPath() | |
ctx.moveTo x0, y0 | |
ctx.lineTo x1, y1 | |
ctx.stroke() | |
#x x x x x | |
drawLinesMaker = (prop) -> | |
() -> | |
firstDay = days[0] | |
secondDay = days[1] | |
if firstDay and secondDay | |
drawLine prop, 0 | |
else if firstDay and not secondDay | |
drawFlatLine prop, 0 | |
for index in [1..days.length - 1] | |
prevDay = days[index - 1] | |
day = days[index] | |
nextDay = days[index + 1] | |
if day and nextDay | |
drawLine prop, index | |
else if day and not(prevDay) and not(nextDay) | |
drawFlatLine prop, index | |
lastDay = days[days.length - 1] | |
secondLastDay = days[days.length - 2] | |
if lastDay and not secondLastDay | |
drawFlatLine prop, days.length - 1 | |
drawMoodLines = drawLinesMaker "mood" | |
drawActivityLines = drawLinesMaker "activities" | |
getEl = () -> | |
canvas | |
self.getEl = getEl | |
drawActivities() | |
drawActivityLines() | |
drawMoods() | |
drawMoodLines() | |
self | |
return | |
$ -> | |
weekChart = WeekChart | |
width: 800 | |
height: 150 | |
weekChart2 = WeekChart | |
width: 800 | |
height: 150 | |
days: [ | |
mood: 1, | |
activities: 3 | |
, | |
null | |
, | |
mood: 10 | |
activities: 5 | |
, | |
mood: 2 | |
activities: 10 | |
, | |
mood: 10 | |
activities: 3 | |
] | |
weekChart3 = WeekChart | |
width: 800 | |
height: 150 | |
days: [ | |
mood: 1, | |
activities: 3 | |
, | |
null | |
, | |
mood: 10 | |
activities: 5 | |
, | |
null | |
, | |
mood: 10 | |
activities: 3 | |
] | |
$(document.body).append weekChart.getEl() | |
$(document.body).append weekChart2.getEl() | |
$(document.body).append weekChart3.getEl() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment