Some documentation tweaks.

This commit is contained in:
Olly Smith 2012-09-15 23:05:48 +01:00
parent c1deb0d5ca
commit 4c045c30dd
4 changed files with 60 additions and 14 deletions

View File

@ -4,6 +4,7 @@ $ = jQuery
# Very simple event-emitter class. # Very simple event-emitter class.
# #
# @private
class Morris.EventEmitter class Morris.EventEmitter
on: (name, handler) -> on: (name, handler) ->
unless @handlers? unless @handlers?
@ -18,8 +19,9 @@ class Morris.EventEmitter
handler(args...) handler(args...)
# Make long numbers prettier by inserting commas. # Make long numbers prettier by inserting commas.
# eg: commas(1234567) -> '1,234,567'
# #
# @example
# Morris.commas(1234567) -> '1,234,567'
Morris.commas = (num) -> Morris.commas = (num) ->
if num is null if num is null
"n/a" "n/a"
@ -33,6 +35,8 @@ Morris.commas = (num) ->
ret += strabsnum.slice(intnum.length) ret += strabsnum.slice(intnum.length)
ret ret
# zero-pad numbers to two characters wide # Zero-pad numbers to two characters wide.
# #
# @example
# Morris.pad2(1) -> '01'
Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number

View File

@ -1,3 +1,13 @@
# Donut charts.
#
# @example
# Morris.Donut({
# el: $('#donut-container'),
# data: [
# { label: 'yin', value: 50 },
# { label: 'yang', value: 50 }
# ]
# });
class Morris.Donut class Morris.Donut
colors: [ colors: [
'#0B62A4' '#0B62A4'
@ -12,6 +22,8 @@ class Morris.Donut
'#042135' '#042135'
] ]
# Create and render a donut chart.
#
constructor: (options) -> constructor: (options) ->
if not (this instanceof Morris.Donut) if not (this instanceof Morris.Donut)
return new Morris.Donut(options) return new Morris.Donut(options)
@ -34,12 +46,17 @@ class Morris.Donut
@el.addClass 'graph-initialised' @el.addClass 'graph-initialised'
# the raphael drawing instance @redraw()
# Clear and redraw the chart.
#
# If you need to re-size your charts, call this method after changing the
# size of the container element.
redraw: ->
@el.clear()
@r = new Raphael(@el[0]) @r = new Raphael(@el[0])
@draw()
draw: ->
cx = @el.width() / 2 cx = @el.width() / 2
cy = @el.height() / 2 cy = @el.height() / 2
w = (Math.min(cx, cy) - 10) / 3 w = (Math.min(cx, cy) - 10) / 3
@ -71,11 +88,13 @@ class Morris.Donut
break break
idx += 1 idx += 1
# @private
select: (segment) => select: (segment) =>
s.deselect() for s in @segments s.deselect() for s in @segments
segment.select() segment.select()
@setLabels segment.data.label, Morris.commas(segment.data.value) @setLabels segment.data.label, Morris.commas(segment.data.value)
# @private
setLabels: (label1, label2) -> setLabels: (label1, label2) ->
inner = (Math.min(@el.width() / 2, @el.height() / 2) - 10) * 2 / 3 inner = (Math.min(@el.width() / 2, @el.height() / 2) - 10) * 2 / 3
maxWidth = 1.8 * inner maxWidth = 1.8 * inner
@ -90,6 +109,10 @@ class Morris.Donut
text2scale = Math.min(maxWidth / text2bbox.width, maxHeightBottom / text2bbox.height) text2scale = Math.min(maxWidth / text2bbox.width, maxHeightBottom / text2bbox.height)
@text2.attr(transform: "S#{text2scale},#{text2scale},#{text2bbox.x + text2bbox.width / 2},#{text2bbox.y}") @text2.attr(transform: "S#{text2scale},#{text2scale},#{text2bbox.x + text2bbox.width / 2},#{text2bbox.y}")
# A segment within a donut chart.
#
# @private
class Morris.DonutSegment extends Morris.EventEmitter class Morris.DonutSegment extends Morris.EventEmitter
constructor: (@cx, @cy, @inner, @outer, p0, p1, @color, @data) -> constructor: (@cx, @cy, @inner, @outer, p0, p1, @color, @data) ->
@sin_p0 = Math.sin(p0) @sin_p0 = Math.sin(p0)

View File

@ -1,7 +1,6 @@
class Morris.Line class Morris.Line
# Initialise the graph. # Initialise the graph.
# #
# @param {Object} options
constructor: (options) -> constructor: (options) ->
if not (this instanceof Morris.Line) if not (this instanceof Morris.Line)
return new Morris.Line(options) return new Morris.Line(options)
@ -92,7 +91,7 @@ class Morris.Line
xLabels: 'auto' xLabels: 'auto'
xLabelFormat: null xLabelFormat: null
# Pre-process data # Update the data series and redraw the chart.
# #
setData: (data, redraw = true) -> setData: (data, redraw = true) ->
# shallow copy & sort data # shallow copy & sort data
@ -168,6 +167,7 @@ class Morris.Line
# Do any size-related calculations # Do any size-related calculations
# #
# @private
calc: -> calc: ->
w = @el.width() w = @el.width()
h = @el.height() h = @el.height()
@ -201,17 +201,21 @@ class Morris.Line
# quick translation helpers # quick translation helpers
# #
# @private
transX: (x) => transX: (x) =>
if @xvals.length is 1 if @xvals.length is 1
@left + @width / 2 @left + @width / 2
else else
@left + (x - @xmin) * @dx @left + (x - @xmin) * @dx
# @private
transY: (y) => transY: (y) =>
return @options.marginTop + @height - (y - @ymin) * @dy return @options.marginTop + @height - (y - @ymin) * @dy
# Clear and redraw the graph # Clear and redraw the chart.
# #
# If you need to re-size your charts, call this method after changing the
# size of the container element.
redraw: -> redraw: ->
@r.clear() @r.clear()
@calc() @calc()
@ -222,6 +226,7 @@ class Morris.Line
# draw the grid, and axes labels # draw the grid, and axes labels
# #
# @private
drawGrid: -> drawGrid: ->
# draw y axis labels, horizontal lines # draw y axis labels, horizontal lines
firstY = @ymin firstY = @ymin
@ -271,6 +276,7 @@ class Morris.Line
# draw the data series # draw the data series
# #
# @private
drawSeries: -> drawSeries: ->
for i in [@seriesCoords.length-1..0] for i in [@seriesCoords.length-1..0]
coords = $.map(@seriesCoords[i], (c) -> c) coords = $.map(@seriesCoords[i], (c) -> c)
@ -293,6 +299,7 @@ class Morris.Line
# create a path for a data series # create a path for a data series
# #
# @private
createPath: (coords, top, left, bottom, right) -> createPath: (coords, top, left, bottom, right) ->
path = "" path = ""
if @options.smooth if @options.smooth
@ -317,6 +324,7 @@ class Morris.Line
# calculate a gradient at each point for a series of points # calculate a gradient at each point for a series of points
# #
# @private
gradients: (coords) -> gradients: (coords) ->
$.map coords, (c, i) -> $.map coords, (c, i) ->
if i is 0 if i is 0
@ -328,6 +336,7 @@ class Morris.Line
# draw the hover tooltip # draw the hover tooltip
# #
# @private
drawHover: -> drawHover: ->
# hover labels # hover labels
@hoverHeight = @options.hoverFontSize * 1.5 * (@series.length + 1) @hoverHeight = @options.hoverFontSize * 1.5 * (@series.length + 1)
@ -351,6 +360,7 @@ class Morris.Line
@yLabels.push(yLabel) @yLabels.push(yLabel)
@hoverSet.push(yLabel) @hoverSet.push(yLabel)
# @private
updateHover: (index) => updateHover: (index) =>
@hoverSet.show() @hoverSet.show()
@xLabel.attr('text', @columnLabels[index]) @xLabel.attr('text', @columnLabels[index])
@ -375,9 +385,11 @@ class Morris.Line
xloc = Math.max @left + maxLabelWidth / 2 + @options.hoverPaddingX, xloc xloc = Math.max @left + maxLabelWidth / 2 + @options.hoverPaddingX, xloc
@hoverSet.attr 'transform', "t#{xloc},#{yloc}" @hoverSet.attr 'transform', "t#{xloc},#{yloc}"
# @private
hideHover: -> hideHover: ->
@hoverSet.hide() @hoverSet.hide()
# @private
hilight: (index) => hilight: (index) =>
if @prevHilight isnt null and @prevHilight isnt index if @prevHilight isnt null and @prevHilight isnt index
for i in [0..@seriesPoints.length-1] for i in [0..@seriesPoints.length-1]
@ -392,6 +404,7 @@ class Morris.Line
if index is null if index is null
@hideHover() @hideHover()
# @private
updateHilight: (x) => updateHilight: (x) =>
x -= @el.offset().left x -= @el.offset().left
for hoverIndex in [@hoverMargins.length..0] for hoverIndex in [@hoverMargins.length..0]
@ -399,17 +412,20 @@ class Morris.Line
@hilight hoverIndex @hilight hoverIndex
break break
# @private
measureText: (text, fontSize = 12) -> measureText: (text, fontSize = 12) ->
tt = @r.text(100, 100, text).attr('font-size', fontSize) tt = @r.text(100, 100, text).attr('font-size', fontSize)
ret = tt.getBBox() ret = tt.getBBox()
tt.remove() tt.remove()
return ret return ret
# @private
yLabelFormat: (label) -> yLabelFormat: (label) ->
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}" "#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
# parse a date into a javascript timestamp # Parse a date into a javascript timestamp
#
# #
Morris.parseDate = (date) -> Morris.parseDate = (date) ->
if typeof date is 'number' if typeof date is 'number'
@ -497,6 +513,7 @@ Morris.parseDate = (date) ->
# generate a series of label, timestamp pairs for x-axis labels # generate a series of label, timestamp pairs for x-axis labels
# #
# @private
Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) -> Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) ->
ddensity = 200 * (dmax - dmin) / pxwidth # seconds per `margin` pixels ddensity = 200 * (dmax - dmin) / pxwidth # seconds per `margin` pixels
d0 = new Date(dmin) d0 = new Date(dmin)
@ -523,12 +540,14 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) ->
spec.incr(d) spec.incr(d)
return ret return ret
# @private
minutesSpecHelper = (interval) -> minutesSpecHelper = (interval) ->
span: interval * 60 * 1000 span: interval * 60 * 1000
start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours()) start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours())
fmt: (d) -> "#{Morris.pad2(d.getHours())}:#{Morris.pad2(d.getMinutes())}" fmt: (d) -> "#{Morris.pad2(d.getHours())}:#{Morris.pad2(d.getMinutes())}"
incr: (d) -> d.setMinutes(d.getMinutes() + interval) incr: (d) -> d.setMinutes(d.getMinutes() + interval)
# @private
secondsSpecHelper = (interval) -> secondsSpecHelper = (interval) ->
span: interval * 1000 span: interval * 1000
start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes()) start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes())

2
morris.min.js vendored

File diff suppressed because one or more lines are too long