2012-02-23 09:11:31 +01:00
|
|
|
# The original line graph.
|
|
|
|
#
|
2012-03-01 09:06:38 +01:00
|
|
|
$ = jQuery
|
|
|
|
|
2012-02-26 01:28:58 +01:00
|
|
|
Morris = {}
|
|
|
|
class Morris.Line
|
2012-02-23 09:11:31 +01:00
|
|
|
# Initialise the graph.
|
|
|
|
#
|
|
|
|
# @param {Object} options
|
2012-02-26 01:28:58 +01:00
|
|
|
constructor: (options) ->
|
|
|
|
if not (this instanceof Morris.Line)
|
|
|
|
return new Morris.Line(options)
|
2012-02-28 23:00:08 +01:00
|
|
|
if typeof options.element is 'string'
|
|
|
|
@el = $ document.getElementById(options.element)
|
|
|
|
else
|
|
|
|
@el = $ options.element
|
2012-05-11 19:25:29 +02:00
|
|
|
|
|
|
|
if @el == null || @el.length == 0
|
|
|
|
throw new Error("Graph placeholder not found.")
|
|
|
|
|
2012-02-28 22:06:55 +01:00
|
|
|
@options = $.extend {}, @defaults, options
|
2012-04-05 21:04:19 +02:00
|
|
|
# backwards compatibility for units -> postUnits
|
|
|
|
if typeof @options.units is 'string'
|
|
|
|
@options.postUnits = options.units
|
2012-02-23 09:11:31 +01:00
|
|
|
# bail if there's no data
|
|
|
|
if @options.data is undefined or @options.data.length is 0
|
|
|
|
return
|
|
|
|
@el.addClass 'graph-initialised'
|
2012-06-10 19:28:36 +02:00
|
|
|
|
|
|
|
# the raphael drawing instance
|
|
|
|
@r = new Raphael(@el[0])
|
|
|
|
|
|
|
|
# Some instance variables for later
|
|
|
|
@pointGrow = Raphael.animation r: @options.pointSize + 3, 25, 'linear'
|
|
|
|
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
|
|
|
|
@elementWidth = null
|
|
|
|
@elementHeight = null
|
|
|
|
@dirty = false
|
|
|
|
# column hilight events
|
|
|
|
@prevHilight = null
|
|
|
|
@el.mousemove (evt) =>
|
|
|
|
@updateHilight evt.pageX
|
|
|
|
if @options.hideHover
|
|
|
|
@el.mouseout (evt) =>
|
|
|
|
@hilight null
|
|
|
|
touchHandler = (evt) =>
|
|
|
|
touch = evt.originalEvent.touches[0] or evt.originalEvent.changedTouches[0]
|
|
|
|
@updateHilight touch.pageX
|
|
|
|
return touch
|
|
|
|
@el.bind 'touchstart', touchHandler
|
|
|
|
@el.bind 'touchmove', touchHandler
|
|
|
|
@el.bind 'touchend', touchHandler
|
|
|
|
|
2012-07-03 08:34:42 +02:00
|
|
|
@seriesLabels = @options.labels
|
2012-06-10 19:28:36 +02:00
|
|
|
@setData(@options.data)
|
2012-02-23 09:11:31 +01:00
|
|
|
|
|
|
|
# Default configuration
|
|
|
|
#
|
|
|
|
defaults:
|
|
|
|
lineWidth: 3
|
|
|
|
pointSize: 4
|
|
|
|
lineColors: [
|
|
|
|
'#0b62a4'
|
|
|
|
'#7A92A3'
|
|
|
|
'#4da74d'
|
|
|
|
'#afd8f8'
|
|
|
|
'#edc240'
|
|
|
|
'#cb4b4b'
|
|
|
|
'#9440ed'
|
|
|
|
]
|
2012-02-28 00:23:43 +01:00
|
|
|
ymax: 'auto'
|
2012-03-08 21:03:45 +01:00
|
|
|
ymin: 'auto 0'
|
2012-02-23 09:11:31 +01:00
|
|
|
marginTop: 25
|
|
|
|
marginRight: 25
|
|
|
|
marginBottom: 30
|
|
|
|
marginLeft: 25
|
|
|
|
numLines: 5
|
|
|
|
gridLineColor: '#aaa'
|
|
|
|
gridTextColor: '#888'
|
|
|
|
gridTextSize: 12
|
|
|
|
gridStrokeWidth: 0.5
|
2012-02-23 22:59:45 +01:00
|
|
|
hoverPaddingX: 10
|
|
|
|
hoverPaddingY: 5
|
|
|
|
hoverMargin: 10
|
|
|
|
hoverFillColor: '#fff'
|
|
|
|
hoverBorderColor: '#ccc'
|
|
|
|
hoverBorderWidth: 2
|
|
|
|
hoverOpacity: 0.95
|
|
|
|
hoverLabelColor: '#444'
|
|
|
|
hoverFontSize: 12
|
2012-02-28 22:27:33 +01:00
|
|
|
smooth: true
|
2012-03-06 22:38:01 +01:00
|
|
|
hideHover: false
|
2012-03-06 23:05:00 +01:00
|
|
|
parseTime: true
|
2012-04-05 21:04:19 +02:00
|
|
|
preUnits: ''
|
|
|
|
postUnits: ''
|
2012-03-15 22:44:55 +01:00
|
|
|
dateFormat: (x) -> new Date(x).toString()
|
2012-04-02 22:27:45 +02:00
|
|
|
xLabels: 'auto'
|
2012-04-04 21:32:24 +02:00
|
|
|
xLabelFormat: null
|
2012-02-23 09:11:31 +01:00
|
|
|
|
2012-06-10 19:28:36 +02:00
|
|
|
# Pre-process data
|
2012-02-23 09:11:31 +01:00
|
|
|
#
|
2012-06-10 19:28:36 +02:00
|
|
|
setData: (data, redraw = true) ->
|
2012-05-14 08:37:45 +02:00
|
|
|
# shallow copy & sort data
|
2012-06-10 19:28:36 +02:00
|
|
|
@options.data = data.slice(0)
|
2012-02-29 21:53:53 +01:00
|
|
|
@options.data.sort (a, b) => (a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey])
|
2012-02-23 09:11:31 +01:00
|
|
|
# extract labels
|
2012-02-23 22:59:45 +01:00
|
|
|
@columnLabels = $.map @options.data, (d) => d[@options.xkey]
|
2012-02-23 09:11:31 +01:00
|
|
|
|
|
|
|
# extract series data
|
|
|
|
@series = []
|
|
|
|
for ykey in @options.ykeys
|
2012-03-11 08:42:04 +01:00
|
|
|
series_data = []
|
|
|
|
for d in @options.data
|
2012-06-08 22:37:27 +02:00
|
|
|
series_data.push switch typeof d[ykey]
|
|
|
|
when 'number' then d[ykey]
|
|
|
|
when 'string' then parseFloat(d[ykey])
|
|
|
|
else null
|
2012-03-11 08:42:04 +01:00
|
|
|
@series.push(series_data)
|
2012-02-23 09:11:31 +01:00
|
|
|
|
|
|
|
# translate x labels into nominal dates
|
|
|
|
# note: currently using decimal years to specify dates
|
2012-03-06 23:05:00 +01:00
|
|
|
if @options.parseTime
|
2012-04-01 23:29:37 +02:00
|
|
|
@xvals = $.map @columnLabels, (x) -> Morris.parseDate x
|
2012-03-06 23:05:00 +01:00
|
|
|
else
|
|
|
|
@xvals = [(@columnLabels.length-1)..0]
|
2012-03-15 22:44:55 +01:00
|
|
|
# translate column labels, if they're timestamps
|
2012-03-18 20:50:40 +01:00
|
|
|
if @options.parseTime
|
|
|
|
@columnLabels = $.map @columnLabels, (d) =>
|
|
|
|
if typeof d is 'number'
|
|
|
|
@options.dateFormat(d)
|
|
|
|
else
|
|
|
|
d
|
2012-02-23 09:11:31 +01:00
|
|
|
@xmin = Math.min.apply null, @xvals
|
|
|
|
@xmax = Math.max.apply null, @xvals
|
|
|
|
if @xmin is @xmax
|
|
|
|
@xmin -= 1
|
|
|
|
@xmax += 1
|
|
|
|
|
2012-02-28 00:23:43 +01:00
|
|
|
# Compute the vertical range of the graph if desired
|
2012-02-28 09:20:11 +01:00
|
|
|
if typeof @options.ymax is 'string' and @options.ymax[0..3] is 'auto'
|
2012-03-08 21:03:45 +01:00
|
|
|
# use Array.concat to flatten arrays and find the max y value
|
|
|
|
ymax = Math.max.apply null, Array.prototype.concat.apply([], @series)
|
|
|
|
if @options.ymax.length > 5
|
2012-07-03 08:34:42 +02:00
|
|
|
@ymax = Math.max parseInt(@options.ymax[5..], 10), ymax
|
2012-03-08 21:03:45 +01:00
|
|
|
else
|
2012-07-03 08:34:42 +02:00
|
|
|
@ymax = ymax
|
2012-08-17 14:23:59 +02:00
|
|
|
else if typeof @options.ymax is 'string'
|
|
|
|
@ymax = parseInt(@options.ymax, 10)
|
|
|
|
else
|
|
|
|
@ymax = @options.ymax
|
2012-03-08 21:03:45 +01:00
|
|
|
if typeof @options.ymin is 'string' and @options.ymin[0..3] is 'auto'
|
|
|
|
ymin = Math.min.apply null, Array.prototype.concat.apply([], @series)
|
|
|
|
if @options.ymin.length > 5
|
2012-07-03 08:34:42 +02:00
|
|
|
@ymin = Math.min parseInt(@options.ymin[5..], 10), ymin
|
2012-03-08 21:03:45 +01:00
|
|
|
else
|
2012-07-03 08:34:42 +02:00
|
|
|
@ymin = ymin
|
2012-08-17 14:23:59 +02:00
|
|
|
else if typeof @options.ymin is 'string'
|
|
|
|
@ymin = parseInt(@options.ymin, 10)
|
|
|
|
else
|
|
|
|
@ymin = @options.ymin
|
2012-07-03 08:34:42 +02:00
|
|
|
if @ymin is @ymax
|
|
|
|
@ymin -= 1
|
|
|
|
@ymax += 1
|
2012-04-02 22:27:45 +02:00
|
|
|
|
2012-07-03 08:34:42 +02:00
|
|
|
@yInterval = (@ymax - @ymin) / (@options.numLines - 1)
|
2012-06-08 22:47:43 +02:00
|
|
|
if @yInterval > 0 and @yInterval < 1
|
|
|
|
@precision = -Math.floor(Math.log(@yInterval) / Math.log(10))
|
|
|
|
else
|
|
|
|
@precision = 0
|
|
|
|
|
2012-06-10 19:28:36 +02:00
|
|
|
@dirty = true
|
|
|
|
@redraw() if redraw
|
2012-03-18 20:10:20 +01:00
|
|
|
|
|
|
|
# Do any size-related calculations
|
|
|
|
#
|
|
|
|
calc: ->
|
|
|
|
w = @el.width()
|
|
|
|
h = @el.height()
|
2012-05-14 12:03:12 +02:00
|
|
|
|
2012-06-10 19:28:36 +02:00
|
|
|
if @elementWidth != w or @elementHeight != h or @dirty
|
|
|
|
@elementWidth = w
|
|
|
|
@elementHeight = h
|
|
|
|
@dirty = false
|
2012-03-18 20:10:20 +01:00
|
|
|
# calculate grid dimensions
|
|
|
|
@maxYLabelWidth = Math.max(
|
2012-07-03 08:34:42 +02:00
|
|
|
@measureText(@yLabelFormat(@ymin), @options.gridTextSize).width,
|
|
|
|
@measureText(@yLabelFormat(@ymax), @options.gridTextSize).width)
|
2012-03-18 20:10:20 +01:00
|
|
|
@left = @maxYLabelWidth + @options.marginLeft
|
|
|
|
@width = @el.width() - @left - @options.marginRight
|
|
|
|
@height = @el.height() - @options.marginTop - @options.marginBottom
|
|
|
|
@dx = @width / (@xmax - @xmin)
|
2012-07-03 08:34:42 +02:00
|
|
|
@dy = @height / (@ymax - @ymin)
|
2012-03-18 20:10:20 +01:00
|
|
|
# calculate series data point coordinates
|
|
|
|
@columns = (@transX(x) for x in @xvals)
|
|
|
|
@seriesCoords = []
|
|
|
|
for s in @series
|
|
|
|
scoords = []
|
|
|
|
$.each s, (i, y) =>
|
|
|
|
if y == null
|
|
|
|
scoords.push(null)
|
|
|
|
else
|
|
|
|
scoords.push(x: @columns[i], y: @transY(y))
|
|
|
|
@seriesCoords.push(scoords)
|
|
|
|
# calculate hover margins
|
2012-04-02 22:27:45 +02:00
|
|
|
@hoverMargins = $.map @columns.slice(1), (x, i) => (x + @columns[i]) / 2
|
|
|
|
|
2012-03-18 20:10:20 +01:00
|
|
|
# quick translation helpers
|
|
|
|
#
|
|
|
|
transX: (x) =>
|
|
|
|
if @xvals.length is 1
|
|
|
|
@left + @width / 2
|
|
|
|
else
|
|
|
|
@left + (x - @xmin) * @dx
|
|
|
|
|
|
|
|
transY: (y) =>
|
2012-07-03 08:34:42 +02:00
|
|
|
return @options.marginTop + @height - (y - @ymin) * @dy
|
2012-02-23 09:11:31 +01:00
|
|
|
|
|
|
|
# Clear and redraw the graph
|
|
|
|
#
|
|
|
|
redraw: ->
|
2012-06-10 19:28:36 +02:00
|
|
|
@r.clear()
|
2012-03-18 20:10:20 +01:00
|
|
|
@calc()
|
|
|
|
@drawGrid()
|
|
|
|
@drawSeries()
|
|
|
|
@drawHover()
|
|
|
|
@hilight(if @options.hideHover then null else 0)
|
2012-02-23 09:11:31 +01:00
|
|
|
|
2012-03-18 20:10:20 +01:00
|
|
|
# draw the grid, and axes labels
|
|
|
|
#
|
|
|
|
drawGrid: ->
|
2012-02-23 09:11:31 +01:00
|
|
|
# draw y axis labels, horizontal lines
|
2012-07-03 08:34:42 +02:00
|
|
|
firstY = @ymin
|
|
|
|
lastY = @ymax
|
2012-05-14 11:52:47 +02:00
|
|
|
|
|
|
|
|
2012-05-14 12:03:12 +02:00
|
|
|
for lineY in [firstY..lastY] by @yInterval
|
2012-06-08 22:47:43 +02:00
|
|
|
v = parseFloat(lineY.toFixed(@precision))
|
2012-03-18 20:10:20 +01:00
|
|
|
y = @transY(v)
|
2012-04-05 21:04:19 +02:00
|
|
|
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v))
|
2012-02-23 09:11:31 +01:00
|
|
|
.attr('font-size', @options.gridTextSize)
|
|
|
|
.attr('fill', @options.gridTextColor)
|
|
|
|
.attr('text-anchor', 'end')
|
2012-03-18 20:10:20 +01:00
|
|
|
@r.path("M#{@left},#{y}H#{@left + @width}")
|
2012-02-23 09:11:31 +01:00
|
|
|
.attr('stroke', @options.gridLineColor)
|
|
|
|
.attr('stroke-width', @options.gridStrokeWidth)
|
|
|
|
|
2012-03-15 21:47:15 +01:00
|
|
|
## draw x axis labels
|
2012-04-01 23:29:37 +02:00
|
|
|
ypos = @options.marginTop + @height + @options.marginBottom / 2
|
2012-03-15 22:06:21 +01:00
|
|
|
xLabelMargin = 50 # make this an option?
|
2012-04-01 23:29:37 +02:00
|
|
|
prevLabelMargin = null
|
|
|
|
drawLabel = (labelText, xpos) =>
|
|
|
|
label = @r.text(@transX(xpos), ypos, labelText)
|
2012-03-15 22:06:21 +01:00
|
|
|
.attr('font-size', @options.gridTextSize)
|
|
|
|
.attr('fill', @options.gridTextColor)
|
|
|
|
labelBox = label.getBBox()
|
2012-06-08 23:39:06 +02:00
|
|
|
# ensure a minimum of `xLabelMargin` pixels between labels, and ensure
|
|
|
|
# labels don't overflow the container
|
|
|
|
if (prevLabelMargin is null or prevLabelMargin <= labelBox.x) and
|
|
|
|
labelBox.x >= 0 and (labelBox.x + labelBox.width) < @el.width()
|
2012-03-15 22:06:21 +01:00
|
|
|
prevLabelMargin = labelBox.x + labelBox.width + xLabelMargin
|
|
|
|
else
|
|
|
|
label.remove()
|
2012-04-01 23:29:37 +02:00
|
|
|
if @options.parseTime
|
2012-04-02 22:27:45 +02:00
|
|
|
if @columnLabels.length == 1 and @options.xLabels == 'auto'
|
|
|
|
# where there's only one value in the series, we can't make a
|
|
|
|
# sensible guess for an x labelling scheme, so just use the original
|
|
|
|
# column label
|
|
|
|
drawLabel(@columnLabels[0], @xvals[0])
|
|
|
|
else
|
2012-04-04 21:32:24 +02:00
|
|
|
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat)
|
2012-04-02 22:27:45 +02:00
|
|
|
drawLabel(l[0], l[1])
|
2012-04-01 23:29:37 +02:00
|
|
|
else
|
|
|
|
for i in [0..@columnLabels.length]
|
|
|
|
labelText = @columnLabels[@columnLabels.length - i - 1]
|
|
|
|
drawLabel(labelText, i)
|
2012-04-02 22:27:45 +02:00
|
|
|
|
2012-03-18 20:10:20 +01:00
|
|
|
# draw the data series
|
|
|
|
#
|
|
|
|
drawSeries: ->
|
|
|
|
for i in [@seriesCoords.length-1..0]
|
2012-06-18 21:04:29 +02:00
|
|
|
coords = $.map(@seriesCoords[i], (c) -> c)
|
2012-02-23 09:11:31 +01:00
|
|
|
if coords.length > 1
|
2012-03-18 20:10:20 +01:00
|
|
|
path = @createPath coords, @options.marginTop, @left, @options.marginTop + @height, @left + @width
|
2012-02-23 09:11:31 +01:00
|
|
|
@r.path(path)
|
|
|
|
.attr('stroke', @options.lineColors[i])
|
|
|
|
.attr('stroke-width', @options.lineWidth)
|
2012-03-18 20:10:20 +01:00
|
|
|
@seriesPoints = ([] for i in [0..@seriesCoords.length-1])
|
|
|
|
for i in [@seriesCoords.length-1..0]
|
|
|
|
for c in @seriesCoords[i]
|
2012-03-11 08:42:04 +01:00
|
|
|
if c == null
|
|
|
|
circle = null
|
|
|
|
else
|
|
|
|
circle = @r.circle(c.x, c.y, @options.pointSize)
|
|
|
|
.attr('fill', @options.lineColors[i])
|
|
|
|
.attr('stroke-width', 1)
|
|
|
|
.attr('stroke', '#ffffff')
|
2012-03-18 20:10:20 +01:00
|
|
|
@seriesPoints[i].push(circle)
|
2012-04-02 22:27:45 +02:00
|
|
|
|
2012-02-23 09:11:31 +01:00
|
|
|
# create a path for a data series
|
|
|
|
#
|
2012-06-18 21:04:29 +02:00
|
|
|
createPath: (coords, top, left, bottom, right) ->
|
2012-02-23 09:11:31 +01:00
|
|
|
path = ""
|
2012-02-28 22:27:33 +01:00
|
|
|
if @options.smooth
|
|
|
|
grads = @gradients coords
|
|
|
|
for i in [0..coords.length-1]
|
|
|
|
c = coords[i]
|
|
|
|
if i is 0
|
|
|
|
path += "M#{c.x},#{c.y}"
|
|
|
|
else
|
|
|
|
g = grads[i]
|
|
|
|
lc = coords[i - 1]
|
|
|
|
lg = grads[i - 1]
|
|
|
|
ix = (c.x - lc.x) / 4
|
|
|
|
x1 = lc.x + ix
|
|
|
|
y1 = Math.min(bottom, lc.y + ix * lg)
|
|
|
|
x2 = c.x - ix
|
|
|
|
y2 = Math.min(bottom, c.y - ix * g)
|
|
|
|
path += "C#{x1},#{y1},#{x2},#{y2},#{c.x},#{c.y}"
|
|
|
|
else
|
|
|
|
path = "M" + $.map(coords, (c) -> "#{c.x},#{c.y}").join("L")
|
2012-02-28 14:30:52 +01:00
|
|
|
return path
|
|
|
|
|
2012-02-23 09:11:31 +01:00
|
|
|
# calculate a gradient at each point for a series of points
|
|
|
|
#
|
|
|
|
gradients: (coords) ->
|
|
|
|
$.map coords, (c, i) ->
|
|
|
|
if i is 0
|
|
|
|
(coords[1].y - c.y) / (coords[1].x - c.x)
|
|
|
|
else if i is (coords.length - 1)
|
|
|
|
(c.y - coords[i - 1].y) / (c.x - coords[i - 1].x)
|
|
|
|
else
|
|
|
|
(coords[i + 1].y - coords[i - 1].y) / (coords[i + 1].x - coords[i - 1].x)
|
|
|
|
|
2012-03-18 20:10:20 +01:00
|
|
|
# draw the hover tooltip
|
|
|
|
#
|
|
|
|
drawHover: ->
|
|
|
|
# hover labels
|
|
|
|
@hoverHeight = @options.hoverFontSize * 1.5 * (@series.length + 1)
|
|
|
|
@hover = @r.rect(-10, -@hoverHeight / 2 - @options.hoverPaddingY, 20, @hoverHeight + @options.hoverPaddingY * 2, 10)
|
|
|
|
.attr('fill', @options.hoverFillColor)
|
|
|
|
.attr('stroke', @options.hoverBorderColor)
|
|
|
|
.attr('stroke-width', @options.hoverBorderWidth)
|
|
|
|
.attr('opacity', @options.hoverOpacity)
|
|
|
|
@xLabel = @r.text(0, (@options.hoverFontSize * 0.75) - @hoverHeight / 2, '')
|
|
|
|
.attr('fill', @options.hoverLabelColor)
|
|
|
|
.attr('font-weight', 'bold')
|
|
|
|
.attr('font-size', @options.hoverFontSize)
|
|
|
|
@hoverSet = @r.set()
|
|
|
|
@hoverSet.push(@hover)
|
|
|
|
@hoverSet.push(@xLabel)
|
|
|
|
@yLabels = []
|
|
|
|
for i in [0..@series.length-1]
|
|
|
|
yLabel = @r.text(0, @options.hoverFontSize * 1.5 * (i + 1.5) - @hoverHeight / 2, '')
|
|
|
|
.attr('fill', @options.lineColors[i])
|
|
|
|
.attr('font-size', @options.hoverFontSize)
|
|
|
|
@yLabels.push(yLabel)
|
|
|
|
@hoverSet.push(yLabel)
|
|
|
|
|
|
|
|
updateHover: (index) =>
|
|
|
|
@hoverSet.show()
|
|
|
|
@xLabel.attr('text', @columnLabels[index])
|
|
|
|
for i in [0..@series.length-1]
|
2012-04-05 21:04:19 +02:00
|
|
|
@yLabels[i].attr('text', "#{@seriesLabels[i]}: #{@yLabelFormat(@series[i][index])}")
|
2012-03-18 20:10:20 +01:00
|
|
|
# recalculate hover box width
|
|
|
|
maxLabelWidth = Math.max.apply null, $.map @yLabels, (l) ->
|
|
|
|
l.getBBox().width
|
|
|
|
maxLabelWidth = Math.max maxLabelWidth, @xLabel.getBBox().width
|
|
|
|
@hover.attr 'width', maxLabelWidth + @options.hoverPaddingX * 2
|
|
|
|
@hover.attr 'x', -@options.hoverPaddingX - maxLabelWidth / 2
|
|
|
|
# move to y pos
|
|
|
|
yloc = Math.min.apply null, $.map @series, (s) =>
|
|
|
|
@transY s[index]
|
|
|
|
if yloc > @hoverHeight + @options.hoverPaddingY * 2 + @options.hoverMargin + @options.marginTop
|
|
|
|
yloc = yloc - @hoverHeight / 2 - @options.hoverPaddingY - @options.hoverMargin
|
|
|
|
else
|
|
|
|
yloc = yloc + @hoverHeight / 2 + @options.hoverPaddingY + @options.hoverMargin
|
|
|
|
yloc = Math.max @options.marginTop + @hoverHeight / 2 + @options.hoverPaddingY, yloc
|
|
|
|
yloc = Math.min @options.marginTop + @height - @hoverHeight / 2 - @options.hoverPaddingY, yloc
|
|
|
|
xloc = Math.min @left + @width - maxLabelWidth / 2 - @options.hoverPaddingX, @columns[index]
|
|
|
|
xloc = Math.max @left + maxLabelWidth / 2 + @options.hoverPaddingX, xloc
|
|
|
|
@hoverSet.attr 'transform', "t#{xloc},#{yloc}"
|
2012-04-02 22:27:45 +02:00
|
|
|
|
2012-03-18 20:10:20 +01:00
|
|
|
hideHover: ->
|
|
|
|
@hoverSet.hide()
|
|
|
|
|
|
|
|
hilight: (index) =>
|
|
|
|
if @prevHilight isnt null and @prevHilight isnt index
|
|
|
|
for i in [0..@seriesPoints.length-1]
|
|
|
|
if @seriesPoints[i][@prevHilight]
|
|
|
|
@seriesPoints[i][@prevHilight].animate @pointShrink
|
|
|
|
if index isnt null and @prevHilight isnt index
|
|
|
|
for i in [0..@seriesPoints.length-1]
|
|
|
|
if @seriesPoints[i][index]
|
|
|
|
@seriesPoints[i][index].animate @pointGrow
|
|
|
|
@updateHover index
|
|
|
|
@prevHilight = index
|
|
|
|
if index is null
|
|
|
|
@hideHover()
|
|
|
|
|
|
|
|
updateHilight: (x) =>
|
|
|
|
x -= @el.offset().left
|
|
|
|
for hoverIndex in [@hoverMargins.length..0]
|
|
|
|
if hoverIndex == 0 || @hoverMargins[hoverIndex - 1] > x
|
|
|
|
@hilight hoverIndex
|
|
|
|
break
|
2012-04-02 22:27:45 +02:00
|
|
|
|
2012-02-23 09:11:31 +01:00
|
|
|
measureText: (text, fontSize = 12) ->
|
|
|
|
tt = @r.text(100, 100, text).attr('font-size', fontSize)
|
|
|
|
ret = tt.getBBox()
|
|
|
|
tt.remove()
|
|
|
|
return ret
|
|
|
|
|
2012-04-05 21:04:19 +02:00
|
|
|
yLabelFormat: (label) ->
|
2012-06-08 22:47:43 +02:00
|
|
|
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
|
2012-04-05 21:04:19 +02:00
|
|
|
|
2012-04-01 23:29:37 +02:00
|
|
|
# parse a date into a javascript timestamp
|
|
|
|
#
|
|
|
|
Morris.parseDate = (date) ->
|
|
|
|
if typeof date is 'number'
|
|
|
|
return date
|
|
|
|
m = date.match /^(\d+) Q(\d)$/
|
|
|
|
n = date.match /^(\d+)-(\d+)$/
|
|
|
|
o = date.match /^(\d+)-(\d+)-(\d+)$/
|
|
|
|
p = date.match /^(\d+) W(\d+)$/
|
2012-04-04 22:39:59 +02:00
|
|
|
q = date.match /^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d\d):?(\d\d))?$/
|
|
|
|
r = date.match /^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d\d):?(\d\d))?$/
|
2012-04-01 23:29:37 +02:00
|
|
|
if m
|
|
|
|
new Date(
|
|
|
|
parseInt(m[1], 10),
|
|
|
|
parseInt(m[2], 10) * 3 - 1,
|
|
|
|
1).getTime()
|
|
|
|
else if n
|
|
|
|
new Date(
|
|
|
|
parseInt(n[1], 10),
|
|
|
|
parseInt(n[2], 10) - 1,
|
|
|
|
1).getTime()
|
|
|
|
else if o
|
|
|
|
new Date(
|
|
|
|
parseInt(o[1], 10),
|
|
|
|
parseInt(o[2], 10) - 1,
|
|
|
|
parseInt(o[3], 10)).getTime()
|
|
|
|
else if p
|
|
|
|
# calculate number of weeks in year given
|
|
|
|
ret = new Date(parseInt(p[1], 10), 0, 1);
|
|
|
|
# first thursday in year (ISO 8601 standard)
|
|
|
|
if ret.getDay() isnt 4
|
|
|
|
ret.setMonth(0, 1 + ((4 - ret.getDay()) + 7) % 7);
|
|
|
|
# add weeks
|
|
|
|
ret.getTime() + parseInt(p[2], 10) * 604800000
|
|
|
|
else if q
|
2012-04-04 23:07:09 +02:00
|
|
|
if not q[6]
|
2012-04-04 22:39:59 +02:00
|
|
|
# no timezone info, use local
|
|
|
|
new Date(
|
|
|
|
parseInt(q[1], 10),
|
|
|
|
parseInt(q[2], 10) - 1,
|
|
|
|
parseInt(q[3], 10),
|
|
|
|
parseInt(q[4], 10),
|
|
|
|
parseInt(q[5], 10)).getTime()
|
|
|
|
else
|
|
|
|
# timezone info supplied, use UTC
|
|
|
|
offsetmins = 0
|
|
|
|
if q[6] != 'Z'
|
|
|
|
offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10)
|
|
|
|
offsetmins = 0 - offsetmins if q[7] == '+'
|
|
|
|
Date.UTC(
|
|
|
|
parseInt(q[1], 10),
|
|
|
|
parseInt(q[2], 10) - 1,
|
|
|
|
parseInt(q[3], 10),
|
|
|
|
parseInt(q[4], 10),
|
|
|
|
parseInt(q[5], 10) + offsetmins)
|
2012-04-01 23:29:37 +02:00
|
|
|
else if r
|
|
|
|
secs = parseFloat(r[6])
|
|
|
|
isecs = Math.floor(secs)
|
2012-04-04 22:39:59 +02:00
|
|
|
msecs = Math.round((secs - isecs) * 1000)
|
2012-04-04 23:07:09 +02:00
|
|
|
if not r[8]
|
2012-04-04 22:39:59 +02:00
|
|
|
# no timezone info, use local
|
|
|
|
new Date(
|
|
|
|
parseInt(r[1], 10),
|
|
|
|
parseInt(r[2], 10) - 1,
|
|
|
|
parseInt(r[3], 10),
|
|
|
|
parseInt(r[4], 10),
|
|
|
|
parseInt(r[5], 10),
|
|
|
|
isecs,
|
|
|
|
msecs).getTime()
|
|
|
|
else
|
|
|
|
# timezone info supplied, use UTC
|
|
|
|
offsetmins = 0
|
|
|
|
if r[8] != 'Z'
|
|
|
|
offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10)
|
|
|
|
offsetmins = 0 - offsetmins if r[9] == '+'
|
|
|
|
Date.UTC(
|
|
|
|
parseInt(r[1], 10),
|
|
|
|
parseInt(r[2], 10) - 1,
|
|
|
|
parseInt(r[3], 10),
|
|
|
|
parseInt(r[4], 10),
|
|
|
|
parseInt(r[5], 10) + offsetmins,
|
|
|
|
isecs,
|
|
|
|
msecs)
|
2012-04-01 23:29:37 +02:00
|
|
|
else
|
|
|
|
new Date(parseInt(date, 10), 0, 1).getTime()
|
2012-02-23 09:11:31 +01:00
|
|
|
|
2012-04-01 23:29:37 +02:00
|
|
|
# make long numbers prettier by inserting commas
|
|
|
|
# eg: commas(1234567) -> '1,234,567'
|
|
|
|
#
|
|
|
|
Morris.commas = (num) ->
|
|
|
|
if num is null
|
|
|
|
"n/a"
|
|
|
|
else
|
|
|
|
ret = if num < 0 then "-" else ""
|
|
|
|
absnum = Math.abs(num)
|
|
|
|
intnum = Math.floor(absnum).toFixed(0)
|
|
|
|
ret += intnum.replace(/(?=(?:\d{3})+$)(?!^)/g, ',')
|
|
|
|
strabsnum = absnum.toString()
|
|
|
|
if strabsnum.length > intnum.length
|
|
|
|
ret += strabsnum.slice(intnum.length)
|
|
|
|
ret
|
|
|
|
|
|
|
|
# zero-pad numbers to two characters wide
|
|
|
|
#
|
|
|
|
Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number
|
|
|
|
|
|
|
|
# generate a series of label, timestamp pairs for x-axis labels
|
|
|
|
#
|
2012-04-04 21:32:24 +02:00
|
|
|
Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) ->
|
2012-04-01 23:29:37 +02:00
|
|
|
ddensity = 200 * (dmax - dmin) / pxwidth # seconds per `margin` pixels
|
|
|
|
d0 = new Date(dmin)
|
2012-04-02 22:27:45 +02:00
|
|
|
spec = Morris.LABEL_SPECS[specName]
|
|
|
|
# if the spec doesn't exist, search for the closest one in the list
|
|
|
|
if spec is undefined
|
|
|
|
for name in Morris.AUTO_LABEL_ORDER
|
|
|
|
s = Morris.LABEL_SPECS[name]
|
|
|
|
if ddensity >= s.span
|
|
|
|
spec = s
|
|
|
|
break
|
|
|
|
# if we run out of options, use second-intervals
|
|
|
|
if spec is undefined
|
|
|
|
spec = Morris.LABEL_SPECS["second"]
|
2012-04-03 17:31:59 +02:00
|
|
|
# check if there's a user-defined formatting function
|
2012-04-04 21:32:24 +02:00
|
|
|
if xLabelFormat
|
|
|
|
spec = $.extend({}, spec, {fmt: xLabelFormat})
|
2012-04-02 22:27:45 +02:00
|
|
|
# calculate labels
|
|
|
|
d = spec.start(d0)
|
|
|
|
ret = []
|
|
|
|
while (t = d.getTime()) <= dmax
|
|
|
|
if t >= dmin
|
|
|
|
ret.push [spec.fmt(d), t]
|
|
|
|
spec.incr(d)
|
|
|
|
return ret
|
2012-04-01 23:29:37 +02:00
|
|
|
|
|
|
|
minutesSpecHelper = (interval) ->
|
2012-04-02 22:27:45 +02:00
|
|
|
span: interval * 60 * 1000
|
|
|
|
start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours())
|
|
|
|
fmt: (d) -> "#{Morris.pad2(d.getHours())}:#{Morris.pad2(d.getMinutes())}"
|
|
|
|
incr: (d) -> d.setMinutes(d.getMinutes() + interval)
|
|
|
|
|
2012-04-01 23:29:37 +02:00
|
|
|
secondsSpecHelper = (interval) ->
|
2012-04-02 22:27:45 +02:00
|
|
|
span: interval * 1000
|
|
|
|
start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes())
|
|
|
|
fmt: (d) -> "#{Morris.pad2(d.getHours())}:#{Morris.pad2(d.getMinutes())}:#{Morris.pad2(d.getSeconds())}"
|
|
|
|
incr: (d) -> d.setSeconds(d.getSeconds() + interval)
|
|
|
|
|
|
|
|
Morris.LABEL_SPECS =
|
|
|
|
"year":
|
2012-04-01 23:29:37 +02:00
|
|
|
span: 17280000000 # 365 * 24 * 60 * 60 * 1000
|
|
|
|
start: (d) -> new Date(d.getFullYear(), 0, 1)
|
|
|
|
fmt: (d) -> "#{d.getFullYear()}"
|
|
|
|
incr: (d) -> d.setFullYear(d.getFullYear() + 1)
|
2012-04-02 22:27:45 +02:00
|
|
|
"month":
|
2012-04-01 23:29:37 +02:00
|
|
|
span: 2419200000 # 28 * 24 * 60 * 60 * 1000
|
|
|
|
start: (d) -> new Date(d.getFullYear(), d.getMonth(), 1)
|
|
|
|
fmt: (d) -> "#{d.getFullYear()}-#{Morris.pad2(d.getMonth() + 1)}"
|
|
|
|
incr: (d) -> d.setMonth(d.getMonth() + 1)
|
2012-04-02 22:27:45 +02:00
|
|
|
"day":
|
2012-04-01 23:29:37 +02:00
|
|
|
span: 86400000 # 24 * 60 * 60 * 1000
|
|
|
|
start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
|
|
|
fmt: (d) -> "#{d.getFullYear()}-#{Morris.pad2(d.getMonth() + 1)}-#{Morris.pad2(d.getDate())}"
|
|
|
|
incr: (d) -> d.setDate(d.getDate() + 1)
|
2012-04-02 22:27:45 +02:00
|
|
|
"hour": minutesSpecHelper(60)
|
|
|
|
"30min": minutesSpecHelper(30)
|
|
|
|
"15min": minutesSpecHelper(15)
|
|
|
|
"10min": minutesSpecHelper(10)
|
|
|
|
"5min": minutesSpecHelper(5)
|
|
|
|
"minute": minutesSpecHelper(1)
|
|
|
|
"30sec": secondsSpecHelper(30)
|
|
|
|
"15sec": secondsSpecHelper(15)
|
|
|
|
"10sec": secondsSpecHelper(10)
|
|
|
|
"5sec": secondsSpecHelper(5)
|
|
|
|
"second": secondsSpecHelper(1)
|
|
|
|
|
|
|
|
Morris.AUTO_LABEL_ORDER = [
|
|
|
|
"year", "month", "day", "hour",
|
|
|
|
"30min", "15min", "10min", "5min", "minute",
|
|
|
|
"30sec", "15sec", "10sec", "5sec", "second"
|
2012-04-01 23:29:37 +02:00
|
|
|
]
|
2012-02-23 22:59:45 +01:00
|
|
|
|
2012-02-26 01:28:58 +01:00
|
|
|
window.Morris = Morris
|
2012-02-23 09:16:42 +01:00
|
|
|
# vim: set et ts=2 sw=2 sts=2
|