Skip to content

Commit 48aef5a

Browse files
Merge branch 'feature-canvas-graph'
Replaced Google Image Chart with HTML5 Canvas Fix #31 * feature-canvas-graph: auto adjust canvas size based on screen DPI replaced google img chart with canvas
2 parents c243ac9 + 7f59ff6 commit 48aef5a

File tree

2 files changed

+65
-28
lines changed

2 files changed

+65
-28
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,15 @@ <h2>Chase car mode</h2>
263263
</div>
264264
</div>
265265
<script src="http://maps.google.com/maps/api/js?v=3.10&sensor=false&libraries=weather&language=en_us&key=AIzaSyCOqkcNey4CCyG4X0X5qxHAhCgD8g5DwXg" type="text/javascript"></script>
266-
<script type="text/javascript" language="javascript" src="js/mobile.js"></script>
267266
<!--
267+
<script type="text/javascript" language="javascript" src="js/mobile.js"></script>
268+
-->
268269
<script type="text/javascript" language="javascript" src="js/jquery-1.8.3-min.js"></script>
269270
<script type="text/javascript" language="javascript" src="js/chasecar.lib.js"></script>
270271
<script type="text/javascript" language="javascript" src="js/iscroll.js"></script>
271272
<script type="text/javascript" language="javascript" src="js/nite-overlay.js"></script>
272273
<script type="text/javascript" language="javascript" src="js/tracker.js"></script>
273274
<script type="text/javascript" language="javascript" src="js/app.js"></script>
274-
-->
275275
<script type="text/javascript">
276276

277277
var _gaq = _gaq || [];

js/tracker.js

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ var markers_url = "img/markers/";
88
var vehicle_names = [];
99
var vehicles = [];
1010

11-
var graph_url = "http://chart.googleapis.com/chart?chf=bg,s,67676700&chxr=0,0,46|1,0,0|2,0,45&chxs=0,676767,0,0,_,000000|1,676767,0,0,t,676767|2,676767,0,0,_,676767&chxt=r,y,x&chs=300x80&cht=lc&chco=33B5E5&chds=0,{AA}&chls=2&chm=B,33B5E533,0,0,0,-1&chd=";
12-
1311
var receiver_names = [];
1412
var receivers = [];
1513

@@ -435,7 +433,7 @@ function updateVehicleInfo(index, newPosition) {
435433
// start
436434
var a = '<div class="header">'
437435
+ '<span>' + vehicle_names[index] + ' <i class="icon-target"></i></span>'
438-
+ '<img class="graph" src="img/blank.png">'
436+
+ '<canvas class="graph"></canvas>'
439437
+ '<i class="arrow"></i></div>'
440438
+ '<div class="data">'
441439
+ '<img class="'+((vehicle.vehicle_type=="car")?'car':'')+'" src="'+image+'" />'
@@ -473,10 +471,14 @@ function updateVehicleInfo(index, newPosition) {
473471
+ c // receivers if any
474472
+ '';
475473

476-
474+
// update html
477475
$('.portrait .vehicle'+index).html(a + p + b);
478476
$('.landscape .vehicle'+index).html(a + l + b);
479477

478+
// redraw canvas
479+
var c = $('.vehicle'+index+' .graph');
480+
drawAltitudeProfile(c.get(0), c.get(1), vehicles[index].alt_list, vehicles[index].alt_max);
481+
480482
return true;
481483
}
482484

@@ -582,19 +584,60 @@ function convert_time(text) {
582584
return stringToDateUTC(text).getTime();
583585
}
584586

585-
var GChartString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
586-
function GChartEncodeData(valueArray,maxValue) {
587-
var chartData = ['s:'];
588-
for (var i = 0; i < valueArray.length; i++) {
589-
var currentValue = valueArray[i];
590-
591-
if (!isNaN(currentValue) && currentValue >= 0) {
592-
chartData.push(GChartString.charAt(Math.round((GChartString.length-1) * currentValue / maxValue)));
593-
} else {
594-
chartData.push('_');
595-
}
587+
function drawAltitudeProfile(c1, c2, alt_list, alt_max) {
588+
var ctx1 = c1.getContext("2d");
589+
var ctx2 = c2.getContext("2d");
590+
591+
c1 = $(c1);
592+
c2 = $(c2);
593+
594+
var ratio = window.devicePixelRatio;
595+
var cw1 = 150 * ratio;
596+
var ch1 = 40 * ratio;
597+
var cw2 = 60 * ratio;
598+
var ch2 = 40 * ratio;
599+
600+
c1.attr('width', cw1).attr('height', ch1);
601+
c2.attr('width', cw2).attr('height', ch2);
602+
603+
ctx1.fillStyle = "#d6f0f9";
604+
ctx1.lineWidth = 2;
605+
ctx1.strokeStyle= "#33B5F5";
606+
ctx2.fillStyle = "#d6f0f9";
607+
ctx2.lineWidth = 2;
608+
ctx2.strokeStyle= "#33B5F5";
609+
610+
var xt1 = (cw1 - 2) / alt_list.length;
611+
var yt1 = (ch1 - 6) / alt_max;
612+
var xt2 = (cw2 - 2) / alt_list.length;
613+
var yt2 = (ch2 - 6) / alt_max;
614+
615+
//xt1 = (xt1 > 1) ? 1 : xt1;
616+
//yt1 = (yt1 > 1) ? 1 : yt1;
617+
//xt2 = (xt2 > 1) ? 1 : xt2;
618+
//yt2 = (yt2 > 1) ? 1 : yt2;
619+
620+
ctx1.beginPath();
621+
ctx1.moveTo(0,c1.height);
622+
ctx2.beginPath();
623+
ctx2.moveTo(0,c2.height);
624+
625+
var i;
626+
for(i = 0; i < alt_list.length; i++) {
627+
ctx1.lineTo(1+((i+1)*xt1), ch1 - (alt_list[i] * yt1));
628+
ctx2.lineTo(1+((i+1)*xt2), ch2 - (alt_list[i] * yt2));
596629
}
597-
return chartData.join('');
630+
631+
ctx1.stroke();
632+
ctx2.stroke();
633+
634+
ctx1.lineTo(cw1 - 1, ch1);
635+
ctx2.lineTo(cw2 - 1, ch2);
636+
637+
ctx1.closePath();
638+
ctx2.closePath();
639+
ctx1.fill();
640+
ctx2.fill();
598641
}
599642

600643
function addPosition(position) {
@@ -1237,11 +1280,8 @@ function update(response) {
12371280

12381281
// update the altitude profile, only if its a balloon
12391282
if(vehicles[idx].vehicle_type != "car") {
1240-
var graph_src = graph_url.replace("{AA}",vehicles[idx].alt_max); // top range, buttom is always 0
1241-
graph_src += GChartEncodeData(vehicles[idx].alt_list, vehicles[idx].alt_max); // encode datapoint to preserve bandwith
1242-
1243-
// update img element
1244-
$('.vehicle'+idx+' .graph').attr('src', graph_src);
1283+
var c = $('.vehicle'+idx+' .graph');
1284+
drawAltitudeProfile(c.get(0), c.get(1), vehicles[idx].alt_list, vehicles[idx].alt_max);
12451285
}
12461286
}, 400*i);
12471287
} else {
@@ -1250,11 +1290,8 @@ function update(response) {
12501290

12511291
// update the altitude profile, only if its a balloon
12521292
if(vehicles[i].vehicle_type != "car") {
1253-
var graph_src = graph_url.replace("{AA}",vehicles[i].alt_max); // top range, buttom is always 0
1254-
graph_src += GChartEncodeData(vehicles[i].alt_list, vehicles[i].alt_max); // encode datapoint to preserve bandwith
1255-
1256-
// update img element
1257-
$('.vehicle'+i+' .graph').attr('src', graph_src);
1293+
var c = $('.vehicle'+idx+' .graph');
1294+
drawAltitudeProfile(c.get(0), c.get(1), vehicles[idx].alt_list, vehicles[idx].alt_max);
12581295
}
12591296

12601297
// remember last position for each vehicle

0 commit comments

Comments
 (0)