forked from n0nick/simplegraph
/
swellchart.html
282 lines (261 loc) · 10.6 KB
/
swellchart.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>SimpleGraph - simple javascript graphs with Raphael and jQuery</title>
<script src="../vendor/jquery.js" type="text/javascript"></script>
<script src="../vendor/raphael.js" type="text/javascript"></script>
<script src="../jquery.simplegraph.js" type="text/javascript"></script>
<script src="swellchart.js" type="text/javascript"></script>
<style type="text/css">
#container {
margin:auto;
width:900px;
}
.chart {
margin: 50px;
}
pre {
background:#EEEEEE none repeat scroll 0 0;
border:4px solid #DDDDDD;
line-height:20px;
overflow:auto;
padding:5px;
width:800px;
}
h1, h2, h3 {
font-family:verdana;
}
a {
background-color:#FFFFCC;
color:#000000;
text-decoration:none;
}
a:hover {
background-color:yellow;
}
</style>
</head>
<body>
<a href="http://github.com/benaskins/simplegraph">
<img alt="Fork me on GitHub" src="http://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" style="border: 0pt none ; position: absolute; top: 0pt; right: 0pt;"/>
</a>
<div id='container'>
<h1>SimpleGraph - Swell Chart Example</h1>
<h2>
Using SimpleGraph to create more sophisticated graphs. See below for the sample code.
</h2>
<div class="swellgraph">
<table class="swelldata" style="display: none;">
<tbody><tr>
<td class="forecast_day">Fri</td>
<td class="wave_height_am">1.5</td>
<td class="wave_height_pm">2</td>
<td class="wind_direction_am">w</td>
<td class="wind_direction_pm">wsw</td>
<td class="swell_direction">wnw</td>
</tr>
<tr>
<td class="forecast_day">Sat</td>
<td class="wave_height_am">3</td>
<td class="wave_height_pm">3</td>
<td class="wind_direction_am">wnw</td>
<td class="wind_direction_pm">wnw</td>
<td class="swell_direction">wsw</td>
</tr>
<tr>
<td class="forecast_day">Sun</td>
<td class="wave_height_am">2</td>
<td class="wave_height_pm">4</td>
<td class="wind_direction_am">sw</td>
<td class="wind_direction_pm">sw</td>
<td class="swell_direction">sw</td>
</tr>
<tr>
<td class="forecast_day">Mon</td>
<td class="wave_height_am">2.5</td>
<td class="wave_height_pm">3.5</td>
<td class="wind_direction_am">wnw</td>
<td class="wind_direction_pm">wsw</td>
<td class="swell_direction">w</td>
</tr>
<tr>
<td class="forecast_day">Tue</td>
<td class="wave_height_am">4</td>
<td class="wave_height_pm">4.5</td>
<td class="wind_direction_am">w</td>
<td class="wind_direction_pm">wsw</td>
<td class="swell_direction">wsw</td>
</tr>
</tbody></table>
<div class="chart"> </div>
</div>
<pre>
(function($) {
var SwellChart = {
drawBackground: function() {
this.canvas.rect(0, 0, 300, 330).attr({stroke: "none", fill: "#eaeaea"});
},
plotAmWaveHeights: function() {
this.dataSet.plot(this.grid, this.canvas);
this.addAmPmIndicators("am");
},
plotPmWaveHeights: function(wave_height_pm) {
this.settings.barOffset = this.settings.barWidth;
this.settings.barColor = "#fff";
this.replaceDataSet(wave_height_pm);
this.plotCurrentDataSet();
this.addAmPmIndicators("pm");
},
drawYAxisSightLines: function() {
var increment = 1;
if (this.grid.maxValueYAxis > 10) {
increment = 2;
}
if (this.grid.maxValueYAxis > 20) {
increment = 3;
}
for (var i = 0, ii = (this.grid.maxValueYAxis); i <= ii; i = i + increment) {
var value = i,
y = this.grid.y(value),
x = this.grid.leftEdge;
this.canvas.path({stroke: "#fff"}).moveTo(x,y).lineTo(285, y);
}
},
addAmPmIndicators: function(am_or_pm) {
(function(sg) {
$.each(sg.dataSet.data, function(i, value) {
var x = sg.grid.x(i);
var color = (am_or_pm == "am") ? "#fff" : "#000";
sg.canvas.text(x + sg.dataSet.settings.barOffset + 10, 192, am_or_pm.toUpperCase()).attr({"font-family": "Arial", "font-size": "8px", fill: color});
});
})(this);
},
labelYAxis: function() {
var increment = 1;
var start_from = 1;
if (this.grid.maxValueYAxis > 10) {
start_from = 2;
increment = 2;
}
if (this.grid.maxValueYAxis > 20) {
start_from = 3;
increment = 3;
}
for (var i = start_from, ii = (this.grid.maxValueYAxis); i <= ii; i = i + increment) {
var value = i,
y = this.grid.y(value) + 4,
x = this.grid.leftEdge - 15;
this.canvas.text(x, y, value + "ft").attr(this.settings.yAxisLabelStyle);
}
},
plotSwellDirections: function(swell_direction) {
// Add the swell direction rectangle
this.canvas.rect(this.grid.leftEdge, 225, 285 - this.grid.leftEdge, 40).attr({stroke: "none", fill: "#fff"});
this.canvas.image("images/chart-swell.gif", this.grid.leftEdge - 15, 230, 8, 27);
// Plot the directions
(function(sg) {
$.each(swell_direction, function(i, sdir) {
var x = sg.grid.x(i);
sg.canvas.image("images/chart-arrow-" + sdir + ".gif", x + 12, 232, 16, 16);
sg.canvas.text(x + 20, 259, sdir.toUpperCase()).attr(sg.settings.windDirLabelStyle);
});
})(this);
},
plotWindDirections: function(wind_direction_am, wind_direction_pm) {
// Add the wind direction rectangle
this.canvas.rect(this.grid.leftEdge, 275, 285 - this.grid.leftEdge, 40).attr({stroke: "none", fill: "#fff"});
this.canvas.image("images/chart-wind.gif", this.grid.leftEdge - 15, 285, 8, 19);
// Plot the directions
this.plotWindDirection(wind_direction_am, 2);
this.plotWindDirection(wind_direction_pm, 22);
},
plotWindDirection: function(wind_direction, offset) {
(function(sg) {
$.each(wind_direction, function(i, sdir) {
var x = sg.grid.x(i);
sg.canvas.image("images/chart-arrow-" + sdir + ".gif", x + offset, 283 , 16, 15).toFront();
sg.canvas.text(x + offset + 8, 310, sdir.toUpperCase()).attr(sg.settings.windDirLabelStyle).toFront();
});
})(this);
},
draw: function() {
this.drawBackground();
this.drawYAxisSightLines();
this.dataSet.labelXAxis(this.grid, this.canvas);
this.plotAmWaveHeights();
this.plotPmWaveHeights(wave_height_pm);
this.labelYAxis();
this.plotSwellDirections(swell_direction);
this.plotWindDirections(wind_direction_am, wind_direction_pm);
},
defaults: {
width: 300,
height: 215,
drawLine: false,
drawBars: true,
barWidth: 20,
barOffset: 0,
barColor: "#416A97",
xAxisLabelOffset: 20,
// Day labels
xAxisLabelFont: "Helvetica",
xAxisLabelColor: "#58595B",
xAxisLabelFontSize: "11px",
xAxisLabelFontWeight: "600",
// Wave height labels (1ft, 2ft...)
yAxisLabelFont: "Helvetica",
yAxisLabelColor: "#58595B",
yAxisLabelFontSize: "11px",
yAxisLabelFontWeight: "600",
// Wind direction labels
windDirLabelFont: "Arial",
windDirLabelFontSize: "8px",
windDirLabelStyle: {
font: "8px Arial"
}
}
};
var wave_height_am = [],
wave_height_pm = [],
swell_direction = [],
wind_direction_am = [],
wind_direction_pm = [],
forecast_day = [];
$(function() {
gatherData();
$(".swellgraph .chart").each( function() {
var canvas = Raphael(this, 300, 330);
var swellchart = new SimpleGraph(wave_height_am, forecast_day, canvas, $.extend({minYAxisValue: maxWaveHeight()}, $.fn.simplegraph.defaults, SwellChart.defaults));
$.extend(swellchart, SwellChart);
swellchart.draw();
});
$(".swellgraph table").hide();
});
function gatherData() {
$("table.swelldata td.wave_height_am").each(function () {
wave_height_am.push($(this).html());
});
$("table.swelldata td.wave_height_pm").each(function () {
wave_height_pm.push($(this).html());
});
$("table.swelldata td.swell_direction").each(function () {
swell_direction.push($(this).html());
});
$("table.swelldata td.wind_direction_am").each(function () {
wind_direction_am.push($(this).html());
});
$("table.swelldata td.wind_direction_pm").each(function () {
wind_direction_pm.push($(this).html());
});
$("table.swelldata td.forecast_day").each(function () {
forecast_day.push($(this).html());
});
}
function maxWaveHeight() {
return Math.ceil(Math.max.apply(Math, wave_height_am.concat(wave_height_pm)));
}
})(jQuery); </pre>
</div>
</body>
</html>