Skip to content

Commit 691bd8d

Browse files
committed
Merge pull request #120 from dynaum/master
Adding support to stacked bars
2 parents 25698d6 + 5c5e882 commit 691bd8d

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

examples/staked_bars.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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>Stacked Bars chart</h1>
13+
<div id="graph"></div>
14+
<pre id="code" class="prettyprint linenums">
15+
// Use Morris.Bar
16+
Morris.Bar({
17+
element: 'graph',
18+
data: [
19+
{x: '2011 Q1', y: 3, z: 2, a: 3},
20+
{x: '2011 Q2', y: 2, z: null, a: 1},
21+
{x: '2011 Q3', y: 0, z: 2, a: 4},
22+
{x: '2011 Q4', y: 2, z: 4, a: 3}
23+
],
24+
xkey: 'x',
25+
ykeys: ['y', 'z', 'a'],
26+
labels: ['Y', 'Z', 'A'],
27+
stacked: true
28+
});
29+
</pre>
30+
</body>

lib/morris.bar.coffee

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Morris.Bar extends Morris.Grid
88
# setup event handlers
99
#
1010
init: ->
11+
@cumulative = @options.stacked
1112
@prevHilight = null
1213
@el.mousemove (evt) =>
1314
@updateHilight evt.pageX
@@ -105,11 +106,12 @@ class Morris.Bar extends Morris.Grid
105106
# @private
106107
drawSeries: ->
107108
groupWidth = @width / @options.data.length
108-
numBars = @options.ykeys.length
109+
numBars = if @options.stacked? then 1 else @options.ykeys.length
109110
barWidth = (groupWidth * @options.barSizeRatio - @options.barGap * (numBars - 1)) / numBars
110111
leftPadding = groupWidth * (1 - @options.barSizeRatio) / 2
111112
zeroPos = if @ymin <= 0 and @ymax >= 0 then @transY(0) else null
112113
@bars = for row, idx in @data
114+
lastTop = 0
113115
for ypos, sidx in row._y
114116
if ypos != null
115117
if zeroPos
@@ -118,10 +120,17 @@ class Morris.Bar extends Morris.Grid
118120
else
119121
top = ypos
120122
bottom = @bottom
121-
left = @left + idx * groupWidth + leftPadding + sidx * (barWidth + @options.barGap)
122-
@r.rect(left, top, barWidth, bottom - top)
123+
124+
left = @left + idx * groupWidth + leftPadding
125+
left += sidx * (barWidth + @options.barGap) unless @options.stacked
126+
size = bottom - top
127+
128+
top -= lastTop if @options.stacked
129+
@r.rect(left, top, barWidth, size)
123130
.attr('fill', @colorFor(row, sidx, 'bar'))
124131
.attr('stroke-width', 0)
132+
133+
lastTop += size
125134
else
126135
null
127136

morris.js

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)