-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.epochchart.coffee
121 lines (107 loc) · 2.7 KB
/
jquery.epochchart.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
$ = jQuery
tallestPoint = (lines, x) ->
computedValues = $.map lines, (line) ->
prevPoint = null
ret = null
$.map line['data'], (datum, i) ->
if x >= datum['x']
prevPoint = datum
return
if x < datum['x']
if prevPoint == null
ret = datum['y']
return
# How far between the two x-points?
xdiff = x-prevPoint['x']
slope = ((datum['y']-prevPoint['y'])/(datum['x']-prevPoint['x']))
ret = prevPoint['y'] + slope*(x-prevPoint['x'])
return
ret
ret = null
$.each computedValues, (i, v) ->
ret = v if v? and (not ret? or ret<v)
ret
defaults =
marker: 'url(marker.png)'
dateFormat: '%b %e'
tooltip:
x: 30
y: 10
$.fn.epochchart = (lines, markers, opts={}) ->
opts = $.extend true, {}, defaults, opts
# Support for single or multiple lines
lines = [lines] if Object.prototype.toString.call( lines ) != '[object Array]'
# Default highcharts options
defaultHighchartsOpts =
title:
text: null
xAxis:
type: 'datetime'
dateTimeLabelFormats:
day: opts.dateFormat
year: '%b'
yAxis:
title:
text: null
plotLines: [
value: 0
width: 1
color: '#808080'
]
tooltip:
formatter: ->
date = Highcharts.dateFormat opts.dateFormat, @x
s = '<b>' + date + '</b><br />'
if @point.name?
s += @point.name
else
s += "#{@series.name}: #{@y}"
s
positioner: ->
x: opts.tooltip.x
y: opts.tooltip.y
plotOptions:
scatter:
marker:
symbol: opts['marker']
states:
hover:
enabled: true
lineColor: 'rgb(100,100,100)'
states:
hover:
marker:
enabled: false
spline:
marker:
enabled: false
legend:
enabled: false
# Build options for this chart
highchartsOpts = $.extend true, defaultHighchartsOpts, opts['highchartsOpts']
# Build data
maxY = null;
lines = $.map lines, (line) ->
lineData = $.map line.data, (data) ->
maxY = data[1] if maxY==null or data[1] > maxY
x: new Date(data[0])
y: data[1]
{
type: 'spline',
name: line.name,
data: lineData
}
markerData = $.map markers, (marker) ->
x: new Date(marker[0])
y: tallestPoint(lines, marker[0])
name: marker[1]
markerLine =
type: 'scatter'
name: 'Markers'
data: markerData
lines.push markerLine
# Build data into highcharts options for final chart hash
chart = $.extend true, highchartsOpts,
series: lines
# Build the chart
$(this).highcharts chart