Skip to content

Commit 5e240e6

Browse files
committed
WIP: interval improvements
1 parent 7a9f523 commit 5e240e6

File tree

5 files changed

+113
-26
lines changed

5 files changed

+113
-26
lines changed

lib/morris.grid.coffee

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,32 @@ class Morris.Grid extends Morris.EventEmitter
160160
@ymin -= 1 if ymin
161161
@ymax += 1
162162

163-
@yInterval = (@ymax - @ymin) / (@options.numLines - 1)
164-
if @yInterval > 0 and @yInterval < 1
165-
@precision = -Math.floor(Math.log(@yInterval) / Math.log(10))
166-
else
167-
@precision = 0
163+
if @options.axes is true or @options.grid is true
164+
# calculate grid placement
165+
span = @ymax - @ymin
166+
ymag = Math.floor(Math.log(span) / Math.log(10))
167+
unit = Math.pow(10, ymag)
168+
169+
# calculate initial grid min and max values
170+
gmin = Math.round(@ymin / unit) * unit
171+
gmax = Math.round(@ymax / unit) * unit
172+
step = (gmax - gmin) / (@options.numLines - 1)
173+
174+
# ensure zero is plotted where the range includes zero
175+
if gmin < 0 and gmax > 0
176+
gmin = Math.round(@ymin / step) * step
177+
gmax = Math.round(@ymax / step) * step
178+
179+
# special case for decimal numbers
180+
if step < 1
181+
smag = Math.floor(Math.log(step) / Math.log(10))
182+
@grid = for y in [gmin..gmax] by step
183+
parseFloat(y.toFixed(1 - smag))
184+
else
185+
@grid = (y for y in [gmin..gmax] by step)
186+
187+
@ymin = gmin
188+
@ymax = gmax
168189

169190
@dirty = true
170191
@redraw() if redraw
@@ -259,13 +280,10 @@ class Morris.Grid extends Morris.EventEmitter
259280
#
260281
drawGrid: ->
261282
return if @options.grid is false and @options.axes is false
262-
firstY = @ymin
263-
lastY = @ymax
264-
for lineY in [firstY..lastY] by @yInterval
265-
v = parseFloat(lineY.toFixed(@precision))
266-
y = @transY(v)
283+
for lineY in @grid
284+
y = @transY(lineY)
267285
if @options.axes
268-
@drawYAxisLabel(@left - @options.padding / 2, y, @yAxisFormat(v))
286+
@drawYAxisLabel(@left - @options.padding / 2, y, @yAxisFormat(lineY))
269287
if @options.grid
270288
@drawGridLine("M#{@left},#{y}H#{@left + @width}")
271289

morris.js

Lines changed: 40 additions & 13 deletions
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.

spec/lib/grid/interval_spec.coffee

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
gridLines = (ymin, ymax, nlines = 5) ->
2+
interval = ymax - ymin
3+
omag = Math.floor(Math.log(interval) / Math.log(10))
4+
unit = Math.pow(10, omag)
5+
6+
ymin1 = Math.round(ymin / unit) * unit
7+
ymax1 = Math.round(ymax / unit) * unit
8+
step = (ymax1 - ymin1) / (nlines - 1)
9+
10+
# ensure zero is plotted where the range includes zero
11+
if ymin1 < 0 and ymax1 > 0
12+
ymin1 = Math.round(ymin / step) * step
13+
ymax1 = Math.round(ymax / step) * step
14+
15+
# small numbers
16+
if step < 1
17+
smag = Math.floor(Math.log(step) / Math.log(10))
18+
(parseFloat(y.toFixed(1 - smag)) for y in [ymin1..ymax1] by step)
19+
else
20+
(y for y in [ymin1..ymax1] by step)
21+
22+
describe 'Morris.Grid#gridLines', ->
23+
24+
it 'should draw at fixed intervals', ->
25+
gridLines(0, 4).should.deep.equal [0, 1, 2, 3, 4]
26+
gridLines(0, 400).should.deep.equal [0, 100, 200, 300, 400]
27+
28+
it 'should pick intervals that show significant numbers', ->
29+
gridLines(98, 502).should.deep.equal [100, 200, 300, 400, 500]
30+
gridLines(98, 302).should.deep.equal [100, 150, 200, 250, 300]
31+
gridLines(98, 202).should.deep.equal [100, 125, 150, 175, 200]
32+
33+
it 'should draw zero when it falls within [ymin..ymax]', ->
34+
gridLines(-100, 300).should.deep.equal [-100, 0, 100, 200, 300]
35+
gridLines(-50, 350).should.deep.equal [0, 100, 200, 300, 400]
36+
gridLines(-500, 300).should.deep.equal [-400, -200, 0, 200, 400]
37+
gridLines(100, 500).should.deep.equal [100, 200, 300, 400, 500]
38+
gridLines(-500, -100).should.deep.equal [-500, -400, -300, -200, -100]
39+
40+
it 'should generate decimal labels to 2 signigicant figures', ->
41+
gridLines(0, 1).should.deep.equal [0, 0.25, 0.5, 0.75, 1]
42+
gridLines(0.1, 0.5).should.deep.equal [0.1, 0.2, 0.3, 0.4, 0.5]

spec/lib/grid/set_data_spec.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe 'Morris.Grid#setData', ->
1313

1414
describe 'ymin/ymax', ->
1515
beforeEach ->
16-
@defaults =
16+
@defaults =
1717
element: 'graph'
1818
xkey: 'x'
1919
ykeys: ['y', 'z']

0 commit comments

Comments
 (0)