Skip to content

Commit

Permalink
Compute the maxYLabelWith using the precision.
Browse files Browse the repository at this point in the history
As I introduced the precision, the maxYLabelWith is not the width of the raw
value but the width of the formatted one.

As the computing the of maxYLabelWidth is done in calc(), I moved yInterval in
it because I need it to compute the precision. Also I think it makes more sense,
because the calc() method is supposed to "do any size-related calculations".
  • Loading branch information
pelletier committed May 14, 2012
1 parent dbc114e commit 7def73f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
25 changes: 14 additions & 11 deletions morris.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,19 @@ class Morris.Line
calc: ->
w = @el.width()
h = @el.height()

@yInterval = (@options.ymax - @options.ymin) / (@options.numLines - 1)

if (@yInterval < 1)
@precision = -parseInt(@yInterval.toExponential().split('e')[1])
else
@precision = 0

if @elementWidth != w or @elementHeight != h
# calculate grid dimensions
@maxYLabelWidth = Math.max(
@measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width,
@measureText(@yLabelFormat(@options.ymax), @options.gridTextSize).width)
@measureText(@yLabelFormat(@options.ymin.toFixed(@precision)), @options.gridTextSize).width,
@measureText(@yLabelFormat(@options.ymax.toFixed(@precision)), @options.gridTextSize).width)
@left = @maxYLabelWidth + @options.marginLeft
@width = @el.width() - @left - @options.marginRight
@height = @el.height() - @options.marginTop - @options.marginBottom
Expand Down Expand Up @@ -203,19 +211,14 @@ class Morris.Line
#
drawGrid: ->
# draw y axis labels, horizontal lines
yInterval = (@options.ymax - @options.ymin) / (@options.numLines - 1)
firstY = (@options.ymin / yInterval) * yInterval
lastY = (@options.ymax / yInterval) * yInterval
firstY = @options.ymin
lastY = @options.ymax

if (yInterval < 1)
precision = -parseInt(yInterval.toExponential().split('e')[1])
else
precision = 0

for lineY in [firstY..lastY] by yInterval
for lineY in [firstY..lastY] by @yInterval
v = lineY
y = @transY(v)
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v.toFixed(precision)))
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v.toFixed(@precision)))
.attr('font-size', @options.gridTextSize)
.attr('fill', @options.gridTextColor)
.attr('text-anchor', 'end')
Expand Down
32 changes: 16 additions & 16 deletions morris.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7def73f

Please sign in to comment.