Skip to content

Commit 38a1cdd

Browse files
author
tiraeth
committed
Added value goals and time events.
1 parent df9eb94 commit 38a1cdd

File tree

5 files changed

+177
-2
lines changed

5 files changed

+177
-2
lines changed

examples/events.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!doctype html>
2+
<head>
3+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
4+
<script src="https://raw.github.com/DmitryBaranovskiy/raphael/300aa589f5a0ba7fce667cd62c7cdda0bd5ad904/raphael-min.js"></script>
5+
<script src="../morris.js"></script>
6+
<script src="lib/prettify.js"></script>
7+
<script src="lib/example.js"></script>
8+
<link rel="stylesheet" href="lib/example.css">
9+
<link rel="stylesheet" href="lib/prettify.css">
10+
</head>
11+
<body>
12+
<h1>Time Events</h1>
13+
<div id="graph"></div>
14+
<pre id="code" class="prettyprint linenums">
15+
var week_data = [
16+
{"period": "2011 W27", "licensed": 3407, "sorned": 660},
17+
{"period": "2011 W26", "licensed": 3351, "sorned": 629},
18+
{"period": "2011 W25", "licensed": 3269, "sorned": 618},
19+
{"period": "2011 W24", "licensed": 3246, "sorned": 661},
20+
{"period": "2011 W23", "licensed": 3257, "sorned": 667},
21+
{"period": "2011 W22", "licensed": 3248, "sorned": 627},
22+
{"period": "2011 W21", "licensed": 3171, "sorned": 660},
23+
{"period": "2011 W20", "licensed": 3171, "sorned": 676},
24+
{"period": "2011 W19", "licensed": 3201, "sorned": 656},
25+
{"period": "2011 W18", "licensed": 3215, "sorned": 622},
26+
{"period": "2011 W17", "licensed": 3148, "sorned": 632},
27+
{"period": "2011 W16", "licensed": 3155, "sorned": 681},
28+
{"period": "2011 W15", "licensed": 3190, "sorned": 667},
29+
{"period": "2011 W14", "licensed": 3226, "sorned": 620},
30+
{"period": "2011 W13", "licensed": 3245, "sorned": null},
31+
{"period": "2011 W12", "licensed": 3289, "sorned": null},
32+
{"period": "2011 W11", "licensed": 3263, "sorned": null},
33+
{"period": "2011 W10", "licensed": 3189, "sorned": null},
34+
{"period": "2011 W09", "licensed": 3079, "sorned": null},
35+
{"period": "2011 W08", "licensed": 3085, "sorned": null},
36+
{"period": "2011 W07", "licensed": 3055, "sorned": null},
37+
{"period": "2011 W06", "licensed": 3063, "sorned": null},
38+
{"period": "2011 W05", "licensed": 2943, "sorned": null},
39+
{"period": "2011 W04", "licensed": 2806, "sorned": null},
40+
{"period": "2011 W03", "licensed": 2674, "sorned": null},
41+
{"period": "2011 W02", "licensed": 1702, "sorned": null},
42+
{"period": "2011 W01", "licensed": 1732, "sorned": null}
43+
];
44+
Morris.Line({
45+
element: 'graph',
46+
data: week_data,
47+
xkey: 'period',
48+
ykeys: ['licensed', 'sorned'],
49+
labels: ['Licensed', 'SORN'],
50+
events: [
51+
'2011-04',
52+
'2011-08'
53+
]
54+
});
55+
</pre>
56+
</body>

examples/goals.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<head>
3+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
4+
<script src="https://raw.github.com/DmitryBaranovskiy/raphael/300aa589f5a0ba7fce667cd62c7cdda0bd5ad904/raphael-min.js"></script>
5+
<script src="../morris.js"></script>
6+
<script src="lib/prettify.js"></script>
7+
<script src="lib/example.js"></script>
8+
<link rel="stylesheet" href="lib/example.css">
9+
<link rel="stylesheet" href="lib/prettify.css">
10+
</head>
11+
<body>
12+
<h1>Value Goals</h1>
13+
<div id="graph"></div>
14+
<pre id="code" class="prettyprint linenums">
15+
var decimal_data = [];
16+
for (var x = 0; x <= 360; x += 10) {
17+
decimal_data.push({
18+
x: x,
19+
y: Math.sin(Math.PI * x / 180).toFixed(4)
20+
});
21+
}
22+
window.m = Morris.Line({
23+
element: 'graph',
24+
data: decimal_data,
25+
xkey: 'x',
26+
ykeys: ['y'],
27+
labels: ['sin(x)'],
28+
parseTime: false,
29+
goals: [-1, 0, 1]
30+
});
31+
</pre>
32+
</body>

lib/morris.grid.coffee

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,33 @@ class Morris.Grid extends Morris.EventEmitter
5151
preUnits: ''
5252
ymax: 'auto'
5353
ymin: 'auto 0'
54+
goals: []
55+
goalStrokeWidth: 1.0
56+
goalLineColors: [
57+
'#666633'
58+
'#999966'
59+
'#cc6666'
60+
'#663333'
61+
]
62+
events: []
63+
eventStrokeWidth: 1.0
64+
eventLineColors: [
65+
'#005a04'
66+
'#ccffbb'
67+
'#3a5f0b'
68+
'#005502'
69+
]
5470

5571
# Update the data series and redraw the chart.
5672
#
5773
setData: (data, redraw = true) ->
5874
ymax = if @cumulative then 0 else null
5975
ymin = if @cumulative then 0 else null
76+
77+
if @options.goals.length > 0
78+
ymax = Math.max(ymax, Math.max.apply(null,@options.goals))
79+
ymin = Math.min(ymin, Math.min.apply(null,@options.goals))
80+
6081
@data = $.map data, (row, index) =>
6182
ret = {}
6283
ret.label = row[@options.xkey]
@@ -94,6 +115,13 @@ class Morris.Grid extends Morris.EventEmitter
94115
# calculate horizontal range of the graph
95116
@xmin = @data[0].x
96117
@xmax = @data[@data.length - 1].x
118+
119+
@events = []
120+
if @options.parseTime and @options.events.length > 0
121+
@events = $.map @options.events, (event, index) => Morris.parseDate(event)
122+
@xmax = Math.max(@xmax, Math.max.apply(null,@events))
123+
@xmin = Math.min(@xmin, Math.min.apply(null,@events))
124+
97125
if @xmin is @xmax
98126
@xmin -= 1
99127
@xmax += 1
@@ -174,8 +202,25 @@ class Morris.Grid extends Morris.EventEmitter
174202
@r.clear()
175203
@_calc()
176204
@drawGrid()
205+
@drawGoals()
206+
@drawEvents()
177207
@draw() if @draw
178208

209+
# draw goals horizontal lines
210+
#
211+
drawGoals: ->
212+
for goal, i in @options.goals
213+
@r.path("M#{@left},#{@transY(goal)}H#{@left + @width}")
214+
.attr('stroke', @options.goalLineColors[i % @options.goalLineColors.length])
215+
.attr('stroke-width', @options.goalStrokeWidth)
216+
217+
# draw events vertical lines
218+
drawEvents: ->
219+
for event, i in @events
220+
@r.path("M#{@transX(event)},#{@bottom}V#{@top}")
221+
.attr('stroke', @options.eventLineColors[i % @options.eventLineColors.length])
222+
.attr('stroke-width', @options.eventStrokeWidth)
223+
179224
# draw y axis labels, horizontal lines
180225
#
181226
drawGrid: ->

morris.js

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

morris.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.

0 commit comments

Comments
 (0)