2012-10-21 23:33:49 +02:00
|
|
|
class Morris.Line extends Morris.Grid
|
2012-11-18 20:02:46 +01:00
|
|
|
@include Morris.Hover
|
|
|
|
|
2012-09-01 12:04:06 +02:00
|
|
|
# Initialise the graph.
|
|
|
|
#
|
|
|
|
constructor: (options) ->
|
2012-10-21 23:33:49 +02:00
|
|
|
return new Morris.Line(options) unless (@ instanceof Morris.Line)
|
|
|
|
super(options)
|
2012-09-01 12:04:06 +02:00
|
|
|
|
2012-10-21 23:33:49 +02:00
|
|
|
init: ->
|
2012-09-01 12:04:06 +02:00
|
|
|
# Some instance variables for later
|
|
|
|
@pointGrow = Raphael.animation r: @options.pointSize + 3, 25, 'linear'
|
|
|
|
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
|
2012-11-18 20:02:46 +01:00
|
|
|
|
|
|
|
@hoverConfigure @options.hoverOptions
|
|
|
|
|
2012-09-01 12:04:06 +02:00
|
|
|
# column hilight events
|
2012-11-18 20:02:46 +01:00
|
|
|
if @options.hilight
|
|
|
|
@prevHilight = null
|
|
|
|
@el.mousemove (evt) =>
|
|
|
|
@updateHilight evt.pageX
|
|
|
|
if @options.hilightAutoHide
|
|
|
|
@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
|
|
|
|
|
|
|
|
postInit: ->
|
|
|
|
@hoverInit()
|
2012-09-01 12:04:06 +02:00
|
|
|
|
|
|
|
# Default configuration
|
|
|
|
#
|
|
|
|
defaults:
|
|
|
|
lineWidth: 3
|
|
|
|
pointSize: 4
|
|
|
|
lineColors: [
|
|
|
|
'#0b62a4'
|
|
|
|
'#7A92A3'
|
|
|
|
'#4da74d'
|
|
|
|
'#afd8f8'
|
|
|
|
'#edc240'
|
|
|
|
'#cb4b4b'
|
|
|
|
'#9440ed'
|
|
|
|
]
|
2012-10-13 11:16:56 +02:00
|
|
|
pointWidths: [1]
|
|
|
|
pointStrokeColors: ['#ffffff']
|
|
|
|
pointFillColors: []
|
2012-09-01 12:04:06 +02:00
|
|
|
smooth: true
|
2012-11-18 20:02:46 +01:00
|
|
|
hilight: true
|
|
|
|
hilightAutoHide: false
|
2012-09-01 12:04:06 +02:00
|
|
|
xLabels: 'auto'
|
|
|
|
xLabelFormat: null
|
|
|
|
|
|
|
|
# Do any size-related calculations
|
|
|
|
#
|
2012-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-09-01 12:04:06 +02:00
|
|
|
calc: ->
|
2012-10-28 19:55:37 +01:00
|
|
|
@calcPoints()
|
2012-11-18 20:02:46 +01:00
|
|
|
@hoverCalculateMargins()
|
2012-10-28 19:55:37 +01:00
|
|
|
@generatePaths()
|
2012-11-18 20:02:46 +01:00
|
|
|
@calcHilightMargins()
|
2012-10-28 19:55:37 +01:00
|
|
|
|
|
|
|
# calculate series data point coordinates
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
calcPoints: ->
|
2012-10-23 10:15:21 +02:00
|
|
|
for row in @data
|
|
|
|
row._x = @transX(row.x)
|
|
|
|
row._y = for y in row.y
|
2012-11-06 08:48:23 +01:00
|
|
|
if y? then @transY(y) else null
|
2012-10-28 19:55:37 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
# calculate hilight margins
|
2012-10-28 19:55:37 +01:00
|
|
|
#
|
|
|
|
# @private
|
2012-11-18 20:02:46 +01:00
|
|
|
calcHilightMargins: ->
|
|
|
|
@hilightMargins = ((r._x + @data[i]._x) / 2 for r, i in @data.slice(1))
|
|
|
|
|
|
|
|
hoverCalculateMargins: ->
|
2012-11-06 08:36:36 +01:00
|
|
|
@hoverMargins = ((r._x + @data[i]._x) / 2 for r, i in @data.slice(1))
|
2012-10-21 23:33:49 +02:00
|
|
|
|
2012-10-28 19:55:37 +01:00
|
|
|
# generate paths for series lines
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
generatePaths: ->
|
|
|
|
@paths = for i in [0...@options.ykeys.length]
|
|
|
|
smooth = @options.smooth is true or @options.ykeys[i] in @options.smooth
|
|
|
|
coords = ({x: r._x, y: r._y[i]} for r in @data when r._y[i] isnt null)
|
|
|
|
if coords.length > 1
|
|
|
|
@createPath coords, smooth
|
|
|
|
else
|
|
|
|
null
|
|
|
|
|
2012-10-21 23:33:49 +02:00
|
|
|
# Draws the line chart.
|
2012-09-01 12:04:06 +02:00
|
|
|
#
|
2012-10-21 23:33:49 +02:00
|
|
|
draw: ->
|
|
|
|
@drawXAxis()
|
2012-09-01 12:04:06 +02:00
|
|
|
@drawSeries()
|
2012-11-18 20:02:46 +01:00
|
|
|
@hilight(if @options.hilightAutoHide then null else @data.length - 1) if @options.hilight
|
2012-09-01 12:04:06 +02:00
|
|
|
|
2012-10-21 23:33:49 +02:00
|
|
|
# draw the x-axis labels
|
2012-09-01 12:04:06 +02:00
|
|
|
#
|
2012-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-10-21 23:33:49 +02:00
|
|
|
drawXAxis: ->
|
|
|
|
# draw x axis labels
|
|
|
|
ypos = @bottom + @options.gridTextSize * 1.25
|
2012-09-01 12:04:06 +02:00
|
|
|
xLabelMargin = 50 # make this an option?
|
|
|
|
prevLabelMargin = null
|
|
|
|
drawLabel = (labelText, xpos) =>
|
|
|
|
label = @r.text(@transX(xpos), ypos, labelText)
|
|
|
|
.attr('font-size', @options.gridTextSize)
|
|
|
|
.attr('fill', @options.gridTextColor)
|
|
|
|
labelBox = label.getBBox()
|
|
|
|
# ensure a minimum of `xLabelMargin` pixels between labels, and ensure
|
|
|
|
# labels don't overflow the container
|
2012-11-06 08:48:23 +01:00
|
|
|
if (not prevLabelMargin? or prevLabelMargin >= labelBox.x + labelBox.width) and
|
2012-09-01 12:04:06 +02:00
|
|
|
labelBox.x >= 0 and (labelBox.x + labelBox.width) < @el.width()
|
2012-10-28 21:30:48 +01:00
|
|
|
prevLabelMargin = labelBox.x - xLabelMargin
|
2012-09-01 12:04:06 +02:00
|
|
|
else
|
|
|
|
label.remove()
|
|
|
|
if @options.parseTime
|
2012-10-23 10:15:21 +02:00
|
|
|
if @data.length == 1 and @options.xLabels == 'auto'
|
2012-09-01 12:04:06 +02:00
|
|
|
# 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
|
2012-10-28 21:30:48 +01:00
|
|
|
labels = [[@data[0].label, @data[0].x]]
|
2012-09-01 12:04:06 +02:00
|
|
|
else
|
2012-10-28 21:30:48 +01:00
|
|
|
labels = Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat)
|
2012-09-01 12:04:06 +02:00
|
|
|
else
|
2012-10-28 21:30:48 +01:00
|
|
|
labels = ([row.label, row.x] for row in @data)
|
|
|
|
labels.reverse()
|
|
|
|
for l in labels
|
|
|
|
drawLabel(l[0], l[1])
|
2012-09-01 12:04:06 +02:00
|
|
|
|
|
|
|
# draw the data series
|
|
|
|
#
|
2012-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-09-01 12:04:06 +02:00
|
|
|
drawSeries: ->
|
2012-10-23 10:15:21 +02:00
|
|
|
for i in [@options.ykeys.length-1..0]
|
2012-10-28 19:55:37 +01:00
|
|
|
path = @paths[i]
|
|
|
|
if path isnt null
|
2012-09-01 12:04:06 +02:00
|
|
|
@r.path(path)
|
2012-11-18 20:02:46 +01:00
|
|
|
.attr('stroke', @colorFor(row, i, 'line'))
|
2012-09-01 12:04:06 +02:00
|
|
|
.attr('stroke-width', @options.lineWidth)
|
2012-10-23 10:15:21 +02:00
|
|
|
@seriesPoints = ([] for i in [0...@options.ykeys.length])
|
|
|
|
for i in [@options.ykeys.length-1..0]
|
|
|
|
for row in @data
|
|
|
|
if row._y[i] == null
|
2012-09-01 12:04:06 +02:00
|
|
|
circle = null
|
|
|
|
else
|
2012-10-23 10:15:21 +02:00
|
|
|
circle = @r.circle(row._x, row._y[i], @options.pointSize)
|
2012-11-18 20:02:46 +01:00
|
|
|
.attr('fill', @colorFor(row, i, 'point'))
|
2012-10-09 17:27:15 +02:00
|
|
|
.attr('stroke-width', @strokeWidthForSeries(i))
|
|
|
|
.attr('stroke', @strokeForSeries(i))
|
2012-09-01 12:04:06 +02:00
|
|
|
@seriesPoints[i].push(circle)
|
|
|
|
|
|
|
|
# create a path for a data series
|
|
|
|
#
|
2012-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-10-21 23:33:49 +02:00
|
|
|
createPath: (coords, smooth) ->
|
2012-09-01 12:04:06 +02:00
|
|
|
path = ""
|
2012-10-21 01:29:40 +02:00
|
|
|
if smooth
|
2012-09-01 12:04:06 +02:00
|
|
|
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
|
2012-10-21 23:33:49 +02:00
|
|
|
y1 = Math.min(@bottom, lc.y + ix * lg)
|
2012-09-01 12:04:06 +02:00
|
|
|
x2 = c.x - ix
|
2012-10-21 23:33:49 +02:00
|
|
|
y2 = Math.min(@bottom, c.y - ix * g)
|
2012-09-01 12:04:06 +02:00
|
|
|
path += "C#{x1},#{y1},#{x2},#{y2},#{c.x},#{c.y}"
|
|
|
|
else
|
2012-11-06 08:48:23 +01:00
|
|
|
path = "M" + ("#{c.x},#{c.y}" for c in coords).join("L")
|
2012-09-01 12:04:06 +02:00
|
|
|
return path
|
|
|
|
|
|
|
|
# calculate a gradient at each point for a series of points
|
|
|
|
#
|
2012-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-09-01 12:04:06 +02:00
|
|
|
gradients: (coords) ->
|
2012-11-06 08:36:36 +01:00
|
|
|
for c, i in coords
|
2012-09-01 12:04:06 +02:00
|
|
|
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-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-09-01 12:04:06 +02:00
|
|
|
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
|
|
|
|
@prevHilight = index
|
|
|
|
|
2012-09-16 00:05:48 +02:00
|
|
|
# @private
|
2012-09-01 12:04:06 +02:00
|
|
|
updateHilight: (x) =>
|
|
|
|
x -= @el.offset().left
|
2012-11-18 20:02:46 +01:00
|
|
|
for hilightIndex in [0...@hilightMargins.length]
|
|
|
|
break if @hilightMargins[hilightIndex] > x
|
|
|
|
@hilight hilightIndex
|
2012-09-25 22:07:32 +02:00
|
|
|
|
2012-10-09 17:27:15 +02:00
|
|
|
# @private
|
|
|
|
strokeWidthForSeries: (index) ->
|
|
|
|
@options.pointWidths[index % @options.pointWidths.length]
|
|
|
|
|
|
|
|
# @private
|
|
|
|
strokeForSeries: (index) ->
|
|
|
|
@options.pointStrokeColors[index % @options.pointStrokeColors.length]
|
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
colorFor: (row, sidx, type) ->
|
|
|
|
if typeof @options.lineColors is 'function'
|
|
|
|
@options.lineColors.call(@, row, sidx, type)
|
|
|
|
else if type is 'point'
|
|
|
|
@options.pointFillColors[sidx % @options.pointFillColors.length] || @options.lineColors[sidx % @options.lineColors.length]
|
|
|
|
else
|
|
|
|
@options.lineColors[sidx % @options.lineColors.length]
|